Float
Functions for interacting with float.
t
type t = float
Type representing a float.
equal
let equal: (float, float) => bool
Checks if two floating point numbers are equal.
Examples
RESCRIPTFloat.equal(1.0, 1.0) == true
Float.equal(1.0, 2.0) == false
compare
let compare: (float, float) => Ordering.t
Compares two floating point numbers, returns an Ordering.t
value.
Examples
RESCRIPTFloat.compare(1.0, 1.0) == Ordering.equal
Float.compare(1.0, 2.0) == Ordering.less
Float.compare(2.0, 1.0) == Ordering.greater
isNaN
let isNaN: float => bool
isNaN(v)
tests if the given v
is NaN
.
See isNaN
on MDN.
Examples
RESCRIPTFloat.isNaN(3.0) // false
Float.isNaN(Float.Constants.nan) // true
isFinite
let isFinite: float => bool
isFinite(v)
tests if the given v
is finite.
See isFinite
on MDN.
Examples
RESCRIPTFloat.isFinite(1.0) == true
Float.isFinite(Float.Constants.nan) == false
Float.isFinite(Float.Constants.positiveInfinity) == false
parseFloat
let parseFloat: string => float
parseFloat(v)
parse the given v
and returns a float. Leading whitespace in
v
is ignored. Returns NaN
if v
can't be parsed. Use [fromString
] to
ensure it returns a valid float and not NaN
.
See parseFloat
on MDN.
Examples
RESCRIPTFloat.parseFloat("1.0") == 1.0
Float.parseFloat(" 3.14 ") == 3.14
Float.parseFloat("3.0") == 3.0
Float.parseFloat("3.14some non-digit characters") == 3.14
Float.parseFloat("error")->Float.isNaN == true
parseInt
let parseInt: ('a, ~radix: int=?) => float
parseInt(v, ~radix=?)
parse the given v
and returns a float. Leading
whitespace in this argument v
is ignored. radix
specifies the radix base to
use for the formatted number. The value must be in the range [2, 36] (inclusive).
Returns NaN
if v
can't be parsed and radix
is smaller than 2 or bigger
than 36.
See parseInt
on MDN.
Examples
RESCRIPTFloat.parseInt("1.0") == 1.0
Float.parseInt(" 3.14 ") == 3.0
Float.parseInt(3) == 3.0
Float.parseInt("3.14some non-digit characters") == 3.0
Float.parseInt("error")->Float.isNaN == true
Float.parseInt("10.0", ~radix=2) == 2.0
Float.parseInt("15 * 3", ~radix=10) == 15.0
Float.parseInt("12", ~radix=13) == 15.0
Float.parseInt("17", ~radix=40)->Float.isNaN == true
parseIntWithRadix
Deprecated
let parseIntWithRadix: ('a, ~radix: int) => float
parseIntWithRadix(v, ~radix)
parse the given v
and returns a float. Leading
whitespace in this argument v
is ignored. radix
specifies the radix base to
use for the formatted number. The value must be in the range [2, 36] (inclusive).
Returns NaN
if v
can't be parsed and radix
is smaller than 2 or bigger
than 36.
See parseInt
on MDN.
Examples
RESCRIPTFloat.parseIntWithRadix("10.0", ~radix=2) == 2.0
Float.parseIntWithRadix("15 * 3", ~radix=10) == 15.0
Float.parseIntWithRadix("12", ~radix=13) == 15.0
Float.parseIntWithRadix("17", ~radix=40)->Float.isNaN == true
toExponential
let toExponential: (float, ~digits: int=?) => string
toExponential(v, ~digits=?)
return a string
representing the given value in
exponential notation. digits
specifies how many digits should appear after
the decimal point.
See Number.toExponential
on MDN.
Examples
RESCRIPTFloat.toExponential(1000.0) == "1e+3"
Float.toExponential(-1000.0) == "-1e+3"
Float.toExponential(77.0, ~digits=2) == "7.70e+1"
Float.toExponential(5678.0, ~digits=2) == "5.68e+3"
Exceptions
RangeError
: Ifdigits
less than 0 or greater than 10.
toExponentialWithPrecision
Deprecated
let toExponentialWithPrecision: (float, ~digits: int) => string
toExponential(v, ~digits)
return a string
representing the given value in
exponential notation. digits
specifies how many digits should appear after
the decimal point.
See Number.toExponential
on MDN.
Examples
RESCRIPTFloat.toExponentialWithPrecision(77.0, ~digits=2) == "7.70e+1"
Float.toExponentialWithPrecision(5678.0, ~digits=2) == "5.68e+3"
Exceptions
RangeError
: Ifdigits
less than 0 or greater than 10.
toFixed
let toFixed: (float, ~digits: int=?) => string
toFixed(v, ~digits=?)
return a string
representing the given
value using fixed-point notation. digits
specifies how many digits should
appear after the decimal point.
See Number.toFixed
on MDN.
Examples
RESCRIPTFloat.toFixed(123456.0) == "123456"
Float.toFixed(10.0) == "10"
Float.toFixed(300.0, ~digits=4) == "300.0000"
Float.toFixed(300.0, ~digits=1) == "300.0"
Exceptions
RangeError
: Ifdigits
is less than 0 or larger than 100.
toFixedWithPrecision
Deprecated
let toFixedWithPrecision: (float, ~digits: int) => string
toFixedWithPrecision(v, ~digits)
return a string
representing the given
value using fixed-point notation. digits
specifies how many digits should
appear after the decimal point.
See Number.toFixed
on MDN.
Examples
RESCRIPTFloat.toFixedWithPrecision(300.0, ~digits=4) == "300.0000"
Float.toFixedWithPrecision(300.0, ~digits=1) == "300.0"
Exceptions
RangeError
: Ifdigits
is less than 0 or larger than 100.
toPrecision
let toPrecision: (float, ~digits: int=?) => string
toPrecision(v, ~digits=?)
return a string
representing the giver value with
precision. digits
specifies the number of significant digits.
See Number.toPrecision
on MDN.
Examples
RESCRIPTFloat.toPrecision(100.0) == "100"
Float.toPrecision(1.0) == "1"
Float.toPrecision(100.0, ~digits=2) == "1.0e+2"
Float.toPrecision(1.0, ~digits=1) == "1"
Exceptions
RangeError
: Ifdigits
is not between 1 and 100 (inclusive). Implementations are allowed to support larger and smaller values as well. ECMA-262 only requires a precision of up to 21 significant digits.
toPrecisionWithPrecision
Deprecated
let toPrecisionWithPrecision: (float, ~digits: int) => string
toPrecisionWithPrecision(v, ~digits)
return a string
representing the giver value with
precision. digits
specifies the number of significant digits.
See Number.toPrecision
on MDN.
Examples
RESCRIPTFloat.toPrecisionWithPrecision(100.0, ~digits=2) == "1.0e+2"
Float.toPrecisionWithPrecision(1.0, ~digits=1) == "1"
Exceptions
RangeError
: Ifdigits
is not between 1 and 100 (inclusive). Implementations are allowed to support larger and smaller values as well. ECMA-262 only requires a precision of up to 21 significant digits.
toString
let toString: (float, ~radix: int=?) => string
toString(v)
return a string
representing the given value.
See Number.toString
on MDN.
Examples
RESCRIPTFloat.toString(1000.0) == "1000"
Float.toString(-1000.0) == "-1000"
toStringWithRadix
Deprecated
let toStringWithRadix: (float, ~radix: int) => string
toStringWithRadix(v, ~radix)
return a string
representing the given value.
~radix
specifies the radix base to use for the formatted number.
See Number.toString
on MDN.
Examples
RESCRIPTFloat.toStringWithRadix(6.0, ~radix=2) == "110"
Float.toStringWithRadix(3735928559.0, ~radix=16) == "deadbeef"
Float.toStringWithRadix(123456.0, ~radix=36) == "2n9c"
Exceptions
RangeError
: if radix
is less than 2 or greater than 36.
toLocaleString
let toLocaleString: float => string
toLocaleString(v)
return a string
with language-sensitive representing the
given value.
See Number.toLocaleString
on MDN.
Examples
RESCRIPT// If the application uses English as the default language
Float.toLocaleString(1000.0) // "1,000"
// If the application uses Portuguese Brazil as the default language
Float.toLocaleString(1000.0) // "1.000"
fromString
let fromString: string => option<float>
fromString(str)
return an option<int>
representing the given value str
.
Examples
RESCRIPTFloat.fromString("0") == Some(0.0)
Float.fromString("NaN") == None
Float.fromString("6") == Some(6.0)
toInt
let toInt: float => int
toInt(v)
returns an int to given float v
.
Examples
RESCRIPTFloat.toInt(2.0) == 2
Float.toInt(1.0) == 1
Float.toInt(1.1) == 1
Float.toInt(1.6) == 1
fromInt
let fromInt: int => float
fromInt(v)
returns a float to given int v
.
Examples
RESCRIPTFloat.fromInt(2) == 2.0
Float.fromInt(1) == 1.0
mod
let mod: (float, float) => float
mod(n1, n2)
calculates the modulo (remainder after division) of two floats.
Examples
RESCRIPTFloat.mod(7.0, 4.0) == 3.0
clamp
let clamp: (~min: float=?, ~max: float=?, float) => float
clamp(~min=?, ~max=?, value)
returns value
, optionally bounded by min
and max
.
if max
< min
returns min
.
Examples
RESCRIPTFloat.clamp(4.2) == 4.2
Float.clamp(4.2, ~min=4.3) == 4.3
Float.clamp(4.2, ~max=4.1) == 4.1
Float.clamp(4.2, ~min=4.3, ~max=4.1) == 4.3
ignore
let ignore: float => unit
ignore(float)
ignores the provided float and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.