Module decimal
The decimal module has functions for working with exact numbers. This
is important when numbers are large or even the slightest inaccuracy is
unacceptable. For example Lua calculates 0.16666666666667 * 6 with
floating-point so the result is 1. But with the decimal module (using
decimal.new to convert the number to decimal type)
decimal.new('0.16666666666667') * 6 is 1.00000000000002.
To construct a decimal number, bring in the module with
require('decimal') and then use decimal.new(n) or any function in
the decimal module:
- abs(n)
- exp(n)
- is_decimal(n)
- ln(n)
- log10(n)
- new(n)
- precision(n)
- rescale(decimal-number, new-scale)
- scale(n)
- sqrt(n)
- trim(decimal-number),
where n can be a string or a non-decimal number or a decimal number. If it is a string or a non-decimal number, Tarantool converts it to a decimal number before working with it. It is best to construct from strings, and to convert back to strings after calculations, because Lua numbers have only 15 digits of precision.
Decimal numbers have N digits of precision, that is, the total number of digits before and after the decimal point can be equal to N. In Tarantool 3.5 the precision was increased from N = 38 to N = 76.
decimal = require('decimal')e = decimal.exp(1)-- In Tarantool version 3.5 and above:decimal.precision(e)---- 76...#tostring(e)---- 77...-- In Tarantool versions before 3.5:decimal.precision(e)---- 38...#tostring(e)---- 39...
Tarantool supports the usual arithmetic and comparison operators + - * / % ^ < > <= >= ~= ==. If an operation has both decimal and non-decimal operands, then the non-decimal operand is converted to decimal before the operation happens.
Use tostring(decimal-number) to convert back to a string.
A decimal operation will fail if overflow happens (when a number is greater than 10^N - 1 or less than -10^N - 1). A decimal operation will fail if arithmetic is impossible (such as division by zero or square root of minus 1). A decimal operation will not fail if rounding of post-decimal digits is necessary to get N-digit precision.
Returns absolute value of a decimal number. For example if a is -1
then decimal.abs(a) returns 1.
Returns e raised to the power of a decimal number. For example if a
is 1 then decimal.exp(a) returns
2.7182818284590452353602874713526624978 for N = 38 or
2.718281828459045235360287471352662497757247093699959574966967627724076630354
for N = 76. Compare math.exp(1) from the Lua math
library, which returns
2.718281828459.
Returns true if the specified value is a decimal, and false
otherwise. For example if a is 123 then decimal.is_decimal(a)
returns false. if a is decimal.new(123) then
decimal.is_decimal(a) returns true.
Returns natural logarithm of a decimal number. For example if a is 1
then decimal.ln(a) returns 0.
Returns base-10 logarithm of a decimal number. For example if a is
100 then decimal.log10(a) returns 2.
Returns the value of the input as a decimal number. For example if a
is 1E-1 then decimal.new(a) returns 0.1.
Returns the number of digits in a decimal number. For example if a is
123.4560 then decimal.precision(a) returns 7.
Returns the number after possible rounding or padding. If the number of
post-decimal digits is greater than new-scale, then rounding occurs. The
rounding rule is: round half away from zero. If the number of
post-decimal digits is less than new-scale, then padding of zeros
occurs. For example if a is -123.4550 then decimal.rescale(a, 2)
returns -123.46, and decimal.rescale(a, 5) returns -123.45500.
Returns the number of post-decimal digits in a decimal number. For
example if a is 123.4560 then decimal.scale(a) returns 4.
Returns the square root of a decimal number. For example if a is 2
then decimal.sqrt(a) returns 1.4142135623730950488016887242096980786
for N = 38 or
1.414213562373095048801688724209698078569671875376948073176679737990732478462
for N = 76.
Returns a decimal number after possible removing of trailing
post-decimal zeros. For example if a is 2.20200 then
decimal.trim(a) returns 2.202.