-
Notifications
You must be signed in to change notification settings - Fork 3
Big Number Datatype
The bignum! datatype is used for arbitrary-precision decimal and non-decimal arithmetic.
No literal format for now.
Use the to-bignum
function or make
to convert string!, integer!, float!, or block! datatypes to a big number:
probe to-bignum "123.45"
123.45
probe to-bignum 123
123
probe to-bignum -123.8
-123.8
probe to-bignum 12.3
12.3
and a base can be specified in a block.
probe to-bignum ["-123" 10]
-123
probe to-bignum ["1011" 2]
11
probe to-bignum ["3E8" 16]
1000
Signed 0, signed Infinity and NaN are supported.
probe to-bignum "1.#INF"
1.#INF
probe to-bignum "1.#NaN"
1.#NaN
probe to-bignum "-0"
0
- Convert to integer. If the bignum value is not in the range of a 32-bit integer, throw out an error.
n: make bignum! 100
i: to-integer n
100
probe type? i
integer!
n: make bignum! "99e200"
i: to-integer n
*** Script Error: integer! overflow/underflow
*** Where: to
*** Stack: to-integer
- Convert to float.
n: make bignum! 10.6
i: to-float n
10.6
n: make bignum! "99e600"
i: to-float n
1.#INF
- Convert to binary. Note that the fraction digits of a bignum value will be omitted.
n: make bignum! 10000
to-binary n
#{2710}
n: make bignum! 10000.123
to-binary n
#{2710}
Use bignum? to determine whether a value is a bignum! datatype.
n: make bignum! 123
print bignum? n
true
Use the form
and print
functions to print a bignum
value in its simplest form:
- integer. If it can be represented as one.
- decimal without exponent. If it's not too big or too small.
- scientific notation. If it's too big or small.
For example,
probe to-bignum 100
100
probe to-bignum 123.4
123.4
probe form to-bignum "9.999e+499"
9.999e+499
Use the mold
function to print a bignum
value in its loadable form:
probe mold to-bignum 123.4
make bignum! 123.4
probe mold to-bignum "9.999e+499"
make bignum! 9.999e+499
probe mold to-bignum [1011 2]
make bignum! 11
The settings for bignum! can be configured using system/options/bignum
object.
probe system/options/bignum
make object! [
decimal-places: 20
rounding-mode: 'half-up
modulo-mode: 'down
exponent-min: -1e9
exponent-max: 1e9
]
The maximum number of decimal places of the results of operations involving division and molding.
Type: integer!
Default value: 20
The rounding mode used in the math operations.
Type: word!
Default value: half-up
Values are accepted:
Value | Description |
---|---|
UP | Rounds away from zero |
DOWN | Rounds towards zero |
CEIL | Rounds towards Infinity |
FLOOR | Rounds towards -Infinity |
HALF_UP | Rounds towards nearest neighbour. If equidistant, rounds away from zero |
HALF_DOWN | Rounds towards nearest neighbour. If equidistant, rounds towards zero |
HALF_EVEN | Rounds towards nearest neighbour. If equidistant, rounds towards even neighbour |
HALF_CEIL | Rounds towards nearest neighbour. If equidistant, rounds towards Infinity |
HALF_FLOOR | Rounds towards nearest neighbour. If equidistant, rounds towards -Infinity |
The modulo mode used when calculating the modulus: a mod n.
The quotient, q = a / n, is calculated according to the ROUNDING-MODE that corresponds to the chosen MODULO-MODE.
The remainder, r, is calculated as: r = a - n * q.
Type: word!
Default value: down
Values are accepted:
Value | Description |
---|---|
UP | The remainder is positive if the dividend is negative, otherwise it is negative. |
DOWN | The remainder has the same sign as the dividend. |
FLOOR | The remainder has the same sign as the divisor. |
HALF_EVEN | The IEEE 754 remainder function. |
EUCLID | The remainder is always positive. Euclidean division: q = sign(n) * floor(a / abs(n)) |
The exponent value(s) beyond which overflow to Infinity and underflow to zero occurs.
- exponent-min: minimum exponent magnitude. values with a negative exponent of greater magnitude become
zero
. - exponent-max: maximum exponent magnitude. Values with a positive exponent of greater magnitude become
Infinity
.
Type: integer!
Default value:
exponent-min: -1e9
exponent-max: 1e9
Examples:
system/options/bignum/exponent-max: 4
make bignum! 99999
99999 ;-- e is 4
make bignum! 100000
1.#INF ;-- e is 5