Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
## Bug fixes
* Runtime: fix parsing of unsigned integers (0u2147483648) (#1633, #1666)
* Runtime: fix incorrect pos_in after unmarshalling
* Runtime: make float_of_string stricter (#1609)
* Toplevel: fix missing primitives with separate compilation
* Compiler: fix link of packed modules with separate compilation
* Compiler: Fixed the static evaluation of some equalities (#1659)
Expand Down
34 changes: 31 additions & 3 deletions compiler/tests-jsoo/test_floats.ml
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,38 @@ let%expect_test "log2" =
p 1024.0;
[%expect {| 10.000000 |}]

let print' f = try print (f ()) with e -> print_endline (Printexc.to_string e)

let%expect_test "of_string" =
let x = "0x1.1" in
print (float_of_string x);
print' (fun () -> float_of_string x);
[%expect {| 1.062500 |}];
let x = "0x1.1p-1" in
print (float_of_string x);
[%expect {| 0.531250 |}]
print' (fun () -> float_of_string x);
[%expect {| 0.531250 |}];
let x = " 0x1.1" in
print' (fun () -> float_of_string x);
[%expect {| 1.062500 |}];
let x = " 0x1.1 " in
print' (fun () -> float_of_string x);
[%expect {| Failure("float_of_string") |}];
let x = "0x1.1 p-1" in
print' (fun () -> float_of_string x);
[%expect {| Failure("float_of_string") |}]

let%expect_test "of_string" =
let x = "3.14" in
print' (fun () -> float_of_string x);
[%expect {| 3.140000 |}];
let x = " 3.14" in
print' (fun () -> float_of_string x);
[%expect {| 3.140000 |}];
let x = "3. 14" in
print' (fun () -> float_of_string x);
[%expect {| Failure("float_of_string") |}];
let x = "3.1 4" in
print' (fun () -> float_of_string x);
[%expect {| Failure("float_of_string") |}];
let x = "3.14 " in
print' (fun () -> float_of_string x);
[%expect {| Failure("float_of_string") |}]
8 changes: 5 additions & 3 deletions runtime/ieee_754.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,14 +562,16 @@ function caml_format_float(fmt, x) {
//Requires: caml_failwith, caml_jsbytes_of_string
function caml_float_of_string(s) {
var res;
var r_float = /^ *[-+]?(?:\d*\.?\d+|\d+\.?\d*)(?:[eE][-+]?\d+)?$/;
s = caml_jsbytes_of_string(s);
res = +s;
//Fast path
if (s.length > 0 && !Number.isNaN(res)) return res;
if (!Number.isNaN(res) && r_float.test(s)) return res;
s = s.replace(/_/g, "");
res = +s;
if (s.length > 0 && (!Number.isNaN(res) || /^[+-]?nan$/i.test(s))) return res;
var m = /^ *([+-]?)0x([0-9a-f]+)\.?([0-9a-f]*)(p([+-]?[0-9]+))?/i.exec(s);
if ((!Number.isNaN(res) && r_float.test(s)) || /^[+-]?nan$/i.test(s))
return res;
var m = /^ *([+-]?)0x([0-9a-f]+)\.?([0-9a-f]*)(p([+-]?[0-9]+))?$/i.exec(s);
// 1 2 3 5
if (m) {
var m3 = m[3].replace(/0+$/, "");
Expand Down