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
34 changes: 16 additions & 18 deletions compiler/lib/javascript.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
5 changes: 4 additions & 1 deletion compiler/tests/variable_declaration_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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.]; |}]