std/num/decimal▲toc

Arbitrary precision decimal numbers.

Decimals have arbitrary precision and range and do exact decimal arithmetic and are well suited for financial calculations for example.

type decimalstd/num/decimal/decimal: V

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.

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.

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.

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 this an even decimal?

Is the decimal negative?

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"

.

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).

The decimal zero.

type roundstd/num/decimal/round: V

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

fun parse-decimal( s : stringstd/core/types/string: V ) : maybestd/core/types/maybe: V -> V<decimalstd/num/decimal/decimal: V>

Parse a decimalstd/num/decimal/decimal: V number.

private import std/core/typesstd/core/types, std/core/hndstd/core/hnd, std/core/exnstd/core/exn, std/core/boolstd/core/bool, std/core/orderstd/core/order, std/core/charstd/core/char, std/core/intstd/core/int, std/core/vectorstd/core/vector, std/core/stringstd/core/string, std/core/sslicestd/core/sslice, std/core/liststd/core/list, std/core/maybestd/core/maybe, std/core/eitherstd/core/either, std/core/tuplestd/core/tuple, std/core/showstd/core/show, std/core/debugstd/core/debug, std/core/delayedstd/core/delayed, std/core/consolestd/core/console, std/corestd/core, std/text/parsestd/text/parse, std/num/float64std/num/float64