[Web GUI] Lua function error : tonumber() fuction returns wrong value

Hi All,
I tried to convert a string to number using tostring() function, but it returns wrong value. Please check below for my input and its results.
Case 1
local variab = "21474836479"
return(tonumber(variab))
Output ==> 21474836479

Case 2
local variab = "21474836480"
return(tonumber(variab))
Output ==> 0

Case 3
local variab = "21474836481"
return(tonumber(variab))
Output ==> 1

Case 4
local variab = "21474836485"
return(tonumber(variab))
Output ==> 5

It seems like value resetting after case 1. Please help me to find a solution for this issue.

Use numbers less than 2^31 x 10...

Looks like the function handles signed 32 bit values, so when you give a number larger than 2^31 x 10 (=21 474 836 480), is rolls over. Strangely it does not give you negative values, but instead subtracts 2^31 x 10 from it and shows the remainder.

Likely a bug, but surfacing due to really large numbers that you want to push to it.

(Ps. the strangest part is the 10x multiplier to 2^31 as the limiter in your example. Straightforward 2^32 or 2^31 would be more understandable.)

1 Like