Skip to content

Commit

Permalink
fix: using f64 in get_float instead of f32 sl-sh-dev#123 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLove committed Feb 24, 2024
1 parent 41d8e9c commit a3db751
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 4 additions & 4 deletions vm/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,11 @@ impl Value {
}
}

pub fn get_float<ENV>(&self, _vm: &GVm<ENV>) -> VMResult<f32> {
pub fn get_float<ENV>(&self, _vm: &GVm<ENV>) -> VMResult<f64> {
match &self {
Value::Byte(b) => Ok(*b as f32),
Value::Int(i) => Ok(from_i56(i) as f32),
Value::Float(f) => Ok(f32::from(*f)),
Value::Byte(b) => Ok(*b as f64),
Value::Int(i) => Ok(from_i56(i) as f64),
Value::Float(f) => Ok(f64::from(*f)),
_ => Err(VMError::new_value(format!("Not a float: {self:?}"))),
}
}
Expand Down
8 changes: 5 additions & 3 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use crate::{
from_i56, CallFrame, CallFunc, CallFuncSig, Chunk, Globals, Handle, Heap, Interner, VMError,
VMErrorObj, VMResult, Value, HALT,
VMErrorObj, VMResult, Value, F56, HALT,
};

mod cons;
Expand Down Expand Up @@ -220,7 +220,7 @@ impl<ENV> GVm<ENV> {
val = Value::True;
}
} else if val1.is_number() && val2.is_number() {
if (val1.get_float(self)? - val2.get_float(self)?).abs() < f32::EPSILON {
if (val1.get_float(self)? - val2.get_float(self)?).abs() < F56::EPSILON {
val = Value::True;
}
} else {
Expand Down Expand Up @@ -911,7 +911,9 @@ mod tests {
let chunk = Arc::new(chunk);
vm.execute(chunk)?;
let result = vm.stack(5).get_float(&vm)?;
assert!(result == 12500.0);

// NOTE: converting the result to f32 because the f64 expects more precision than our F56 can provide.
assert_eq!(result as f32, 12500.0);

Ok(())
}
Expand Down

0 comments on commit a3db751

Please sign in to comment.