» Import: decimal

The decimal import provides functions for operating on numbers represented as decimals.

Binary floating-point numbers provided by the float type are unable to fully represent numbers like 1.1 and 2.2. The decimal import can represent these numbers exactly, and so should be used when exactness is required.

Decimals are created through the new function, which takes any type that can represent a number. Arguments to the decimal operators can also take a value of any of these types. The full list is:

  • float
  • int
  • string
  • existing decimal value

Decimals are represented internally by a sign, coefficient, and exponent. The value is calculated with the formula sign * coefficient * 10^exponent. Exponent is limited to 32 bits, and the coefficient is limited by the total precision.

The maximum precision a decimal can represent is 100 digits. This includes both before and after the decimal place.

» decimal.infinity(sign)

Creates an infinite-value decimal. Infinite-value decimals are always larger or smaller, depending on sign, than any non-infinite decimals.

decimal.infinity(1)  // +Infinity
decimal.infinity(-1) // -Infinity

Also available as decimal.inf.

» decimal.nan

A decimal representing a "not-a-number" value. NaNs are not equal to any other number, even themselves.

decimal.new(-1).sqrt()               // NaN
decimal.new(0).mul(decimal.inf(1))   // NaN
decimal.nan.is(decimal.nan)          // false

» decimal.isinfinite(d)

Evaluates to true if the decimal is infinite.

decimal.isinfinite(100)          // false
decimal.isinfinite("Infinity")   // true

Also available as decimal.isinf.

» decimal.isnan(d)

Evaluates to true if the decimal is not-a-number.

decimal.isnan("NaN") // true

» decimal.new(v)

Constructs a decimal from another value.

decimal.new("1.1")           // Constructs a decimal from a string.
decimal.new(2.2)             // Constructs a decimal from a float.
decimal.new(3)               // Constructs a decimal from an integer.
decimal.new("1.1234E+400")   // Constructs a decimal from a string using E-notation.

» Type: number

» number.string

A string representation of the decimal.

» number.sign

A number between -1 and +1 representing the sign of the decimal.

» number.coefficient

The coefficient component of the decimal.

» number.exponent

The exponent component of the decimal.

» number.float

A floating-point representation of the decimal.

» number.int

An integer representation of the decimal. This always truncates the decimal to the nearest integer less than or equal to itself.

» number.is(v)

Test for equality with another value.

» number.is_not(v)

Test for inequality with another value.

» number.less_than(v)

Test that the decimal is less than another value. Can also be called as number.lt(v)

» number.less_than_or_equals(v)

Test that the decimal is less than or equals to another value. Can also be called as number.lte(v)

» number.greater_than(v)

Test that the decimal is greater than another value. Can also be called as number.gt(v)

» number.greater_than_or_equals(v)

Test that the decimal is greater than or equals to another value. Can also be called as number.gte(v)

» number.add(v)

Add another number to the decimal.

» number.subtract(v)

Subtract another number from the decimal. Can also be called as number.sub(v)

» number.multiply(v)

Multiply the decimal by a number. Can also be called as number.mul(v)

» number.divide(v)

Divide the decimal by a number. Can also be called as number.div(v)

» number.modulo(v)

Find the remainder after dividing the decimal by a number. Can also be called as mod, remainder, or rem.

» number.power(v)

Raise the decimal to a power. Can also be called as number.pow(v)

» number.exp()

The natural exponent of the decimal. This calculates e^number.

» number.loge()

The natural logarithm of the decimal. Can also be called as number.ln()

» number.log10()

The base-10 logarithm of the decimal. Can also be called as number.log()

» number.square_root()

The square root of the decimal. Can also be called as number.sqrt()

» number.ceiling()

Round the decimal to the smallest integer greater or equal to itself. Can also be called as number.ceil()

» number.floor()

Round the decimal to the largest integer less than or equal to itself.

» number.absolute()

The absolute value of the decimal. Can also be called as number.abs()

» number.negate()

The negated value of the decimal. A positive decimal will become negative, and a negative decimal will become positive. Can also be called as number.neg()