|
| 1 | +.. _decimal: |
| 2 | + |
| 3 | +------------------------------------------------------------------------------- |
| 4 | + Module `decimal` |
| 5 | +------------------------------------------------------------------------------- |
| 6 | + |
| 7 | +.. module:: decimal |
| 8 | + |
| 9 | +The ``decimal`` module has functions for working with |
| 10 | +exact numbers. This is important when numbers are large |
| 11 | +or even the slighest inaccuracy is unacceptable. |
| 12 | +For example Lua calculates ``0.16666666666667 * 6`` |
| 13 | +with floating-point so the result is 1. |
| 14 | +But with the decimal module (using ``decimal.new`` |
| 15 | +to convert the number to decimal type) |
| 16 | +``decimal.new('0.16666666666667') * 6`` is 1.00000000000002. |
| 17 | + |
| 18 | +To construct a decimal number, bring in the module with |
| 19 | +``require('decimal')`` and then use ``decimal.new(n)`` |
| 20 | +or any function in the decimal module: |
| 21 | +:ref:`abs(n) <decimal-abs>` |
| 22 | +:ref:`exp(n) <decimal-exp>` |
| 23 | +:ref:`ln(n) <decimal-ln>` |
| 24 | +:ref:`log10(n) <decimal-log10>` |
| 25 | +:ref:`new(n) <decimal-new>` |
| 26 | +:ref:`precision(n) <decimal-precision>` |
| 27 | +:ref:`scale(n) <decimal-scale>` |
| 28 | +:ref:`sqrt(n) <decimal-sqrt>`, |
| 29 | +where n can be a string or a non-decimal number or a decimal number. |
| 30 | +If it is a string or a non-decimal number, |
| 31 | +Tarantool converts it to a decimal number before |
| 32 | +working with it. |
| 33 | +It is best to construct from strings, and to convert |
| 34 | +back to strings after calculations, because Lua numbers |
| 35 | +have only 15 digits of precision. Decimal numbers have |
| 36 | +38 digits of precision, that is, the total number of digits |
| 37 | +before and after the decimal point can be 38. |
| 38 | +Tarantool supports the usual arithmetic and comparison operators |
| 39 | ++ - * / % ^ < > <= >= ~= ==. |
| 40 | +If an operation has both decimal and non-decimal operands, |
| 41 | +then the non-decimal operand is converted to decimal before |
| 42 | +the operation happens. |
| 43 | + |
| 44 | +Use ``tostring(decimal-number)`` to convert back to a string. |
| 45 | + |
| 46 | +A decimal operation will fail if overflow happens (when a |
| 47 | +number is greater than 10^38 - 1 or less than -10^38 - 1). |
| 48 | +A decimal operation will fail if arithmetic is impossible |
| 49 | +(such as division by zero or square root of minus 1). |
| 50 | +A decimal operation will not fail if rounding of |
| 51 | +post-decimal digits is necessary to get 38-digit precision. |
| 52 | + |
| 53 | +.. _decimal-abs: |
| 54 | + |
| 55 | +.. function:: abs(string-or-number-or-decimal) |
| 56 | + |
| 57 | + Returns absolute value of a decimal number. |
| 58 | + For example if a is -1 then ``decimal.abs(a)`` returns 1. |
| 59 | + |
| 60 | +.. _decimal-exp: |
| 61 | + |
| 62 | +.. function:: exp(string-or-number-or-decimal) |
| 63 | + |
| 64 | + Returns *e* raised to the power of a decimal number. |
| 65 | + For example if a is 1 then ``decimal.exp(a)`` returns |
| 66 | + 2.7182818284590452353602874713526624978. |
| 67 | + Compare ``math.exp(1)`` from the |
| 68 | + `Lua math library <https://www.lua.org/pil/18.html>`_, |
| 69 | + which returns 2.718281828459. |
| 70 | + |
| 71 | +.. _decimal-ln: |
| 72 | + |
| 73 | +.. function:: ln(string-or-number-or-decimal) |
| 74 | + |
| 75 | + Returns natural logarithm of a decimal number. |
| 76 | + For example if a is 1 then ``decimal.ln(a)`` returns 0. |
| 77 | + |
| 78 | +.. _decimal-log10: |
| 79 | + |
| 80 | +.. function:: log10(string-or-number-or-decimal) |
| 81 | + |
| 82 | + Returns base-10 logarithm of a decimal number. |
| 83 | + For example if a is 100 then ``decimal.log10(a)`` returns 2. |
| 84 | + |
| 85 | +.. _decimal-new: |
| 86 | + |
| 87 | +.. function:: new(string-or-number-or-decimal) |
| 88 | + |
| 89 | + Returns the value of the input as a decimal number. |
| 90 | + For example if a is 1E-1 then |
| 91 | + ``decimal.new(a)`` returns 0.1. |
| 92 | + |
| 93 | +.. _decimal-precision: |
| 94 | + |
| 95 | +.. function:: precision(string-or-number-or-decimal) |
| 96 | + |
| 97 | + Returns the number of digits in a decimal number. |
| 98 | + For example if a is 123.4560 then ``decimal.precision(a)`` returns 7. |
| 99 | + |
| 100 | +.. _decimal-scale: |
| 101 | + |
| 102 | +.. function:: scale(string-or-number-or-decimal) |
| 103 | + |
| 104 | + Returns the number of post-decimal digits in a decimal number. |
| 105 | + For example if a is 123.4560 then ``decimal.scale(a)`` returns 4. |
| 106 | + |
| 107 | +.. _decimal-sqrt: |
| 108 | + |
| 109 | +.. function:: sqrt(string-or-number-or-decimal) |
| 110 | + |
| 111 | + Returns the square root of a decimal number. |
| 112 | + For example if a is 2 then ``decimal.sqrt(a)`` returns |
| 113 | + 1.4142135623730950488016887242096980786. |
0 commit comments