Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve Js.Float and change some functions to pipe-last #968

Merged
merged 1 commit into from
Dec 11, 2023
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 @@ -97,6 +97,8 @@ Unreleased
- BREAKING(runtime): Improve docstrings in the `Node` library and change some
of its functions to pipe-last
([#970](https://github.com/melange-re/melange/pull/970))
- BREAKING(runtime): Improve `Js.Float` and change some of its functions to
pipe-last ([#968](https://github.com/melange-re/melange/pull/968))

2.2.0 2023-12-05
---------------
Expand Down
182 changes: 41 additions & 141 deletions jscomp/runtime/js_float.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

(** Provides functions for inspecting and manipulating [float]s
*)
(** Provides functions for inspecting and manipulating [float]s *)

type t = float

external _NaN : float = "NaN"
external _NaN : t = "NaN"

(** The special value "Not a Number"

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN> MDN
*)

external isNaN : float -> bool = "isNaN"
external isNaN : t -> bool = "isNaN"
[@@mel.scope "Number"]
(** Tests if the given value is [_NaN]

Expand All @@ -44,7 +45,7 @@ therefore necessary to test for [_NaN].
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN> MDN
*)

external isFinite : float -> bool = "isFinite"
external isFinite : t -> bool = "isFinite"
[@@mel.scope "Number"]
(** Tests if the given value is finite

Expand All @@ -67,28 +68,8 @@ let _ = Js.Float.isFinite 1234
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite> MDN
*)

external toExponential : float -> string = "toExponential"
[@@mel.send]
(** Formats a [float] using exponential (scientific) notation

{b Returns} a [string] representing the given value in exponential notation

@raise RangeError if digits is not in the range \[0, 20\] (inclusive)

{[
(* prints "7.71234e+1" *)
let _ = Js.log (Js.Float.toExponential 77.1234)

(* prints "7.7e+1" *)
let _ = Js.log (Js.Float.toExponential 77.)
]}

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential> MDN
*)

external toExponentialWithPrecision : float -> digits:int -> string
= "toExponential"
[@@mel.send]
external toExponential : ?digits:int -> string = "toExponential"
[@@mel.send.pipe: t]
(** Formats a [float] using exponential (scientific) notation

{b digits} specifies how many digits should appear after the decimal point. The
Expand All @@ -101,35 +82,16 @@ The output will be rounded or padded with zeroes if necessary.
@raise RangeError if digits is not in the range \[0, 20\] (inclusive)

{[
```
(* prints "7.71e+1" *)
let _ = Js.log (Js.Float.toExponentialWithPrecision 77.1234 ~digits:2)
Js.Float.toExponential 77.1234 = "7.71234e+1"
Js.Float.toExponential 77. = "7.7e+1"
Js.Float.toExponential ~digits:2 77.1234 = "7.71e+1"
]}

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential> MDN
*)

external toFixed : float -> string = "toFixed"
[@@mel.send]
(** Formats a [float] using fixed point notation

{b Returns} a [string] representing the given value in fixed-point notation (usually)

@raise RangeError if digits is not in the range \[0, 20\] (inclusive)

{[
(* prints "12346" (note the rounding) *)
let _ = Js.log (Js.Float.toFixed 12345.6789)

(* print "1.2e+21" *)
let _ = Js.log (Js.Float.toFixed 1.2e21)
]}

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed> MDN
*)

external toFixedWithPrecision : float -> digits:int -> string = "toFixed"
[@@mel.send]
external toFixed : ?digits:int -> string = "toFixed"
[@@mel.send.pipe: t]
(** Formats a [float] using fixed point notation

{b digits} specifies how many digits should appear after the decimal point. The
Expand All @@ -142,43 +104,17 @@ The output will be rounded or padded with zeroes if necessary.
@raise RangeError if digits is not in the range \[0, 20\] (inclusive)

{[
(* prints "12345.7" (note the rounding) *)
let _ = Js.log (Js.Float.toFixedWithPrecision 12345.6789 ~digits:1)

(* prints "0.00" (note the added zeroes) *)
let _ = Js.log (Js.Float.toFixedWithPrecision 0. ~digits:2)
Js.Float.toFixed 12345.6789 = "12346"
Js.Float.toFixed 1.2e21 = "1.2e+21"
Js.Float.toFixed ~digits:1 12345.6789 = "12345.7"
Js.Float.toFixed ~digits:2 0. = "0.00"
]}

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed> MDN
*)

external toPrecision : float -> string = "toPrecision"
[@@mel.send]
(** Formats a [float] using some fairly arbitrary rules

{b Returns} a [string] representing the given value in fixed-point (usually)

[toPrecision] differs from [toFixed] in that the former will format the number
with full precision, while the latter will not output any digits after the
decimal point.

@raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?)

{[
(* prints "12345.6789" *)
let _ = Js.log (Js.Float.toPrecision 12345.6789)

(* print "1.2e+21" *)
let _ = Js.log (Js.Float.toPrecision 1.2e21)
]}

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision> MDN
*)
(* equivalent to `toString` I think *)

external toPrecisionWithPrecision : float -> digits:int -> string
= "toPrecision"
[@@mel.send]
external toPrecision : ?digits:int -> string = "toPrecision"
[@@mel.send.pipe: t]
(** Formats a [float] using some fairly arbitrary rules

{b digits} specifies how many digits should appear in total. The
Expand All @@ -189,41 +125,26 @@ than 20 (for Node it's 21. Why? Who knows).

The output will be rounded or padded with zeroes if necessary.

[toPrecisionWithPrecision] differs from [toFixedWithPrecision] in that the former
[toPrecision] differs from [toFixed] in that the former
will count all digits against the precision, while the latter will count only
the digits after the decimal point. [toPrecisionWithPrecision] will also use
the digits after the decimal point. [toPrecision] will also use
scientific notation if the specified precision is less than the number for digits
before the decimal point.

@raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?)

{[
(* prints "1e+4" *)
let _ = Js.log (Js.Float.toPrecisionWithPrecision 12345.6789 ~digits:1)

(* prints "0.0" *)
let _ = Js.log (Js.Float.toPrecisionWithPrecision 0. ~digits:2)
Js.Float.toPrecision 12345.6789 = "12345.6789"
Js.Float.toPrecision 1.2e21 = "1.2e+21"
Js.Float.toPrecision ~digits:1 12345.6789 = "1e+4"
Js.Float.toPrecision ~digits:2 0. = "0.0"
]}

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision> MDN
*)

external toString : float -> string = "toString"
[@@mel.send]
(** Formats a [float] as a string

{b Returns} a [string] representing the given value in fixed-point (usually)

{[
(* prints "12345.6789" *)
let _ = Js.log (Js.Float.toString 12345.6789)
]}

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString> MDN
*)

external toStringWithRadix : float -> radix:int -> string = "toString"
[@@mel.send]
external toString : ?radix:int -> string = "toString"
[@@mel.send.pipe: t]
(** Formats a [float] as a string

{b radix} specifies the radix base to use for the formatted number. The
Expand All @@ -234,51 +155,30 @@ value must be in the range \[2, 36\] (inclusive).
@raise RangeError if radix is not in the range \[2, 36\] (inclusive)

{[
(* prints "110" *)
let _ = Js.log (Js.Float.toStringWithRadix 6. ~radix:2)

(* prints "11.001000111101011100001010001111010111000010100011111" *)
let _ = Js.log (Js.Float.toStringWithRadix 3.14 ~radix:2)

(* prints "deadbeef" *)
let _ = Js.log (Js.Float.toStringWithRadix 3735928559. ~radix:16)

(* prints "3f.gez4w97ry0a18ymf6qadcxr" *)
let _ = Js.log (Js.Float.toStringWithRadix 123.456 ~radix:36)
Js.Float.toString 12345.6789 = "12345.6789"
Js.Float.toString ~radix:2 6. = "110"
Js.Float.toString ~radix:2 3.14 = "11.001000111101011100001010001111010111000010100011111"
Js.Float.toString ~radix:16 3735928559. = "deadbeef"
Js.Float.toString ~radix:36 123.456 = "3f.gez4w97ry0a18ymf6qadcxr"
]}

@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString> MDN
*)

external fromString : string -> float = "Number"
external fromString : string -> t = "Number"

(** Parses the given [string] into a [float] using JavaScript semantics

{b Returns} the number as a [float] if successfully parsed, [_NaN] otherwise.

{[
(* returns 123 *)
let _ = Js.Float.fromString "123"

(* returns 12.3 *)
let _ = Js.Float.fromString "12.3"

(* returns 0 *)
let _ = Js.Float.fromString ""

(* returns 17 *)
let _ = Js.Float.fromString "0x11"

(* returns 3 *)
let _ = Js.Float.fromString "0b11"

(* returns 9 *)
let _ = Js.Float.fromString "0o11"

(* returns [_NaN] *)
let _ = Js.Float.fromString "foo"

(* returns [_NaN] *)
let _ = Js.Float.fromString "100a"
Js.Float.fromString "123" = 123.
Js.Float.fromString "12.3" = 12.3
Js.Float.fromString "" = 0.
Js.Float.fromString "0x11" = 17.
Js.Float.fromString "0b11" = 3.
Js.Float.fromString "0o11" = 9.
Js.Float.fromString "foo" = _NaN
Js.Float.fromString "100a" = _NaN
]}
*)
16 changes: 8 additions & 8 deletions jscomp/test/dist/jscomp/test/js_float_test.js

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

Loading
Loading