Skip to content

Commit a3db751

Browse files
committed
fix: using f64 in get_float instead of f32 sl-sh-dev#123 (comment)
1 parent 41d8e9c commit a3db751

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

vm/src/value.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,11 @@ impl Value {
457457
}
458458
}
459459

460-
pub fn get_float<ENV>(&self, _vm: &GVm<ENV>) -> VMResult<f32> {
460+
pub fn get_float<ENV>(&self, _vm: &GVm<ENV>) -> VMResult<f64> {
461461
match &self {
462-
Value::Byte(b) => Ok(*b as f32),
463-
Value::Int(i) => Ok(from_i56(i) as f32),
464-
Value::Float(f) => Ok(f32::from(*f)),
462+
Value::Byte(b) => Ok(*b as f64),
463+
Value::Int(i) => Ok(from_i56(i) as f64),
464+
Value::Float(f) => Ok(f64::from(*f)),
465465
_ => Err(VMError::new_value(format!("Not a float: {self:?}"))),
466466
}
467467
}

vm/src/vm.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::Arc;
44

55
use crate::{
66
from_i56, CallFrame, CallFunc, CallFuncSig, Chunk, Globals, Handle, Heap, Interner, VMError,
7-
VMErrorObj, VMResult, Value, HALT,
7+
VMErrorObj, VMResult, Value, F56, HALT,
88
};
99

1010
mod cons;
@@ -220,7 +220,7 @@ impl<ENV> GVm<ENV> {
220220
val = Value::True;
221221
}
222222
} else if val1.is_number() && val2.is_number() {
223-
if (val1.get_float(self)? - val2.get_float(self)?).abs() < f32::EPSILON {
223+
if (val1.get_float(self)? - val2.get_float(self)?).abs() < F56::EPSILON {
224224
val = Value::True;
225225
}
226226
} else {
@@ -911,7 +911,9 @@ mod tests {
911911
let chunk = Arc::new(chunk);
912912
vm.execute(chunk)?;
913913
let result = vm.stack(5).get_float(&vm)?;
914-
assert!(result == 12500.0);
914+
915+
// NOTE: converting the result to f32 because the f64 expects more precision than our F56 can provide.
916+
assert_eq!(result as f32, 12500.0);
915917

916918
Ok(())
917919
}

0 commit comments

Comments
 (0)