Wrong rapresentation of Decimal Numbers in Lua

Issue #2824 resolved
alessandro profiti
created an issue

As working on manual_lens_info PR I found an issues when setting aperture value to as submenu with "Choices".

I am unable to set correct values because some values are memorized as approximation and then fail to be found in choices when converting to strings.

Test Script:

val = {1.2,1.4,2.8,4.3,4.5,5.6}

for i = 1, 6 do
  console.write(val[i])
  console.write("\n")
end

console.write(2.8 * 10)

Output:

1.2
1.399999
2.799999
4.3
4.5
5.59999
28

Comments (4)

  1. alessandro profiti reporter

    I started from lua_fix branch, then merged manual_lens_info PR (also tried with vsnprintf) and lua64

    Lua module will fail to compile when using "make zip". I get this when compiling from it's own directory:

    Alessandro:lua alex$ make
    Using ~/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gcc (preferred).
    Updated HGVERSION
    [ README   ]   module_strings.h
    [ CC       ]   lua/ml-lua-shim.o
    In file included from lua/ml-lua-shim.c:9:0:
    dietlibc/include/unistd.h:72:5: warning: conflicting types for built-in function 'execl'
     int execl(const char *path, ...) __THROW;
         ^
    dietlibc/include/unistd.h:73:5: warning: conflicting types for built-in function 'execle'
     int execle(const char *path, ...) __THROW;
         ^
    [ CC       ]   lua/lapi.o
    [ CC       ]   lua/lcode.o
    [ CC       ]   lua/lctype.o
    [ CC       ]   lua/ldebug.o
    [ CC       ]   lua/ldo.o
    [ CC       ]   lua/ldump.o
    [ CC       ]   lua/lfunc.o
    [ CC       ]   lua/lgc.o
    [ CC       ]   lua/llex.o
    [ CC       ]   lua/lmem.o
    [ CC       ]   lua/lobject.o
    [ CC       ]   lua/lopcodes.o
    [ CC       ]   lua/lparser.o
    [ CC       ]   lua/lstate.o
    [ CC       ]   lua/lstring.o
    [ CC       ]   lua/ltable.o
    [ CC       ]   lua/ltm.o
    [ CC       ]   lua/lundump.o
    [ CC       ]   lua/lvm.o
    [ CC       ]   lua/lzio.o
    [ CC       ]   lua/lauxlib.o
    In file included from lua/lauxlib.c:24:0:
    lua/lauxlib.c: In function 'luaL_tolstring':
    lua/lua.h:340:27: warning: implicit conversion from 'lua_Number {aka float}' to 'double' when passing argument to function [-Wdouble-promotion]
     #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
                               ^
    lua/lauxlib.c:755:36: note: in expansion of macro 'lua_tonumber'
               lua_pushfstring(L, "%f", lua_tonumber(L, idx));
                                        ^
    lua/lauxlib.c: In function 'luaL_checkversion_':
    lua/lauxlib.c:970:19: warning: implicit conversion from 'lua_Number {aka float}' to 'double' when passing argument to function [-Wdouble-promotion]
                       ver, *v);
                       ^
    lua/lauxlib.c:970:24: warning: implicit conversion from 'lua_Number {aka const float}' to 'double' when passing argument to function [-Wdouble-promotion]
                       ver, *v);
                            ^
    [ CC       ]   lua/lbaselib.o
    [ CC       ]   lua/lbitlib.o
    [ CC       ]   lua/lcorolib.o
    [ CC       ]   lua/ldblib.o
    [ CC       ]   lua/liolib.o
    In file included from lua/liolib.c:20:0:
    lua/liolib.c: In function 'g_write':
    lua/lua.h:340:27: warning: implicit conversion from 'lua_Number {aka float}' to 'double' when passing argument to function [-Wdouble-promotion]
     #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
                               ^
    lua/liolib.c:611:46: note: in expansion of macro 'lua_tonumber'
                     : fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg));
                                                  ^
    [ CC       ]   lua/lmathlib.o
    lua/lmathlib.c: In function 'math_random':
    lua/lmathlib.c:243:59: warning: implicit conversion from 'float' to 'double' to match other operand of binary expression [-Wdouble-promotion]
       double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
                                                               ^
    lua/lmathlib.c:243:38: warning: implicit conversion from 'float' to 'double' to match other operand of binary expression [-Wdouble-promotion]
       double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
                                          ^
    lua/lmathlib.c:265:27: warning: implicit conversion from 'float' to 'double' to match other operand of binary expression [-Wdouble-promotion]
       r *= (double)(up - low) + 1.0;
                               ^
    [ CC       ]   lua/lstrlib.o
    lua/lstrlib.c: In function 'str_format':
    lua/lstrlib.c:910:44: warning: implicit conversion from 'lua_Number {aka float}' to 'double' when passing argument to function [-Wdouble-promotion]
               nb = snprintf(buff, 65536, form, luaL_checknumber(L, arg));
                                                ^
    [ CC       ]   lua/ltablib.o
    [ CC       ]   lua/lutf8lib.o
    [ CC       ]   lua/loadlib.o
    [ CC       ]   lua/linit.o
    [ CC       ]   lua_globals.o
    [ CC       ]   lua_console.o
    [ CC       ]   lua_camera.o
    [ CC       ]   lua_lv.o
    [ CC       ]   lua_lens.o
    [ CC       ]   lua_movie.o
    [ CC       ]   lua_display.o
    [ CC       ]   lua_key.o
    [ CC       ]   lua_menu.o
    [ CC       ]   lua_dryos.o
    lua_dryos.c: In function 'luaCB_dryos_call':
    lua_dryos.c:169:38: warning: implicit conversion from 'float' to 'double' when passing argument to function [-Wdouble-promotion]
             result = call(function_name, arg);
                                          ^
    [ CC       ]   lua_interval.o
    [ CC       ]   lua_battery.o
    [ CC       ]   lua_task.o
    [ CC       ]   lua_property.o
    [ CC       ]   lua_constants.o
    [ CC       ]   lua.o
    [ AR       ]   fopen.o
    [ AR       ]   dietlibc.a
    [ HGDIFF   ]   hgdiff.tmp
    [ MODULE   ]   lua.mo
    lua/lauxlib.o: In function `panic':
    /Users/alex/Desktop/pullML/Clone/modules/lua/lua/lauxlib.c:949: warning: warning: your code uses stdio (7+k bloat).
    dietlibc.a(vfprintf.o): In function `vfprintf':
    vfprintf.c:(.text+0x34): warning: warning: the printf functions add several kilobytes of bloat.
    [ STRIP    ]   lua.mo
    [ OBJCOPY  ]   lua.mo
    [ RM       ]   hgdiff.tmp
    [ STRIP    ]   lua.sym
    [ STRIP    ]   lua.sym
    [ EXPORTS  ]   lua.sym
    [ DEPENDS  ]   lua.dep
    Will NOT load on: 
        50D (umm_malloc, umm_free_heap_size, umm_free, and 2 others)
    Not checked (compile ML for these cameras first):
        1100D, 500D, 550D, 5D2, 5D3.113, 5D3.123, 600D, 60D, 650D, 6D, 700D, 7D, EOSM
    make: *** [lua.dep] Error 1
    

    lua.mo is created and after copyng I get this when lua module is loaded: Schermata 2017-12-25 alle 20.13.45.png Previously I was getting this when merging also vsnprintf: Schermata 2017-12-25 alle 17.54.30.png But maybe I mixed something so I restarted from aclean repository

  2. Alex

    Lua: fixed issue #2824 - decimal number precision when printing

    thanks dmilligan for the hint and aprofiti for the test code

    tested with:

    val = {1.2,1.4,2.8,4.3,4.5,5.6}
    
    for i = 1, 6 do
      console.write(val[i])
      console.write(" ")
      console.write(val[i] - 0.000001)
      console.write(" ")
      console.write(val[i] + 0.000001)
      console.write("\n")
    end
    

    output:

    1.2 1.199999 1.200001
    1.4 1.399999 1.400001
    2.8 2.799999 2.800001
    4.3 4.299999 4.300001
    4.5 4.499999 4.500001
    5.6 5.599999 5.600001
    

    before:

    1.2 1.199999 1.2
    1.399999 1.399999 1.4
    2.799999 2.799999 2.8
    4.3 4.299999 4.300001
    4.5 4.499999 4.5
    5.599999 5.599998 5.6
    

    → <<cset aa9c7f1b64b2>>

  3. Log in to comment