Skip to content

Commit

Permalink
fix: reproduce and fix motoko-base/issues/653 (#4712)
Browse files Browse the repository at this point in the history
Cf. fixes dfinity/motoko-base#653

It appears that `a <> Big_int.zero_big_int` blows up when
`a` is zero. Weird. Eliminating the comparison doesn't hurt
correctness, and is probably more efficient too, as negating
zero should be cheap!

@timohanke feel free to review too!
  • Loading branch information
ggreif authored Sep 26, 2024
1 parent 8d6841e commit 8557a30
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/mo_values/numerics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let bigint_of_double (f : Wasm.F64.t) : Big_int.big_int =

(* Fraction part of IEEE 754 double, 52 bits from the float, with an implicit
1 at the 53rd bit *)
let frac = Int64.(logor (shift_right_logical (shift_left bits 12) 12) (shift_left (of_int 1) 52)) in
let frac = Int64.(logor (shift_right_logical (shift_left bits 12) 12) (shift_left one 52)) in

if Int64.(equal exp bits_11) then
(* Exponent is fully set: NaN or inf *)
Expand All @@ -55,7 +55,7 @@ let bigint_of_double (f : Wasm.F64.t) : Big_int.big_int =

let a = Big_int.big_int_of_int64 frac in

let a = if Int64.(compare exp (of_int 0)) < 0 then
let a = if Int64.(compare exp zero) < 0 then
(* Exponent < 0, shift right *)
Big_int.(shift_right_big_int a (- (Int64.to_int exp)))
else
Expand All @@ -64,7 +64,7 @@ let bigint_of_double (f : Wasm.F64.t) : Big_int.big_int =
in

(* Negate the number if sign bit is set (double is negative) *)
if Int64.shift_right_logical bits 63 = Int64.of_int 1 && a <> Big_int.zero_big_int then
if Int64.(shift_right_logical bits 63 = one) then
Big_int.minus_big_int a
else
a
Expand Down
4 changes: 4 additions & 0 deletions test/run/issue-base653.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { floatCopySign; floatToInt64 } = "mo:⛔";

assert 0 == floatToInt64(-0.5);
assert 0 == floatToInt64(floatCopySign(0.0, -1.0));

0 comments on commit 8557a30

Please sign in to comment.