From 1db43c5030dd488526c47ed46ee5fcbc390ac043 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 28 Jul 2022 17:10:12 +0200
Subject: [PATCH] Bytecode MEX: in error messages, revert back to default
 floating point format

A move to fixed format was erroneously made in
4893f0e82cacae7ed0e9a7ca8d02c21c73cffcf5 and
ff85fc6489492f73b3bb8f079fe8d417e1cd14fb, where stream formatting of floating
points has been replaced by the use of std::to_string().

(partial cherry pick from commit 7c4dc1abce62b5497eb699363f2b9d471484193c)
---
 mex/sources/bytecode/ErrorHandling.hh | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/mex/sources/bytecode/ErrorHandling.hh b/mex/sources/bytecode/ErrorHandling.hh
index 273977ac05..84a8ddbc9b 100644
--- a/mex/sources/bytecode/ErrorHandling.hh
+++ b/mex/sources/bytecode/ErrorHandling.hh
@@ -78,7 +78,10 @@ public:
   LogExceptionHandling(double value_arg) : FloatingPointExceptionHandling("log(X)"),
                                            value(value_arg)
   {
-    completeErrorMsg(" with X=" + to_string(value) + "\n");
+    // We don’t use std::to_string(), because it uses fixed formatting
+    ostringstream s;
+    s << " with X=" << defaultfloat << value << "\n";
+    completeErrorMsg(s.str());
   }
 };
 
@@ -89,7 +92,10 @@ public:
   Log10ExceptionHandling(double value_arg) : FloatingPointExceptionHandling("log10(X)"),
                                              value(value_arg)
   {
-    completeErrorMsg(" with X=" + to_string(value) + "\n");
+    // We don’t use std::to_string(), because it uses fixed formatting
+    ostringstream s;
+    s << " with X=" << defaultfloat << value << "\n";
+    completeErrorMsg(s.str());
   }
 };
 
@@ -101,7 +107,10 @@ public:
                                                                   value1(value1_arg),
                                                                   value2(value2_arg)
   {
-    completeErrorMsg(" with X=" + to_string(value2) + "\n");
+    // We don’t use std::to_string(), because it uses fixed formatting
+    ostringstream s;
+    s << " with X=" << defaultfloat << value2 << "\n";
+    completeErrorMsg(s.str());
   }
 };
 
@@ -113,10 +122,13 @@ public:
                                                                value1(value1_arg),
                                                                value2(value2_arg)
   {
-    if (fabs(value1) > 1e-10)
-      completeErrorMsg(" with X=" + to_string(value1) + "\n");
-    else
-      completeErrorMsg(" with X=" + to_string(value1) + " and a=" + to_string(value2) + "\n");
+    // We don’t use std::to_string(), because it uses fixed formatting
+    ostringstream s;
+    s << " with X=" << defaultfloat << value1;
+    if (fabs(value1) <= 1e-10)
+      s << " and a=" << value2;
+    s << "\n";
+    completeErrorMsg(s.str());
   };
 };
 
-- 
GitLab