Search code examples
luatype-conversionmultiplatform

V-REP's Lua `tonumber` returns nil on Linux


We developed a simple V-REP simulation that works pretty well on OS X but not on Linux (Ubuntu 15.04). Lua is employed as the main scripting language.

We attach the following code (with the return values in comments) that unfortunately returns nil on Linux (but converts e[3] to number without problem on OS X):

e[3]                   -- -0.677782532263
type(e[3])             -- string
type(tonumber(e[3]))   -- nil

What is really interesting is the fact that the previous code works as one would expect in Lua 5.2.3 console (both OS X and Linux). However, V-REP can't convert the string to number properly when running on Linux.

We tried both 32b and 64b V-REP versions (today downloaded) with exactly same results - nils. Could you please point out some things we're missing? Neither Lua nor V-REP are utils we use every day.

Edit 1: I Use Ubuntu 15.04. V-REP uses Lua 5.1, My Lua version:

$ apt-cache policy lua5.1
lua5.1:
  Installed: 5.1.5-7.1
  Candidate: 5.1.5-7.1
  Version table:
 *** 5.1.5-7.1 0
        500 http://cz.archive.ubuntu.com/ubuntu/ vivid/main amd64 Packages
        100 /var/lib/dpkg/status

In console, I tried the following:

$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> e={[3]="-0.677782532263"}; print(e[3], type(e[3]), tonumber(e[3]), type(tonumber(e[3])))
-0.677782532263 string  -0.677782532263 number

Packages


Solution

  • The error is caused by the fact that V-REP uses Lua 5.1, and the computers we tested it on had different locales for numbers (the linux had LC_NUMERIC=cs_CZ.UTF-8 and the mac had, probably, en_US).

    That means that Lua on Mac recognized the float numbers in strings as numbers, but Lua on Linux with the different locale did not - it didn't have a comma (e.g. -3,513) as a decimal separator that the locale required, so it returned nil for the conversion.

    The fix is to set the LC_NUMERIC flag before running the vrep to the en_US locale, like this:

    ...$ LC_NUMERIC=en_US.UTF-8 ./vrep
    

    which would force the locale to be a dot-based, and enable Lua to recognize the numbers.

    Thanks @Etan for all the help and for poking at the issue from the right direction.