diff --git a/compiler/lib/javascript.ml b/compiler/lib/javascript.ml index deb7482495..dd4fe8999a 100644 --- a/compiler/lib/javascript.ml +++ b/compiler/lib/javascript.ml @@ -67,25 +67,23 @@ end = struct let of_int32 = Int32.to_string let of_float v = - if Float.equal v infinity - then "Infinity" - else if Float.equal v neg_infinity - then "-Infinity" - else if not (Float.equal v v) - then "NaN" (* [1/-0] = -inf seems to be the only way to detect -0 in JavaScript *) - else if Float.equal v 0. && Float.equal (1. /. v) neg_infinity - then "-0." - else - let vint = int_of_float v in - if Float.equal (float_of_int vint) v - then Printf.sprintf "%d." vint - else - let s1 = Printf.sprintf "%.12g" v in - if Float.equal v (float_of_string s1) - then s1 + match Float.classify_float v with + | FP_nan -> "NaN" + | FP_zero -> + (* [1/-0] < 0. seems to be the only way to detect -0 in JavaScript *) + if Float.(1. /. v < 0.) then "-0." else "0." + | FP_infinite -> if Float.(v < 0.) then "-Infinity" else "Infinity" + | FP_normal | FP_subnormal -> + let vint = int_of_float v in + if Float.equal (float_of_int vint) v + then Printf.sprintf "%d." vint else - let s2 = Printf.sprintf "%.15g" v in - if Float.equal v (float_of_string s2) then s2 else Printf.sprintf "%.18g" v + let s1 = Printf.sprintf "%.12g" v in + if Float.equal v (float_of_string s1) + then s1 + else + let s2 = Printf.sprintf "%.15g" v in + if Float.equal v (float_of_string s2) then s2 else Printf.sprintf "%.18g" v let is_zero s = String.equal s "0" diff --git a/compiler/tests/variable_declaration_output.ml b/compiler/tests/variable_declaration_output.ml index 1716095316..7276f61023 100644 --- a/compiler/tests/variable_declaration_output.ml +++ b/compiler/tests/variable_declaration_output.ml @@ -36,13 +36,16 @@ let%expect_test _ = let ex = {x = 5; y = "hello"} ;; let ax = [|1;2;3;4|] ;; let bx = [|1.0;2.0;3.0;4.0|] ;; + let cx = [|0./.0.;-0./.0.;1./.0.;-1./.0.;0.;-0.|] ;; |} in print_var_decl program "ex"; print_var_decl program "ax"; print_var_decl program "bx"; + print_var_decl program "cx"; [%expect {| var ex = [0,5,runtime.caml_new_string("hello")]; var ax = [0,1,2,3,4]; - var bx = [254,1.,2.,3.,4.]; |}] + var bx = [254,1.,2.,3.,4.]; + var cx = [254,NaN,NaN,Infinity,- Infinity,0.,- 0.]; |}]