From 8ce0ef3aed1b919eab8ff388141a304f66cf9ed0 Mon Sep 17 00:00:00 2001 From: prostone4 <219053450+prostone4@users.noreply.github.com> Date: Fri, 30 Oct 2026 13:09:04 +0100 Subject: [PATCH] Fix NumbersToExpressions on Lua 5.4 Co-Authored-By: Claude Opus 5.5 --- src/prometheus/steps/NumbersToExpressions.lua | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/prometheus/steps/NumbersToExpressions.lua b/src/prometheus/steps/NumbersToExpressions.lua index e65c669..c66ecc6 100644 --- a/src/prometheus/steps/NumbersToExpressions.lua +++ b/src/prometheus/steps/NumbersToExpressions.lua @@ -54,6 +54,10 @@ local function generateModuloExpression(n) return lhs, rhs end +local function safe(v) + return type(v) == "number" and v == v and v > -2 ^ 53 and v < 2 ^ 53 +end + local function contains(table, value) for _, v in ipairs(table) do if v == value then @@ -68,7 +72,7 @@ function NumbersToExpressions:init(_) function(val, depth) -- Addition local val2 = math.random(-2 ^ 20, 2 ^ 20) local diff = val - val2 - if tonumber(tostring(diff)) + tonumber(tostring(val2)) ~= val then + if not safe(diff) or tonumber(tostring(diff)) + tonumber(tostring(val2)) ~= val then return false end return Ast.AddExpression( @@ -81,7 +85,7 @@ function NumbersToExpressions:init(_) function(val, depth) -- Subtraction local val2 = math.random(-2 ^ 20, 2 ^ 20) local diff = val + val2 - if tonumber(tostring(diff)) - tonumber(tostring(val2)) ~= val then + if not safe(diff) or tonumber(tostring(diff)) - tonumber(tostring(val2)) ~= val then return false end return Ast.SubExpression( @@ -93,7 +97,7 @@ function NumbersToExpressions:init(_) function(val, depth) -- Modulo local lhs, rhs = generateModuloExpression(val) - if tonumber(tostring(lhs)) % tonumber(tostring(rhs)) ~= val then + if not safe(lhs) or not safe(rhs) or rhs == 0 or tonumber(tostring(lhs)) % tonumber(tostring(rhs)) ~= val then return false end return Ast.ModExpression( @@ -148,7 +152,7 @@ function NumbersToExpressions:CreateNumberExpression(val, depth) binary = "0" else while n > 0 do - binary = (n % 2) .. binary + binary = math.floor(n % 2) .. binary n = math.floor(n / 2) end end @@ -162,7 +166,11 @@ function NumbersToExpressions:CreateNumberExpression(val, depth) local exp = math.floor(math.log10(math.abs(val))) local mantissa = val / (10 ^ exp) - return Ast.NumberExpression(string.format("%.15ge%d", mantissa, exp)) + local text = string.format("%.15ge%d", mantissa, exp) + if tonumber(text) ~= val then + return Ast.NumberExpression(val) + end + return Ast.NumberExpression(text) end if format == "normal" then