Skip to content

Commit

Permalink
Issue yuin#472: tonumber does not handle E notation correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
shieliang.poh committed Jan 3, 2024
1 parent 2348fd0 commit c225c40
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions _lua5.1-tests/math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and
tonumber'.' == nil)
assert(tonumber('-12') == -10-2)
assert(tonumber('-1.2e2') == - - -120)
assert(tonumber('2e2') == 200)
assert(tonumber('2e2', 15) == 662)
assert(f(tonumber('1 a')) == nil)
assert(f(tonumber('e1')) == nil)
assert(f(tonumber('e 1')) == nil)
Expand Down
2 changes: 1 addition & 1 deletion baselib.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func baseToNumber(L *LState) int {
L.Push(lv)
case LString:
str := strings.Trim(string(lv), " \n\t")
if strings.Index(str, ".") > -1 {
if base == 10 && strings.ContainsAny(str, ".eE") {
if v, err := strconv.ParseFloat(str, LNumberBit); err != nil {
L.Push(LNil)
} else {
Expand Down

0 comments on commit c225c40

Please sign in to comment.