From 0fb06f0524c8b5747e7acc15b73cf31abb434398 Mon Sep 17 00:00:00 2001 From: David Wang Date: Wed, 16 Jul 2025 20:48:41 -0400 Subject: [PATCH] Improve Mac/Windows consistency in parsing very small and large floats represented in scientific-notation. --- lib/lexer.lxx | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/lexer.lxx b/lib/lexer.lxx index cdaeb7551..86b2b8fb1 100644 --- a/lib/lexer.lxx +++ b/lib/lexer.lxx @@ -16,6 +16,8 @@ %top{ #include +#include +#include } %{ @@ -101,9 +103,28 @@ namespace MiniZinc { } bool strtofloatval(const char* s, double& v) { - std::istringstream iss(s); - iss >> v; - return !iss.fail(); + char* endptr; + errno = 0; + v = std::strtod(s, &endptr); + + // Check if the entire string was consumed + if (*endptr != '\0') { + return false; + } + + // Check for overflow or underflow + if (errno == ERANGE) { + // ERANGE can indicate overflow to infinity or underflow to zero + // Both are valid floating point representations, so we accept them + return true; + } + + // Any other errno value indicates an error + if (errno != 0) { + return false; + } + + return true; } void clearBuffer(void* parm) {