Skip to content

Commit

Permalink
Make PrintMacfloatToBuf check for e, print . before
Browse files Browse the repository at this point in the history
Add Max' comments
  • Loading branch information
sebasguts authored and Dominik Bernhardt committed Apr 9, 2019
1 parent 5862484 commit 0b2deb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 19 additions & 4 deletions 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,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);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tst/testinstall/float.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0b2deb5

Please sign in to comment.