From c225c40e97b1a3b5d4cfe4a679d0cde2ce9c6e70 Mon Sep 17 00:00:00 2001 From: "shieliang.poh" Date: Wed, 3 Jan 2024 16:53:33 +0800 Subject: [PATCH] Issue #472: tonumber does not handle E notation correctly --- _lua5.1-tests/math.lua | 2 ++ baselib.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/_lua5.1-tests/math.lua b/_lua5.1-tests/math.lua index 21429a6f..efdf83df 100644 --- a/_lua5.1-tests/math.lua +++ b/_lua5.1-tests/math.lua @@ -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) diff --git a/baselib.go b/baselib.go index aa2c08a9..97e2630a 100644 --- a/baselib.go +++ b/baselib.go @@ -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 {