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
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Unreleased
- `[@@deriving abstract]` had been deprecated since Melange 5 (in
[#979](https://github.com/melange-re/melange/pull/979)), and has now been
removed in [#1624](https://github.com/melange-re/melange/pull/1624)
- runtime: support more functions in `Stdlib.Float`
([#1625](https://github.com/melange-re/melange/pull/1625))


5.1.0-53 2025-03-23
Expand Down
5 changes: 3 additions & 2 deletions jscomp/core/lam_dispatch_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
call Js_runtime_modules.float
| "caml_fmod_float" (* float module like js number module *) -> (
match args with [ e0; e1 ] -> E.float_mod e0 e1 | _ -> assert false)
| "caml_signbit_float" -> (
| "caml_fma_float" -> (
match args with
| [ e0 ] -> E.float_comp CFlt e0 E.zero_float_lit
| [ e0; e1; e2 ] -> E.float_add (E.float_mul e0 e1) e2
| _ -> assert false)
| "caml_signbit_float" -> call Js_runtime_modules.float
| "caml_string_equal" -> (
match args with [ e0; e1 ] -> E.string_equal e0 e1 | _ -> assert false)
| "caml_string_notequal" -> (
Expand Down
8 changes: 8 additions & 0 deletions jscomp/runtime/caml_float.ml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ let caml_copysign_float (x : float) (y : float) : float =
let y = if y = 0. then 1. /. y else y in
if y < 0. then -.x else x

let caml_signbit_float =
let infinity = 0x1p2047 in
let neg_infinity = -0x1p2047 in
fun (x : float) ->
if x == 0. && 1. /. x == neg_infinity then true
else if x == 0. && 1. /. x == infinity then false
else x < 0.

(* http://www.johndcook.com/blog/cpp_expm1/ *)
let caml_expm1_float : float -> float = function
| x ->
Expand Down
1 change: 1 addition & 0 deletions jscomp/runtime/caml_float.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ val caml_copysign_float : float -> float -> float
val caml_expm1_float : float -> float
val caml_hypot_float : float -> float -> float
val caml_log10_float : float -> float
val caml_signbit_float : float -> bool
12 changes: 4 additions & 8 deletions jscomp/stdlib/float.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,10 @@ external erf : float -> float = "caml_erf_float" "caml_erf"
[@@unboxed] [@@noalloc]
external erfc : float -> float = "caml_erfc_float" "caml_erfc"
[@@unboxed] [@@noalloc]
external trunc : float -> float = "caml_trunc_float" "caml_trunc"
[@@unboxed] [@@noalloc]
external round : float -> float = "caml_round_float" "caml_round"
[@@unboxed] [@@noalloc]
external ceil : float -> float = "caml_ceil_float" "ceil"
[@@unboxed] [@@noalloc]
external floor : float -> float = "caml_floor_float" "floor"
[@@unboxed] [@@noalloc]
external trunc : float -> float = "trunc" [@@mel.scope "Math"]
external round : float -> float = "round" [@@mel.scope "Math"]
external ceil : float -> float = "ceil" [@@mel.scope "Math"]
external floor : float -> float = "floor" [@@mel.scope "Math"]
#else
external pow : float -> float -> float = "caml_power_float" "pow"
[@@unboxed] [@@noalloc]
Expand Down
16 changes: 16 additions & 0 deletions jscomp/stdlib/float.cppo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,23 @@ external erfc : float -> float = "caml_erfc_float" "caml_erfc"
@since 4.13
*)

#ifdef BS
external trunc : float -> float = "trunc" [@@mel.scope "Math"]
#else
external trunc : float -> float = "caml_trunc_float" "caml_trunc"
[@@unboxed] [@@noalloc]
#endif
(** [trunc x] rounds [x] to the nearest integer whose absolute value is
less than or equal to [x].

@since 4.08 *)

#ifdef BS
external round : float -> float = "round" [@@mel.scope "Math"]
#else
external round : float -> float = "caml_round_float" "caml_round"
[@@unboxed] [@@noalloc]
#endif
(** [round x] rounds [x] to the nearest integer with ties (fractional
values of 0.5) rounded away from zero, regardless of the current
rounding direction. If [x] is an integer, [+0.], [-0.], [nan], or
Expand All @@ -459,14 +467,22 @@ external round : float -> float = "caml_round_float" "caml_round"

@since 4.08 *)

#ifdef BS
external ceil : float -> float = "ceil" [@@mel.scope "Math"]
#else
external ceil : float -> float = "caml_ceil_float" "ceil"
[@@unboxed] [@@noalloc]
#endif
(** Round above to an integer value.
[ceil f] returns the least integer value greater than or equal to [f].
The result is returned as a float. *)

#ifdef BS
external floor : float -> float = "floor" [@@mel.scope "Math"]
#else
external floor : float -> float = "caml_floor_float" "floor"
[@@unboxed] [@@noalloc]
#endif
(** Round below to an integer value.
[floor f] returns the greatest integer value less than or
equal to [f].
Expand Down
34 changes: 32 additions & 2 deletions jscomp/test/dist/jscomp/test/float_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions jscomp/test/float_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ let () =
eq __LOC__ (generic_greaterequal Js.Float._NaN 4.2) false;
eq __LOC__ (Float.min (-. 1.) 1.) (-1.);
eq __LOC__ (Float.min 1. (-. 1.)) (-1.);
eq __LOC__ (Float.neg 1.) (-1.);
eq __LOC__ (Float.add 1.2 1.2) 2.4;
eq __LOC__ (Float.mul 1.2 2.) 2.4;
eq __LOC__ (Float.div 2.4 2.) 1.2;
eq __LOC__ (Float.rem 2.4 1.2) 0.;
eq __LOC__ (Float.fma 1.2 2. 1.) 3.4;
eq __LOC__ (Float.copy_sign 1.2 (-1.)) (-1.2);
eq __LOC__ (Float.sign_bit (-1.)) true;
eq __LOC__ (Float.sign_bit (-0.)) true;
eq __LOC__ (Float.sign_bit Float.nan) false;
eq __LOC__ (Float.floor 1.2) 1.0;
eq __LOC__ (Float.ceil 1.2) 2.0;
eq __LOC__ (Float.round 1.2) 1.0;
eq __LOC__ (Float.round 1.6) 2.0;
eq __LOC__ (Float.trunc 1.6) 1.0;
;;


Expand Down