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..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,6 +71,24 @@ static void PrintMacfloatToBuf(char *buf, size_t bufsize, Double val, int precis } else { snprintf(buf, bufsize, "%.*" PRINTFFORMAT, precision, val); + // 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 523a448772..821dc658bf 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 ] # # 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",