From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Fri, 21 Jun 2019 18:53:07 +0300 From: Vladimir Davydov Subject: Re: [PATCH 2/2] decimal: expose decimal type to lua. Message-ID: <20190621155307.z6pnpfbgqgni6zud@esperanza> References: <89c41adbdf7452c1ef4a8f478363fe2ccb95a848.1560958964.git.sergepetrenko@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <89c41adbdf7452c1ef4a8f478363fe2ccb95a848.1560958964.git.sergepetrenko@tarantool.org> To: Serge Petrenko Cc: tarantool-patches@freelists.org List-ID: On Wed, Jun 19, 2019 at 06:58:05PM +0300, Serge Petrenko wrote: > Add a decimal library to lua. > > Part of #692 > > @TarantoolBot document > Title: Document decimal module in lua. > > First of all, you have to require the package via > `decimal = require('decimal')` > Now you can construct decimals via `tonumber` 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.tonumber(123e-7) > b = decimal.tonumber('123.456') > c = decimal.tonumber('123.456e2') > d = decimal.tonumber(123ULL) > e = decimal.tonumber(2) > ``` tonumber is a confusing name IMO. Let's rename it to decimal.new() > 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. > When the operation is called as `decimal.opname`, both operands may be > strings or lua numbers, e.g. `decimal.add(23, '123.456') == 146.456`: > ``` Can these functions fail (overflow, underflow)? What happens on error? Please mention in this document. > tarantool> a + b > --- > - '123.456012300000000' > ... > > tarantool> decimal.add(a,b) > --- > - '123.456012300000000' > ... > > tarantool> c - d > --- > - '12222.6' > ... > > tarantool> decimal.sub(c,d) I don't think we need decimal.add/sub/div/mul methods as long as we have corresponding operators. > The following math functions are also supported: > log10, ln, exp, sqrt, pow. When specified as > `decimal.opname()`, operations may be performed on > strings and lua numbers. > ``` > f = decimal.tonumber(100) > tarantool> f:log10() This looks weird. I think we should only allow decimal.log10(x), not x:log10(). > --- > src/CMakeLists.txt | 1 + > src/lua/decimal.c | 327 ++++++++++++++++++++++++++++++++++++++ > src/lua/decimal.h | 39 +++++ > src/lua/init.c | 2 + > test/app/decimal.result | 172 ++++++++++++++++++++ > test/app/decimal.test.lua | 50 ++++++ > 6 files changed, 591 insertions(+) > create mode 100644 src/lua/decimal.c > create mode 100644 src/lua/decimal.h > create mode 100644 test/app/decimal.result > create mode 100644 test/app/decimal.test.lua Please consider implementing this using Lua ffi. Take a look at src/lua/uuid.lua for example. They say that ffi implementation is generally faster than Lua C, because it doesn't break JIT traces. It might be worth benchmarking the two approaches.