std/num/decimal▲toc

Operations on arbitrary precision decimal numbers.

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.

Multiply two decimals with full precision.

Divide two decimals using 15 digits of extra precision.

The absolute value of a decimal.

Round a decimalstd/num/decimal/decimal: V to the smallest integer that is not less than x.

Create a decimal from an integer i with an optional exponent exp (=0) such that the result equals i×10exp.

Create a decimal from a doublestd/core/double: V with a specified maximal precision (=-1). Use a negative maximal precision to create a decimal that precisely represents the doublestd/core/double: V. Note: creating a decimalstd/num/decimal/decimal: V from a doublestd/core/double: V may lose precision and give surprising results as many decimal fractions cannot be represented precisely by a doublestd/core/double: V. Also, decimal(i,exp) 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

.

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

.

Convert a decimal to a doublestd/core/double: V. This may lose precision.

Is this an even decimal?

The exponent of a decimal if displayed in scientific notation.
11.2e-1.decimal.exponent?std/num/decimal/exponent?: (d : decimal) -> int == 0.

Return the ‘floored’ fraction, always in the range [0,1.0). x.floor + x.ffraction == x.

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.

Is the decimal negative?

Is this an odd decimal?

Is the decimal positive?

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"

.

The sign of a decimal number.

Take the sum of a list of decimal numbers (0 for the empty list).

The decimal zero.

Is this 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.

con Ceiling

Round to the minimum integer that is larger or equal.

con Floor

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 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-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 Truncate constructor of the roundstd/num/decimal/round: V type.