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
decimal.infinity(sign)
Creates an infinite-value decimal. Infinite-value decimals are always larger or smaller, depending on sign, than any non-infinite decimals.
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.is_infinite(d)
Evaluates to true if the decimal is infinite.
Also available as decimal.is_inf
, decimal.isinf
and decimal.isinfinite
.
decimal.is_nan(d)
Evaluates to true if the decimal is not-a-number.
Also available as decimal.isnan
.
decimal.new(v)
Constructs a decimal from another value.
Type: number
Numbers 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 number can represent is 100 digits. This includes both before and after the decimal place.
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 rounds down to the nearest integer.
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()