From 1d97bea6cb4358ca3961dc40beb1831b6857ae3d Mon Sep 17 00:00:00 2001 From: Dominik Bernhardt Date: Sat, 6 Apr 2019 11:24:09 +0100 Subject: [PATCH 1/3] PrintMacfloatToBuf now adds a "." at the end. Deletes old hack DOTTIFY --- lib/ieee754.g | 18 ++---------------- src/macfloat.c | 4 ++++ 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/lib/ieee754.g b/lib/ieee754.g index ae508e8d89..e3e9b4967b 100644 --- a/lib/ieee754.g +++ b/lib/ieee754.g @@ -42,25 +42,11 @@ INSTALLFLOATCREATOR("for IsIEEE754FloatRep and string", return MACFLOAT_STRING(s); end); -############################################################################# -## -#M String( x ) . . . . . . . . . . . . . . . . . . . . . . . . for macfloats -## -BindGlobal("MACFLOAT_STRING_DOTTIFY", function(s) - local p; - if '.' in s or Intersection(s,"0123456789")=[] then return s; fi; - for p in [1..Length(s)] do - if not s[p] in "+-0123456789" then p := p-1; break; fi; - od; - Add(s,'.',p+1); - return s; -end); - InstallMethod( String, "for macfloats", [ IsIEEE754FloatRep ], - f->MACFLOAT_STRING_DOTTIFY(STRING_DIGITS_MACFLOAT(FLOAT.DECIMAL_DIG,f))); + f->STRING_DIGITS_MACFLOAT(FLOAT.DECIMAL_DIG,f)); InstallMethod( ViewString, "for macfloats", [ IsIEEE754FloatRep ], - f->MACFLOAT_STRING_DOTTIFY(STRING_DIGITS_MACFLOAT(FLOAT.VIEW_DIG,f))); + f->STRING_DIGITS_MACFLOAT(FLOAT.VIEW_DIG,f)); ############################################################################# ## diff --git a/src/macfloat.c b/src/macfloat.c index bb88c308e5..e63ff0fe42 100644 --- a/src/macfloat.c +++ b/src/macfloat.c @@ -70,6 +70,10 @@ static void PrintMacfloatToBuf(char *buf, size_t bufsize, Double val, int precis } else { snprintf(buf, bufsize, "%.*" PRINTFFORMAT, precision, val); + if(!strchr(buf, '.')) + { + strcat(buf, "."); + } } } From 58624841c01af5fd18bb2f716857abebecca048b Mon Sep 17 00:00:00 2001 From: Dominik Bernhardt Date: Mon, 8 Apr 2019 13:31:57 +0200 Subject: [PATCH 2/3] Adjust tests for PrintObj --- tst/testinstall/float.tst | 8 ++++---- tst/testinstall/syntaxtree.tst | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tst/testinstall/float.tst b/tst/testinstall/float.tst index 523a448772..c968e33c14 100644 --- a/tst/testinstall/float.tst +++ b/tst/testinstall/float.tst @@ -130,9 +130,9 @@ gap> l := [ 0.0, -0.0, 1.0, Sqrt(2.0), posinf, neginf, nan ]; gap> ViewObj(l); Print("\n"); [ 0., -0., 1., 1.41421, inf, -inf, nan ] gap> PrintObj(l); Print("\n"); -[ 0, -0, 1, 1.414213562373095, inf, -inf, nan ] +[ 0., -0., 1., 1.414213562373095, inf, -inf, nan ] gap> Display(l); -[ 0, -0, 1, 1.414213562373095, inf, -inf, nan ] +[ 0., -0., 1., 1.414213562373095, inf, -inf, nan ] # # @@ -179,12 +179,12 @@ gap> 1.5e10; gap> -1.5e0; -1.5 gap> 0.7e-10; -7.e-11 +7e-11. gap> -0.8e-0; -0.8 gap> 1000000000000000000000000000000000000000000000000000000000000000\ > 00000000000000000000000000000000000000000000000000000000000000.0; -1.e+125 +1e+125. gap> 1.5+1; 2.5 gap> last-1.6; diff --git a/tst/testinstall/syntaxtree.tst b/tst/testinstall/syntaxtree.tst index 9f29ba3242..fb8f728a64 100644 --- a/tst/testinstall/syntaxtree.tst +++ b/tst/testinstall/syntaxtree.tst @@ -2855,7 +2855,7 @@ rec( statements := [ rec( obj := rec( type := "T_FLOAT_EXPR_EAGER", - value := 1 ), + value := 1. ), type := "T_RETURN_OBJ" ) ], type := "T_SEQ_STAT" ), type := "T_FUNC_EXPR", From 0b2deb59a9508a6236eec49c123d91ef3bd0733e Mon Sep 17 00:00:00 2001 From: Sebastian Gutsche Date: Tue, 9 Apr 2019 10:33:28 +0200 Subject: [PATCH 3/3] Make PrintMacfloatToBuf check for `e`, print `.` before Add Max' comments --- src/macfloat.c | 23 +++++++++++++++++++---- tst/testinstall/float.tst | 4 ++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/macfloat.c b/src/macfloat.c index e63ff0fe42..60abfb089b 100644 --- a/src/macfloat.c +++ b/src/macfloat.c @@ -55,7 +55,8 @@ static Obj TypeMacfloat(Obj val) // helper function for printing a "decimal" representation of a macfloat // into a buffer. -static void PrintMacfloatToBuf(char *buf, size_t bufsize, Double val, int precision) +static void +PrintMacfloatToBuf(char * buf, size_t bufsize, Double val, int precision) { // handle printing of NaN and infinities ourselves, to ensure // they are printed uniformly across all platforms @@ -70,9 +71,23 @@ static void PrintMacfloatToBuf(char *buf, size_t bufsize, Double val, int precis } else { snprintf(buf, bufsize, "%.*" PRINTFFORMAT, precision, val); - if(!strchr(buf, '.')) - { - strcat(buf, "."); + // check if a period is in the output; this is not always the case, + // e.g. if the value is an integer + if (strchr(buf, '.')) + return; // everything is fine + // we need to insert a '.'; either at the end, or before an exponent + // (e.g. "7e10" -> "7.e10"). For this we need 1 extra byte of storage, + // plus of course 1 byte for the string terminator; check if the + // buffer is big enough + if (strlen(buf) + 2 <= bufsize) { + char * loc = strchr(buf, 'e'); + if (loc) { + memmove(loc + 1, loc, strlen(loc) + 1); + loc[0] = '.'; + } + else { + strxcat(buf, ".", bufsize); + } } } } diff --git a/tst/testinstall/float.tst b/tst/testinstall/float.tst index c968e33c14..821dc658bf 100644 --- a/tst/testinstall/float.tst +++ b/tst/testinstall/float.tst @@ -179,12 +179,12 @@ gap> 1.5e10; gap> -1.5e0; -1.5 gap> 0.7e-10; -7e-11. +7.e-11 gap> -0.8e-0; -0.8 gap> 1000000000000000000000000000000000000000000000000000000000000000\ > 00000000000000000000000000000000000000000000000000000000000000.0; -1e+125. +1.e+125 gap> 1.5+1; 2.5 gap> last-1.6;