Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'PrintObj(1.)' #3395

Merged
merged 3 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions lib/ieee754.g
Original file line number Diff line number Diff line change
Expand Up @@ -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));

#############################################################################
##
Expand Down
21 changes: 20 additions & 1 deletion src/macfloat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tst/testinstall/float.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]

#
#
Expand Down
2 changes: 1 addition & 1 deletion tst/testinstall/syntaxtree.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down