Skip to content

Commit 23b25ca

Browse files
pmwhitePhilip Whitehhugo
authored
Runtime: Implement float primitives for 4.13 (#1113)
* Runtime: add float primitives * Add tests for float operations * Stop using polyfills for well-support float functions The fma implementation is taken from https://gist.github.com/Yaffle/fb47de4c18b63147699e0b621f1031f7 The tests were taken directly from the ocaml testsuite and modified to be expect-tests. Co-authored-by: Philip White <[email protected]> Co-authored-by: Hugo Heuzard <[email protected]>
1 parent 6c6664b commit 23b25ca

File tree

6 files changed

+1096
-52
lines changed

6 files changed

+1096
-52
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Lib: add offsetX and offsetY to Dom_html.mouseEvent
55
* Lib: add innerText property for Dom_html
66
* Runtime: add dummy implementation for many dummy primitives
7+
* Runtime: add runtime for new float operation in 4.13 #1113 (by pmwhite)
78

89
## Misc
910
* manual/rev_bindings.wiki: fix compilation error

compiler/tests-jsoo-floats/dune

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(library
2+
(name jsoo_floats_testsuite)
3+
(inline_tests (modes js))
4+
(preprocess (pps ppx_expect)))
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
(* Js_of_ocaml compiler
2+
* http://www.ocsigen.org/js_of_ocaml/
3+
* Copyright (C) 2019 Hugo Heuzard
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, with linking exception;
8+
* either version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
*)
19+
20+
(* TODO: These float tests were put in their own library because they depend on
21+
* primitives that aren't implemented in native OCaml. Once OCaml 4.13 gets
22+
* released, they can be moved back into the tests-jsoo directory.
23+
*)
24+
25+
let%expect_test _ =
26+
(* copied from https://github.com/ocaml/ocaml/pull/1794 *)
27+
let z =
28+
let x = -0. and y = 0. in
29+
if mod_float x 1. >= 0. then x else if false then x else y
30+
in
31+
Printf.printf "%g\n" (1. /. z);
32+
[%expect {|-inf|}]
33+
34+
module Float = struct
35+
include Float
36+
37+
external acosh : float -> float = "caml_acosh_float"
38+
39+
external asinh : float -> float = "caml_asinh_float"
40+
41+
external atanh : float -> float = "caml_atanh_float"
42+
43+
external erf : float -> float = "caml_erf_float"
44+
45+
external erfc : float -> float = "caml_erfc_float"
46+
47+
external cbrt : float -> float = "caml_cbrt_float"
48+
49+
external exp2 : float -> float = "caml_exp2_float"
50+
51+
external log2 : float -> float = "caml_log2_float"
52+
end
53+
54+
let print f =
55+
match Float.classify_float f with
56+
| FP_nan -> print_endline "nan"
57+
| _ -> Printf.printf "%f\n" f
58+
59+
let%expect_test "acosh" =
60+
let p x = print (Float.acosh x) in
61+
p (-1.0);
62+
[%expect {| nan |}];
63+
p 0.0;
64+
[%expect {| nan |}];
65+
p 0.5;
66+
[%expect {| nan |}];
67+
p 1.0;
68+
[%expect {| 0.000000 |}];
69+
p 2.0;
70+
[%expect {| 1.316958 |}]
71+
72+
let%expect_test "asinh" =
73+
let p x = print (Float.asinh x) in
74+
p 1.0;
75+
[%expect {| 0.881374 |}];
76+
p 0.0;
77+
[%expect {| 0.000000 |}];
78+
p (-1.0);
79+
[%expect {| -0.881374 |}];
80+
p 2.0;
81+
[%expect {| 1.443635 |}]
82+
83+
let%expect_test "atanh" =
84+
let p x = print (Float.atanh x) in
85+
p (-2.0);
86+
[%expect {| nan |}];
87+
p (-1.0);
88+
[%expect {| -inf |}];
89+
p 0.0;
90+
[%expect {| 0.000000 |}];
91+
p 0.5;
92+
[%expect {| 0.549306 |}];
93+
p 1.0;
94+
[%expect {| inf |}]
95+
96+
let%expect_test "erf" =
97+
let p x = print (Float.erf x) in
98+
p (-2.0);
99+
[%expect {| -0.995322 |}];
100+
p (-1.0);
101+
[%expect {| -0.842701 |}];
102+
p 0.0;
103+
[%expect {| 0.000000 |}];
104+
p 0.5;
105+
[%expect {| 0.520500 |}];
106+
p 1.0;
107+
[%expect {| 0.842701 |}];
108+
p 10.0;
109+
[%expect {| 1.000000 |}]
110+
111+
let%expect_test "erfc" =
112+
let p x = print (Float.erfc x) in
113+
p (-2.0);
114+
[%expect {| 1.995322 |}];
115+
p (-1.0);
116+
[%expect {| 1.842701 |}];
117+
p 0.0;
118+
[%expect {| 1.000000 |}];
119+
p 0.5;
120+
[%expect {| 0.479500 |}];
121+
p 1.0;
122+
[%expect {| 0.157299 |}];
123+
p 10.0;
124+
[%expect {| 0.000000 |}]
125+
126+
let%expect_test "cbrt" =
127+
let p x = print (Float.cbrt x) in
128+
p Float.nan;
129+
[%expect {| nan |}];
130+
p (-1.0);
131+
[%expect {| -1.000000 |}];
132+
p (-0.0);
133+
[%expect {| -0.000000 |}];
134+
p Float.neg_infinity;
135+
[%expect {| -inf |}];
136+
p 0.0;
137+
[%expect {| 0.000000 |}];
138+
p 1.0;
139+
[%expect {| 1.000000 |}];
140+
p Float.infinity;
141+
[%expect {| inf |}];
142+
p 2.0;
143+
[%expect {| 1.259921 |}]
144+
145+
let%expect_test "exp2" =
146+
let p x = print (Float.exp2 x) in
147+
p Float.nan;
148+
[%expect {| nan |}];
149+
p (-1.0);
150+
[%expect {| 0.500000 |}];
151+
p (-0.0);
152+
[%expect {| 1.000000 |}];
153+
p Float.neg_infinity;
154+
[%expect {| 0.000000 |}];
155+
p 0.0;
156+
[%expect {| 1.000000 |}];
157+
p 1.0;
158+
[%expect {| 2.000000 |}];
159+
p Float.infinity;
160+
[%expect {| inf |}];
161+
p 2.0;
162+
[%expect {| 4.000000 |}]
163+
164+
let%expect_test "log2" =
165+
let p x = print (Float.log2 x) in
166+
p 3.0;
167+
[%expect {| 1.584963 |}];
168+
p 2.0;
169+
[%expect {| 1.000000 |}];
170+
p 1.0;
171+
[%expect {| 0.000000 |}];
172+
p 0.0;
173+
[%expect {| -inf |}];
174+
p (-2.0);
175+
[%expect {| nan |}];
176+
p 1024.0;
177+
[%expect {| 10.000000 |}]

0 commit comments

Comments
 (0)