Skip to content

Lua to Redis integer conversion loses precision #1118

Closed

Description

@ssimeonov

Lua has a single number type that follows the IEEE 754 binary64 specification, which allows for 52 bits of precision. This makes the continuous range of positive integers that can be represented without loss of precision [1, (2^53) - 1].

When converting numbers from Lua to Redis, Redis uses default stringification which outputs in scientific notation with limited precision. This has the effect of losing precision for large integers, violating the Redis promise in the scripting documentation:

This conversion between data types is designed in a way that if a Redis type is converted into a Lua type, and then the result is converted back into a Redis type, the result is the same as of the initial value.

Because Redis has to stringify numbers in decimal notation, some precision loss for non-integer floating point numbers is to be expected. (The scripting documentation should probably be updated to point this out.) However, losing precision for integers that can be represented exactly in IEEE 754 is a bug that can have significant impact and can be very difficult to track down. The larger the integers, the larger the precision loss.

To reproduce, put the following in an Rspec suite:

it 'demonstrates Lua to Redis conversion precision loss' do
  redis = Redis.new

  val = 1.0 * ((2**53) - 1)

  lua = <<-EOS
      local value = (2^53) - 1
      local hashData = {
              "defaultPrecision",
              value,
              "extendedPrecision",
              string.format("%.0f", value)
          }
      redis.call("HMSET", KEYS[1], unpack(hashData))
      return redis.call("HGETALL", KEYS[1])
  EOS

  result = redis.eval(lua, keys: %w("test:key"))

  result[2].should == 'extendedPrecision'
  result[3].to_f.should == val

  result[0].should == 'defaultPrecision'
  result[1].to_f.should == val
end

This will generate the following HMSET command:

1369104793.981944 [0 lua] "HMSET" "test:key" "defaultPrecision" "9.007199254741e+15" "extendedPrecision" "9007199254740991"

It will fail with the following message:

     Failure/Error: result[1].to_f.should == val
       expected: 9007199254740991.0
            got: 9007199254741000.0 (using ==)

The suggested fix is to check whether a number is an integer and, in that case, stringify using full precision as opposed to scientific notation.

Activity

  1. josiahcarlson commented on May 21, 2013

    @josiahcarlson

    Lua's numbers are IEE 754 floating point doubles. Which means only exact precision for integers up to +/- 2**53. This is expected behavior.

  2. ssimeonov commented on May 21, 2013

    @ssimeonov
    Author

    @josiahcarlson I am confused. You and I are saying the same thing yet you think this is not a bug?

    If you read the spec, it shows loss of precision for 2^53 - 1 (within the range that IEEE 754 specifies) by default and it also shows how the loss of precision can be avoided by actually writing out all the digits of the number represented in Lua.

    Further, there are many more integers outside this range which can be represented precisely (but not in a continuous range) in IEEE 754 which will not be correctly passed from Lua to Redis.

  3. josiahcarlson commented on May 21, 2013

    @josiahcarlson

    I misread late at night. You are right, the format specifier should be updated. For some reason, I thought that maybe you were expecting integers outside the +/- 2**53 range to be exactly represented in Lua.

    My mistake, you're right :)

  4. kryogenic commented on Jun 3, 2014

    @kryogenic

    I have this issue too.
    Storing a very small or very large floating point number (ex. 1.1710299640683E-305) as a score in a redis zset results in a loss of precision when the float is converted to a string.

  5. antirez commented on Jun 4, 2014

    @antirez
    Contributor

    Hello, checking this issue right now, report ASAP.

  6. mattsta commented on Jun 4, 2014

    @mattsta
    Contributor

    The default lua number-to-string conversion routine called by lua_tolstring() is using %.14g which is too short for 9007199254740991.

    Looks like we need to modify the conversion from a one-line lua_tolstring() to do:

    if (lua_isnumber(lua, -1))
      custom format a string from double returned by lua_tonumber(lua, -1)
    else
       use lua_tolstring()
  7. antirez commented on Jun 4, 2014

    @antirez
    Contributor

    Exactly Matt, that's my fix:

    diff --git a/src/scripting.c b/src/scripting.c
    index 4b485d9..2a7f1b9 100644
    --- a/src/scripting.c
    +++ b/src/scripting.c
    @@ -233,12 +233,22 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error
             char *obj_s;
             size_t obj_len;
    
    -        obj_s = (char*)lua_tolstring(lua,j+1,&obj_len);
    -        if (obj_s == NULL) break; /* Not a string. */
    +        if (lua_isnumber(lua,j+1)) {
    +            /* We can't use lua_tolstring() for number -> string conversion
    +             * since Lua uses a format specifier that loses precision. */
    +            char dbuf[64];
    +            lua_Number num = lua_tonumber(lua,j+1);
    +
    +            obj_len = snprintf(dbuf,sizeof(dbuf),"%.17g",(double)num);
    +            obj_s = dbuf;
    +        } else {
    +            obj_s = (char*)lua_tolstring(lua,j+1,&obj_len);
    +            if (obj_s == NULL) break; /* Not a string. */
    +        }
    
             /* Try to use a cached object. */
    -        if (j < LUA_CMD_OBJCACHE_SIZE && cached_objects[j] &&
    -            cached_objects_len[j] >= obj_len)
    +        if (j < LUA_CMD_OBJCACHE_SIZE && cached_objects[j] &&
    +            cached_objects_len[j] >= obj_len)
             {
                 char *s = cached_objects[j]->ptr;
                 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));

    Do you people like it? The OP's test passes now.

  8. mattsta commented on Jun 4, 2014

    @mattsta
    Contributor

    Looks good! Though, it may be unsafe to keep a reference to dbuf[64] in obj_s after we leave the defining scope.

  9. antirez commented on Jun 4, 2014

    @antirez
    Contributor

    Thanks @mattsta, yep, the reference will no longer be valid, but exactly like with lua_tostring that is subject to the Lua GC. The code path will use the value only in the context of the code that follows, that never calls Lua nor rewrites the dbuf buffer.

  10. mattsta commented on Jun 4, 2014

    @mattsta
    Contributor

    Oh, I meant we're saving the pointer to dbuf after it goes out of scope. :)

  11. antirez commented on Jun 4, 2014

    @antirez
    Contributor

    @mattsta oh, right! That's bad. Fixing, thank you. I'm not 100% sure of ANSI C guarantees or not that a stack allocated thing is in scope as long as the function did not returned. Should be only a matter of visibility AFAIK but I'm not 100% sure so better to fix.

  12. 19 remaining items

  13. antirez commented on Jun 11, 2014

    @antirez
    Contributor

    I finally read the thread, isn't the following a more straightforward fix?

    diff --git a/src/scripting.c b/src/scripting.c
    index 2796477..c968adb 100644
    --- a/src/scripting.c
    +++ b/src/scripting.c
    @@ -234,7 +234,7 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error)
             size_t obj_len;
             char dbuf[64];
    
    -        if (lua_isnumber(lua,j+1)) {
    +        if (lua_type(lua,j+1) == LUA_TNUMBER) {
                 /* We can't use lua_tolstring() for number -> string conversion
                  * since Lua uses a format specifier that loses precision. */
                 lua_Number num = lua_tonumber(lua,j+1);
  14. mattsta commented on Jun 11, 2014

    @mattsta
    Contributor

    Yeah, that looks even better. It removes any possibility of converting strings into numbers, so we don't need to check for string types anymore.

  15. antirez commented on Jun 11, 2014

    @antirez
    Contributor

    Exactly, I don't think there are any non-string-type -> number conversion rules in Lua, so probably the code is functionally equivalent, however the smaller patch is more clear in the intent, that is, enter the branch only if it's really a number.

    Matt, I'm using the test from your PR, what is the best way to change stuff like that? I may:

    • Apply your PR, commit this change to simplify it.
    • Apply your PR locally, amend it adding my comment in the commit message where I say I modified it a bit.
    • Apply my change, apply your PR, resolve conflicts by just taking the test part.
    • Any other idea.

    Please teach some Gittetiquette ;-)

  16. antirez commented on Jun 11, 2014

    @antirez
    Contributor

    Ok just read Matt is going to bed (the power of Twitter), I'll handle this. Thanks!

  17. added 2 commits that reference this issue on Jun 11, 2014
  18. added a commit that references this issue on Jun 11, 2014
  19. added a commit that references this issue on Jun 11, 2014
  20. antirez commented on Jun 11, 2014

    @antirez
    Contributor

    Fix applied.

    Rant: Lua API is not perfect in this regard. The lack of a true integer type distinct from float was already a pain point, shared with Javascript, but given the "we break old stuff" philosophy of Lua (that I like) it's shame it was not fixed at some point much earlier. However even with this type system, the API could be much better, without explicit conversions if not via specially named APIs like lua_is_convertible_to_number or alike, but really, an API called is_number should not do automatic coercion.

  21. ignacio commented on Jun 11, 2014

    @ignacio

    Indeed, automatic coercion should go away, although, to be fair, the manual indicates it. There is some talk of taking the upcoming 5.3 release as an oportunity to remove it (the manual says it may be removed ).
    Regarding, the lack of an integer type, Lua 5.3 will finally have 64 bit integers, but script authors will have to watch out for some surprises :)

    Lua 5.3.0 (work2)  Copyright (C) 1994-2014 Lua.org, PUC-Rio
    > 2+"3"
    5.0
    
  22. added a commit that references this issue on Jun 21, 2014
  23. added a commit that references this issue on Jun 21, 2014
  24. bybera commented on Apr 28, 2018

    @bybera

    Hello, i am working on windows machine. And redis version 3.2.100 for windows latest pre release version. And I have same problem. Can you help me?

  25. szmcdull commented on Sep 13, 2023

    @szmcdull

    The latest Lua can handle 64 bit integer. Like it can detect a number is of integer or float. So tonumber(999999999999999999) return the exact number (18 of 9s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions