Type of a decimal number. Decimals have arbitrary precision and range and do exact decimal arithmetic and are well suited for financial calculations for example.
Create a decimal from a float64std/core/types/float64: V
with a specified maximal precision (=-1
).
Use a negative maximal precision to create a decimal that precisely represents the float64std/core/types/float64: V
.
Note: creating a decimalstd/num/decimal/decimal: V
from a float64std/core/types/float64: V
may lose precision and give surprising results as many decimal
fractions cannot be represented precisely by a float64std/core/types/float64: V
.
Also, decimal(i,expstd/num/float64/exp: (p : float64) -> float64)
is more efficient and better when when exact representations
are required. For example:
> decimal(1.1) 1.100000000000000088817841970012523233890533447265625 > decimal(1.1,17) 1.10000000000000008 > decimal(11,-1) 1.1 > decimal(1.1) 1.100000000000000088817841970012523233890533447265625 > decimal(1.1,17) 1.10000000000000008 > decimal(11,-1) 1.1
.
Create a decimal from an integer i
with an optional
exponent expstd/num/float64/exp: (p : float64) -> float64
(=0
) such that the result equals i
×10expstd/num/float64/exp: (p : float64) -> float64
.
Multiply two decimals with full precision.
Add two decimals.
Subtract two decimals.
Divide two decimals using 15 digits of extra precision.
Negate a decimal.
The absolute value of a decimal.
Round a decimalstd/num/decimal/decimal: V
to the smallest integer that is not less than x
.
Compare decimals.
Decrement a decimal.
Divide two decimals with a given extra precision min-prec
(=15
).
The min-prec
is the number of extra digits used to calculate inexact
divisions.
Note: the division uses up to min-prec
precision using Floor
rounding
for the last digit if the result is inexact. To round differently, you can
for example divide with larger precision and use round-to-prec
.
> div( decimal(2), decimal(3), 0 ) 0 > div( decimal(2), decimal(3), 1 ) 0.6 > div( decimal(2), decimal(3) ) // default precision is 15 0.6666666666666666 > div( decimal(2), decimal(3) ).round-to-prec(6) 0.666667 > divstd/num/decimal/div: (x : decimal, y : decimal, min-prec : ? int) -> decimal( decimal(2), decimal(3), 0 ) 0 > divstd/num/decimal/div: (x : decimal, y : decimal, min-prec : ? int) -> decimal( decimal(2), decimal(3), 1 ) 0.6 > divstd/num/decimal/div: (x : decimal, y : decimal, min-prec : ? int) -> decimal( decimal(2), decimal(3) ) // default precision is 15 0.6666666666666666 > divstd/num/decimal/div: (x : decimal, y : decimal, min-prec : ? int) -> decimal( decimal(2), decimal(3) ).round-to-prec(6) 0.666667
.
The exponent of a decimal if displayed in scientific notation.
11.2e-1.decimal.exponentstd/num/decimal/exponent: (d : decimal) -> int == 0
.
Return the ‘floored’ fraction, always in the range [0
,1.0
).
x.floor + x.ffraction == x
.
Convert a decimal to a float64std/core/types/float64: V
. This may lose precision.
Round a decimalstd/num/decimal/decimal: V
using to the largest integer that is not larger than x
.
Return the ‘truncated’ fraction, always in the range (-1.0
,1.0
).
x.truncate + x.fraction == x
.
Increment a decimal.
Round a decimalstd/num/decimal/decimal: V
number to an integer an optional rounding mode rnd
(=Half-evenstd/num/decimal/Half-even: round
).
Is this an even decimal?
Is the decimal negative?
Is this an odd decimal?
Is the decimal positive?
Is this decimal zero?
The maximum of x
and y
.
The minimum of x
and y
.
Decimal to the power of n
.
Choose an exponent that minimizes memory usage.
Round the decimal-point number x
to
to a specified number of digits behind the dot prec
(=0
) with an optional
rounding mode rnd
(=Half-evenstd/num/decimal/Half-even: round
). The precision can be negative.
decimal(1,485).round-to-prec(2).show == "1.48"
decimal(112,49).round-to-prec(-1).show == "110"
.
Show a decimal d
with a given precision prec
(=-1000
).
The precision specifies the number of digits after the dot (in either scientific of fixed-point notation).
If the precision is negative, at most prec
digits are displayed, while for a positive
precision, exactly prec
digits behind the dot are displayed.
This uses show-fixed
when the exponent of d
in scientific notation is larger than -5
and smaller than the precision (or 15 in case of a negative precision), otherwise it uses show-exp
.
Show a decimal d
with a given precision prec
(=-1000
) in scientific notation.
The precision specifies the number of digits after the dot, i.e.
the number of significant digits is prec+1
.
If the precision is negative, at most prec
digits are displayed, and if
it is positive exactly prec
digits are used.
> decimal(1,-1).show-exp "1e-1" > 1.1.decimal.show-exp "1.100000000000000088817841970012523233890533447265625" > 1.1.decimal.show-exp(-20) "1.10000000000000008882" > 0.125.decimal.show-exp(-20) "1.25e-1" > 0.125.decimal.show-exp(20) "1.25000000000000000000e-1" > decimal(1,-1).show-exp "1e-1" > 1.1.decimal.show-exp "1.100000000000000088817841970012523233890533447265625" > 1.1.decimal.show-exp(-20) "1.10000000000000008882" > 0.125.decimal.show-exp(-20) "1.25e-1" > 0.125.decimal.show-exp(20) "1.25000000000000000000e-1"
.
Show a decimal d
with a given precision prec
(=-1000
) in fixed-point notation.
The precision specifies the number of digits after the dot.
If the precision is negative, at most prec
digits after the dot are displayed,
while for a positive precision, exactly prec
digits are used.
> decimal(1,-1).show-fixed "0.1" > 0.1.decimal.show-fixed "0.1000000000000000055511151231257827021181583404541015625" > 0.1.decimal.show-fixed(20) "0.1000000000000000555" > 0.1.decimal.show-fixed(-20) "0.1000000000000000555" > decimal(1,-1).show-fixed(20) "0.1000000000000000000" > decimal(1,-1).show-fixed "0.1" > 0.1.decimal.show-fixed "0.1000000000000000055511151231257827021181583404541015625" > 0.1.decimal.show-fixed(20) "0.1000000000000000555" > 0.1.decimal.show-fixed(-20) "0.1000000000000000555" > decimal(1,-1).show-fixed(20) "0.1000000000000000000"
.
Show a decimal d
using its internal representation.
The sign of a decimal number.
Take the sum of a list of decimal numbers (0 for the empty list).
Truncate a decimalstd/num/decimal/decimal: V
to an integer by rounding towards zero.
The decimal zero.
Rounding modes.
Round to the nearest integer away from zero, i.e. toward negative infinity for negative numbers, and positive infinity for positive numbers.
Round to the minimum integer that is larger or equal.
Round to the maximum integer that is lower or equal.
Round to nearest integer, round away from zero in case of a tie.
Round to nearest integer, round towards infinity in case of a tie.
Round to neareast integer, round to the even number in case of a tie.
Round to nearest integer, round towards negative infinity in case of a tie.
Round to nearest integer, round towards zero in case of a tie.
Round to the nearest integer towards zero (i.e. truncate).
Automatically generated. Tests for the Away-from-zerostd/num/decimal/Away-from-zero: round
constructor of the roundstd/num/decimal/round: V
type.
Automatically generated. Tests for the Ceiling
constructor of the roundstd/num/decimal/round: V
type.
Automatically generated. Tests for the Floor
constructor of the roundstd/num/decimal/round: V
type.
Automatically generated. Tests for the Half-away-from-zerostd/num/decimal/Half-away-from-zero: round
constructor of the roundstd/num/decimal/round: V
type.
Automatically generated. Tests for the Half-ceilingstd/num/decimal/Half-ceiling: round
constructor of the roundstd/num/decimal/round: V
type.
Automatically generated. Tests for the Half-evenstd/num/decimal/Half-even: round
constructor of the roundstd/num/decimal/round: V
type.
Automatically generated. Tests for the Half-floorstd/num/decimal/Half-floor: round
constructor of the roundstd/num/decimal/round: V
type.
Automatically generated. Tests for the Half-truncatestd/num/decimal/Half-truncate: round
constructor of the roundstd/num/decimal/round: V
type.
Automatically generated. Tests for the Truncate
constructor of the roundstd/num/decimal/round: V
type.
Round a decimalstd/num/decimal/decimal: V
number to a whole number with an optional rounding mode (=Half-evenstd/num/decimal/Half-even: round
).
Parse a decimalstd/num/decimal/decimal: V
number.
Arbitrary precision decimal numbers.
Decimals have arbitrary precision and range and do exact decimal arithmetic and are well suited for financial calculations for example.