From ee2516df7da3b1da09000e57b10908ded5caa7ef Mon Sep 17 00:00:00 2001
From: Gabor Szalai <gabor.szalai@ericsson.com>
Date: Mon, 17 Aug 2020 08:49:11 +0200
Subject: [PATCH] ttcn2str correction (bug 566118 & 566029)

The number of the significant digits used during the code generation
is increased to 17

The string representation of the float generated by the ttcn2str is
corrected: no leading zero in exponent, 17 significant digits

Change-Id: Ib49b878a78c87c1a8fee55b06c9b713023e90797
Signed-off-by: Gabor Szalai <gabor.szalai@ericsson.com>
---
 compiler2/Real.cc |  4 ++--
 core/Float.cc     | 24 ++++++++++++++++++++----
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/compiler2/Real.cc b/compiler2/Real.cc
index e6eb1c1c9..292412f2b 100644
--- a/compiler2/Real.cc
+++ b/compiler2/Real.cc
@@ -48,7 +48,7 @@ namespace Common {
     double integral = floor(fraction);
     char   tmp[64];
 
-    sprintf(tmp, "%s%.15g%se%d",
+    sprintf(tmp, "%s%.17g%se%d",
             (sign == -1) ? "-" : "",
             fraction,
             (fraction == integral) ? ".0" : "",
@@ -68,7 +68,7 @@ namespace Common {
       Real exponent = floor(log10(rabs));
       Real mantissa = rabs * pow(10.0, -exponent);
 
-      char *tmp = mprintf("%s%.15g", r < 0.0 ? "-" : "", mantissa);
+      char *tmp = mprintf("%s%.17g", r < 0.0 ? "-" : "", mantissa);
       if (floor(mantissa) == mantissa) tmp = mputstr(tmp, ".0");
       if (exponent != 0.0) tmp = mputprintf(tmp, "e%d", (int)exponent);
       string ret_val(tmp);
diff --git a/core/Float.cc b/core/Float.cc
index 73998f077..d7723dcfb 100644
--- a/core/Float.cc
+++ b/core/Float.cc
@@ -68,12 +68,28 @@ static inline void log_float(double float_val)
   else if(float_val!=float_val)
     TTCN_Logger::log_event_str("not_a_number");
   else {
-    boolean f = (float_val > -MAX_DECIMAL_FLOAT && float_val <= -MIN_DECIMAL_FLOAT)
-      || (float_val >= MIN_DECIMAL_FLOAT && float_val <   MAX_DECIMAL_FLOAT)
-      || (float_val == 0.0);
     const char* loc = setlocale(LC_ALL, NULL);
     setlocale(LC_NUMERIC, "C"); // use default locale for displaying numbers
-    TTCN_Logger::log_event(f ? "%f" : "%e", float_val);
+    if(TTCN_Logger::get_log_format() == TTCN_Logger::LF_TTCN && float_val != 0.0){
+      // ttcn2str, use the TTCN-3 standard format and print enough 
+      // digits so when scanning the string back, we get the original floating point.
+      // for 64bit double 17 digit is needed
+      // The 0.0 is habdled by the normal logging case. The %f prints the 0.0 or -0.0 correctly
+      double rabs = fabs(float_val);
+      double exponent = floor(log10(rabs));
+      double mantissa = rabs * pow(10.0, -exponent);
+
+      TTCN_Logger::log_event("%s%.17g", float_val < 0.0 ? "-" : "", mantissa);
+      if (floor(mantissa) == mantissa) TTCN_Logger::log_event( ".0");
+      if (exponent != 0.0) TTCN_Logger::log_event("e%d", (int)exponent);
+      
+    } else {
+      // Normal logging
+      boolean f = (float_val > -MAX_DECIMAL_FLOAT && float_val <= -MIN_DECIMAL_FLOAT)
+        || (float_val >= MIN_DECIMAL_FLOAT && float_val <   MAX_DECIMAL_FLOAT)
+        || (float_val == 0.0);
+      TTCN_Logger::log_event(f ? "%f" : "%e", float_val);
+    }
     setlocale(LC_NUMERIC, loc);
   }
 }
-- 
GitLab