Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: Serge Petrenko <sergepetrenko@tarantool.org>
Cc: tarantool-patches@freelists.org
Subject: Re: [PATCH 2/2] decimal: expose decimal type to lua.
Date: Fri, 21 Jun 2019 18:53:07 +0300	[thread overview]
Message-ID: <20190621155307.z6pnpfbgqgni6zud@esperanza> (raw)
In-Reply-To: <89c41adbdf7452c1ef4a8f478363fe2ccb95a848.1560958964.git.sergepetrenko@tarantool.org>

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.

  parent reply	other threads:[~2019-06-21 15:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-19 15:58 [PATCH 0/2] decimal: expose decimal module " Serge Petrenko
2019-06-19 15:58 ` [PATCH 1/2] lua/utils: add a function to register FFI metatypes Serge Petrenko
2019-06-19 15:58 ` [PATCH 2/2] decimal: expose decimal type to lua Serge Petrenko
2019-06-20 11:02   ` Serge Petrenko
2019-06-21 15:53   ` Vladimir Davydov [this message]
2019-06-24 14:53     ` [tarantool-patches] " Serge Petrenko
2019-06-26 14:02       ` Vladimir Davydov
2019-06-28 14:39         ` Serge Petrenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190621155307.z6pnpfbgqgni6zud@esperanza \
    --to=vdavydov.dev@gmail.com \
    --cc=sergepetrenko@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [PATCH 2/2] decimal: expose decimal type to lua.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox