MantisBT - Cheat Engine
View Issue Details
0000328Cheat Enginepublic2014-05-01 01:542015-04-30 04:53
Reporterflarn2006 
Assigned ToDark Byte 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionfixed 
PlatformOSOS Version
Summary0000328: Lua tonumber() function not returning negative numbers in some cases
DescriptionFor some reason, when using the tonumber() function on a string containing a negative number, starting with "-0.", it returns the absolute value of that number. This happens with any negative number -1 < x < 0, and only when the zero is given before the decimal point in the string.

To reproduce:

Open Lua Engine window, and type: print(tonumber("-0.028"))

Then click Execute. It will say "0.028", while it should say "-0.028".
TagsNo tags attached.
Attached Files

Notes
(0000675)
Dark Byte   
2014-05-03 14:04   
(Last edited: 2014-05-03 14:06)
Confirmed. The problem is that this is lua which is an external library. There is a new version, but it's missing some of the patches I need (large number support)

But for now you can 'fix' the problem by always executing this script (it could be improved to deal with annoying users that do ----0.12 or add spaces in front, but you should get the idea)

---
if original_tonumber==nil then
  original_tonumber=tonumber
end

function tonumber(value)
  local result=original_tonumber(value)
  if string.sub(s,1,1)=="-" then
    if result>0 then
      result=-result
    end
  end

  return result
end
--

place it in main.lua, or in the autorun folder, or just in the start of your tables/trainers if they use it

(0000676)
mgr_inz_Player   
2014-05-05 15:03   
function tonumber2(value)
  value = value:gsub( "%-0*%." , "-." ):gsub("%-%-","")
  return tonumber(value)
end

test1 = "-0.234"
test2 = "-000.234"
test3 = "---0.234"
test4 = "-----0000.234"
test5 = "------0000.234" -- positive value. ( -1^6 )

print( tonumber2(test1) )
print( tonumber2(test2) )
print( tonumber2(test3) )
print( tonumber2(test4) )
print( tonumber2(test5) )



output:
-0.234
-0.234
-0.234
-0.234
0.234
(0000677)
flarn2006   
2014-05-05 15:04   
@Dark Byte: That's pretty much what I did, but thanks! :-)
(0000816)
Dark Byte   
2015-04-30 04:53   
fixed now that ce is using Lua 5.3

Issue History
2014-05-01 01:54flarn2006New Issue
2014-05-03 14:04Dark ByteNote Added: 0000675
2014-05-03 14:04Dark ByteStatusnew => confirmed
2014-05-03 14:06Dark ByteNote Edited: 0000675
2014-05-05 15:03mgr_inz_PlayerNote Added: 0000676
2014-05-05 15:04flarn2006Note Added: 0000677
2015-04-30 04:53Dark ByteNote Added: 0000816
2015-04-30 04:53Dark ByteStatusconfirmed => resolved
2015-04-30 04:53Dark ByteResolutionopen => fixed
2015-04-30 04:53Dark ByteAssigned To => Dark Byte
2016-06-05 15:18JptnucIssue cloned: 0000487