std/float
Functions for Float, the type of finite IEEE 754 binary64 numbers.
A Float operation whose result is not finite stops the program
(§11.3), so a Float is never infinite or NaN. a /. 0.0 is 0.0;
divide gives Error(Nil) for a zero divisor instead. Functions here
that could give a result that is not finite, such as power, give
Error(Nil) in its place, so they never stop the program.
parse
Reads a Float: an optional - or +, one or more digits, ., one or
more digits, and then an optional exponent: e or E, an optional -
or +, and one or more digits. The result is the nearest Float, with
ties to even, worked out with exact integers. Text that is not in this
form, or a number too big for a Float, gives Error(Nil). A number too
small for a Float gives 0.0 with its sign.
float.parse("1.5") // Ok(1.5)
float.parse("-2.0e-3") // Ok(-0.002)
float.parse("1") // Error(Nil) parse = (text : String) -> Result(Float, Nil) { to_string
The text form of the Float (§11.8), the same as core.inspect: the
shortest digits that read back as the same number, with .0 or an
exponent where needed.
float.to_string(2.5) // "2.5"
float.to_string(1.0e20) // "1.0e20" to_string = (x : Float) -> String { core.inspect(x) } compare
Compares two Floats. 0.0 and -0.0 are Equal.
compare = (a : Float, with : Float) -> Order { loosely_compare
Compares two Floats, and gives Equal when they are no more than
tolerating apart.
loosely_compare = (a : Float, with : Float, tolerating : Float) -> Order { loosely_equals
True when the two Floats are no more than tolerating apart.
float.loosely_equals(0.1 +. 0.2, 0.3, 1.0e-9) // True loosely_equals = (a : Float, with : Float, tolerating : Float) -> Bool { min
The smaller of two Floats.
min = (a : Float, b : Float) -> Float { max
The greater of two Floats.
max = (a : Float, b : Float) -> Float { clamp
The Float, but not less than min and not greater than max. When
min is greater than max, the two bounds are swapped.
clamp = (x : Float, min : Float, max : Float) -> Float { abs
The Float without its sign.
abs = (x : Float) -> Float { { x <. 0.0 } |> (True) { -.x } | (False) { x } } negate
The Float with the other sign.
negate = (x : Float) -> Float { -.x } ceiling
The smallest whole number that is not less than x.
ceiling = (x : Float) -> Float { core.ceiling(x) } floor
The greatest whole number that is not greater than x.
floor = (x : Float) -> Float { core.floor(x) } round
The nearest whole number, as an Int. A half goes away from zero.
float.round(2.5) // 3
float.round(-2.5) // -3 round = (x : Float) -> Int { truncate
The Float without its fraction, as an Int, rounded toward zero.
truncate = (x : Float) -> Int { core.truncate(x) } to_precision
The Float rounded to precision digits after the point. A negative
precision rounds to tens, hundreds, and so on. The result is the
nearest Float to the rounded number, and so it may not be exact.
float.to_precision(2.43434348473, 2) // 2.43
float.to_precision(547890.453444, -3) // 548000.0 to_precision = (x : Float, precision : Int) -> Float { power
base to the power of a whole number. A negative exponent gives the
inverse: power(2.0, -1) is 0.5. 0.0 with a negative exponent, and a
result too big for a Float, give Error(Nil). The result is within one
unit in the last place of the exact power, and exact when the exact power
is a Float.
float.power(2.0, 10) // Ok(1024.0)
float.power(-.2.0, -1) // Ok(-.0.5) power = (base : Float, of : Int) -> Result(Float, Nil) { real_power
base to the power of, for any Float exponent. A negative base with
an exponent that is not a whole number, 0.0 with a negative exponent,
and a result too big for a Float give Error(Nil). The result is within
one unit in the last place of the exact power.
float.real_power(16.0, 0.5) // Ok(4.0)
float.real_power(-.8.0, 1.0 /. 3.0) // Error(Nil) real_power = (base : Float, of : Float) -> Result(Float, Nil) { exponential
e to the power x. A result too big for a Float gives Error(Nil);
that is for x greater than about 709.78.
float.exponential(1.0) // Ok(2.718281828459045) exponential = (x : Float) -> Result(Float, Nil) { logarithm
The natural logarithm (base e). 0.0 and negative numbers give
Error(Nil).
float.logarithm(1.0) // Ok(0.0)
float.logarithm(0.0) // Error(Nil) logarithm = (x : Float) -> Result(Float, Nil) { square_root
The square root. A negative number gives Error(Nil).
float.square_root(4.0) // Ok(2.0)
float.square_root(-1.0) // Error(Nil) square_root = (x : Float) -> Result(Float, Nil) { sum
The sum of the Floats. The sum of no Floats is 0.0. A sum that is not
finite stops the program.
sum = (numbers : List(Float)) -> Float { product
The product of the Floats. The product of no Floats is 1.0. A product
that is not finite stops the program.
product = (numbers : List(Float)) -> Float { divide
a /. by, or Error(Nil) when by is 0.0.
divide = (a : Float, by : Float) -> Result(Float, Nil) { modulo
The remainder of a divided by by, rounded down, with the sign of
by, or Error(Nil) when by is 0.0.
float.modulo(13.3, 3.3) // Ok(0.10000000000000053) modulo = (a : Float, by : Float) -> Result(Float, Nil) { add
a +. b, as a function.
add = (a : Float, b : Float) -> Float { a +. b } subtract
a -. b, as a function.
subtract = (a : Float, b : Float) -> Float { a -. b } multiply
a *. b, as a function.
multiply = (a : Float, b : Float) -> Float { a *. b } is_integer
True when the Float is a whole number.
is_integer = (x : Float) -> Bool { core.floor(x) == x } max_finite
The greatest finite Float, about 1.8e308.
max_finite = 1.7976931348623157e308 min_positive
The smallest Float greater than 0.0: 5.0e-324, a subnormal number.
min_positive = 5.0e-324 pi
The nearest binary64 value to pi.
pi = 3.141592653589793 tau
The nearest binary64 value to two pi.
tau = 6.283185307179586 sin
Sine of an angle in radians.
sin = (x : Float) -> Float { cos
Cosine of an angle in radians.
cos = (x : Float) -> Float { tan
Tangent of an angle in radians.
tan = (x : Float) -> Result(Float, Nil) { sign_bit
Whether the sign bit is set, including for negative zero.
sign_bit = (x : Float) -> Bool { core.float_to_bits(x) >= sign_mask } copy_sign
Copy the sign bit of sign to magnitude.
copy_sign = (magnitude : Float, sign : Float) -> Float { asin
The arcsine in radians; inputs outside [-1, 1] give Error(Nil).
asin = (x : Float) -> Result(Float, Nil) { acos
The arccosine in radians; inputs outside [-1, 1] give Error(Nil).
acos = (x : Float) -> Result(Float, Nil) { atan
Arctangent in radians.
atan = (x : Float) -> Float { atan2
The angle of (x, y), in radians, with the sign of y.
atan2 = (y : Float, x : Float) -> Float { hypot
The Euclidean length of (x, y), returning an error on overflow.
hypot = (x : Float, y : Float) -> Result(Float, Nil) { radians
Convert degrees to radians.
radians = (degrees : Float) -> Float { degrees *. 0.017453292519943295 } degrees
Convert radians to degrees, returning an error if the result overflows.
degrees = (radians : Float) -> Result(Float, Nil) {