Module /fusion/number
Operations on numbers (int, decimal, and float).
Exported Bindings
(* num ...)
Returns the product of the numbers, which must be int or decimal. With no
arguments, returns integer 1.
(+ num ...)
Returns the sum of the numbers, which must be int or decimal. With no
arguments, returns integer 0.
(- num ...+)
With two or more int or decimal numbers, returns their difference,
associating to the left. With one int or decimal argument, returns its
negation.
(/ dividend divisor)
Returns a decimal whose numeric value is (dividend / divisor). Both
arguments must be decimals. An exception is thrown if the result cannot be
represented exactly.
(< a b)
Returns true if a is less than b.
Numbers are compared without regard to precision or negative zeros; if the
values have different concrete types they are both coerced to decimal.
Timestamps are compared to each other without regard to precision or local
offset. Annotations are ignored.
Warning: The behavior of this procedure is undefined for values +inf,
-inf, and nan.
See issue #64.
(<= a b)
Returns true if a is less than or equal to b.
Numbers are compared without regard to precision or negative zeros; if the
values have different concrete types they are both coerced to decimal.
Timestamps are compared to each other without regard to precision or local
offset. Annotations are ignored.
Warning: The behavior of this procedure is undefined for values +inf,
-inf, and nan.
See issue #64.
(= left right)
Returns true if the arguments are equivalent in shape and content, possibly by coercing values to a common type. Annotations and precision are ignored.
- Any value is
=to itself. - Nulls of any type are
=to each other. - Bools are
=in the obvious way. - Numbers are
=when they represent the same numeric value, without regard to precision or negative zeros. When a float is compared to an int or decimal, it is coerced to a decimal. - Timestamps are
=when they represent the same point-in-time, without regard to precision or local offset. - Strings and symbols are
=(interchangably) when they contain the same sequence of code points. - Blobs and clobs are
=(interchangably) when they contain the same sequence of bytes. - Pairs are
=when their heads and tails are=. - Sequences are
=when they have the same size, and elements at the same index are=. - Structs are
=when they have the same set of field names, and each name maps to a set of elements that are=. - Void is
=only to itself. - Exceptions are not thrown due to mismatched types; instead the result is
false.
For example:
(= null (quote a::null)) --> true
(= null null.clob) --> true
(= 1 1.00) --> true
(= 0 -0e-3) --> true
(= 2014T 2014-01-01T02:00+02:00) --> true
(= 2014T 2014) --> false
(= "text" (quote text)) --> true
(= "text" (quote a::"text")) --> true
(= [1, 2] (sexp 1 2.00)) --> true
(= null.list []) --> false
(= (struct "f" 1) (mutable_struct "f" 1.0)) --> true
(= {f:1, f:1} {f:1}) --> false
At present, the coercion of float to decimal is precise, without rounding to approximate a prettier decimal form. This may lead to strange behavior since most decimal numbers don't have a precise binary representation:
(= 1.2 1.2e0) --> false
(= 1.5 1.5e0) --> true
It's possible this may change in a future release.
Warning: The behavior of this procedure is undefined for values +inf,
-inf, and nan.
See issue #64.
(> a b)
Returns true if a is greater than b.
Numbers are compared without regard to precision or negative zeros; if the
values have different concrete types they are both coerced to decimal.
Timestamps are compared to each other without regard to precision or local
offset. Annotations are ignored.
Warning: The behavior of this procedure is undefined for values +inf,
-inf, and nan.
See issue #64.
(>= a b)
Returns true if a is greater than or equal to b.
Numbers are compared without regard to precision or negative zeros; if the
values have different concrete types they are both coerced to decimal.
Timestamps are compared to each other without regard to precision or local
offset. Annotations are ignored.
Warning: The behavior of this procedure is undefined for values +inf,
-inf, and nan.
See issue #64.
(ceiling number)
Returns the smallest int greater than or equal to number (that is, truncate
toward positive infinity). The input must be a non-null int or decimal, and
the result is an int.
(decimal coefficient)
(decimal coefficient exponent)
Returns the decimal value from the coefficient and optional exponent.
The coefficient must be a number.
The exponent, if supplied, must be a non-null int.
Returns null.decimal when coefficient is null.
Examples:
(decimal null.float) // null.decimal
(decimal 1234e-2) // 12.34
(decimal 1.21e1 0) // 12.1
(decimal 4.2 1) // 42.
(decimal 1234 -2) // 12.34
(floor number)
Returns the largest int less than or equal to number (that is, truncate
toward negative infinity). The input must be a non-null int or decimal, and the
result is an int.
(int_to_string int)
Converts an int to a string. Returns null.string when given null.int.
(is_decimal value)
Determines whether a value is of type decimal, returning true or false.
(is_float value)
Determines whether a value is of type float, returning true or false.
(is_int value)
Determines whether a value is of type int, returning true or false.
(random)
(random max)
Returns a random float value between 0.0 (inclusive) and 1.0 (exclusive) when
no additional arguments are provided. When max is provided, it must be a
positive integer, and the result is a random number between 0 (inclusive)
and max (exclusive).
Examples:
(random) // Returns a decimal value: 0.0 <= value < 1.0
(random 45) // Returns an int value: 0 <= value < 45
(string_to_int string)
Converts a string to an int. The string must contain an optional minus sign
("-" aka "\x2D") followed by one or more ASCII digits. Returns
null.int when given null.string. An exception is raised if the string is
of invalid format.