You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, you have to require the package via decimal = require('decimal')
Now you can construct decimals via new method.
Decimals may be constructed from lua numbers, strings, unsigned and
signed 64 bit integers.
Decimal is a fixed-point type with maximum 38 digits of precision. All
the calculations are exact, so, be careful when constructing decimals
from lua numbers: they may hold only 15 decimal digits of precision.
You are advised to construct decimals from strings, since strings
represent decimals exactly, and vice versa.
a = decimal.new(123e-7)
b = decimal.new('123.456')
c = decimal.new('123.456e2')
d = decimal.new(123ULL)
e = decimal.new(2)
The allowed operations are addition, subtraction, division,
multiplication and power. If at least one of the operands is decimal,
decimal operations are performed. The other operand may be either
decimal or string, containing a number representation, or a lua number.
Operations only fail on an overflow, i.e. when result exceeds 10^38 - 1.
This includes division by zero. In these cases an error Operation failed is raised.
Underflow is also possible, when precision needed to store the exact
result exceeds 38 digits. Underflow is not an error. When an underflow
happens, the result is rounded to 38 digits of precision.
a = decimal.new(123e-7)
b = decimal.new('123.456')
c = decimal.new('123.456e2')
d = decimal.new(123ULL)
e = decimal.new(2)
tarantool> a + b
---
- '123.456012300000000'
...
tarantool> c - d
---
- '12222.6'
...
tarantool> c / b
---
- '100'
...
tarantool> d * d
---
- '15129'
...
tarantool> d ^ 2
---
- '15129'
...
tarantool> 2 ^ d
---
- '10633823966279326983230456482242756608'...
tarantool> e ^ d
---
- '10633823966279326983230456482242756608'
...
The following math functions are also supported:
log10, ln, exp, sqrt. When specified as decimal.opname(), operations may be performed on
strings and lua numbers.
There are also abs and tostring methods, and an unary minus
operator, which are pretty self-explanatory.
tarantool> a = decimal.new('-5')
---
...
tarantool> a
---
- '-5'
...
tarantool> decimal.abs(a)
---
- '5'
...
tarantool> -a
---
- '5'
...
tostring(a)
---
- '-5'
...
decimal.precision, decimal.scale and decimal.round :
The first two methods return precision, i.e. decimal digits in
number representation, and scale, i.e. decimal digits after the decimal
point in the number representation. decimal.round rounds the number to the given scale.
Comparsions: >, <, >=, <=, == are also legal and work as
expected. You may compare decimals with lua numbers or strings. In that
case comparsion will happen after the values are converted to decimal
type.
Requested by @sergepetrenko in tarantool/tarantool@3ab387a.
The text was updated successfully, but these errors were encountered:
First of all, you have to require the package via
decimal = require('decimal')
Now you can construct decimals via
new
method.Decimals may be constructed from lua numbers, strings, unsigned and
signed 64 bit integers.
Decimal is a fixed-point type with maximum 38 digits of precision. All
the calculations are exact, so, be careful when constructing decimals
from lua numbers: they may hold only 15 decimal digits of precision.
You are advised to construct decimals from strings, since strings
represent decimals exactly, and vice versa.
The allowed operations are addition, subtraction, division,
multiplication and power. If at least one of the operands is decimal,
decimal operations are performed. The other operand may be either
decimal or string, containing a number representation, or a lua number.
Operations only fail on an overflow, i.e. when result exceeds 10^38 - 1.
This includes division by zero. In these cases an error
Operation failed
is raised.Underflow is also possible, when precision needed to store the exact
result exceeds 38 digits. Underflow is not an error. When an underflow
happens, the result is rounded to 38 digits of precision.
The following math functions are also supported:
log10, ln, exp, sqrt. When specified as
decimal.opname()
, operations may be performed onstrings and lua numbers.
There are also
abs
andtostring
methods, and an unary minusoperator, which are pretty self-explanatory.
decimal.precision
,decimal.scale
anddecimal.round
:The first two methods return precision, i.e. decimal digits in
number representation, and scale, i.e. decimal digits after the decimal
point in the number representation.
decimal.round
rounds the number to the given scale.Comparsions:
>
,<
,>=
,<=
,==
are also legal and work asexpected. You may compare decimals with lua numbers or strings. In that
case comparsion will happen after the values are converted to decimal
type.
Requested by @sergepetrenko in tarantool/tarantool@3ab387a.
The text was updated successfully, but these errors were encountered: