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

Remove restrictions on float formatting. #157

Merged
merged 1 commit into from
Dec 17, 2023
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
6 changes: 3 additions & 3 deletions basis/Real.sml
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ structure Real : REAL =
case spec of
SCI NONE => to_string_gen "%e" r
| SCI (SOME n) =>
if n < 0 orelse n > 400 then raise Size
if n < 0 then raise Size
else to_string_gen ("%." ^ Int.toString n ^ "e") r
| FIX NONE => to_string_gen "%f" r
| FIX (SOME n) =>
if n < 0 orelse n > 400 then raise Size
if n < 0 then raise Size
else to_string_gen ("%." ^ Int.toString n ^ "f") r
| GEN NONE => toString r
| GEN (SOME n) =>
if n < 1 orelse n > 400 then raise Size
if n < 1 then raise Size
else mlify (to_string_gen ("%." ^ Int.toString n ^ "g") r)
| EXACT => fmt (SCI (SOME 30)) r
end
Expand Down
16 changes: 8 additions & 8 deletions src/Runtime/Math.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,19 +892,19 @@ REG_POLY_FUN_HDR(stringOfFloat, Region rAddr, size_t arg)
String
REG_POLY_FUN_HDR(generalStringOfFloat, Region rAddr, String format, size_t f)
{
char buf[512];
size_t size = snprintf(NULL, 0, &(format->data), get_d(f)) + 1;
char *buf = malloc(size);
melsman marked this conversation as resolved.
Show resolved Hide resolved
snprintf(buf, size, &(format->data), get_d(f));

/* Unfortunately there seems to be no way to ensure that this does not
* crash by overflowing the result_buffer (e.g. when specifying a huge
* number of decimal digits in the fixed-point format):
*/
sprintf(buf, &(format->data), get_d(f));
mkSMLMinus(buf);
String s;
if ( strcmp (buf, "~nan") == 0 ) {
return REG_POLY_CALL(convertStringToML,rAddr,"nan");
s = REG_POLY_CALL(convertStringToML,rAddr,"nan");
} else {
return REG_POLY_CALL(convertStringToML,rAddr,buf);
s = REG_POLY_CALL(convertStringToML,rAddr,buf);
}
free(buf);
return s;
}

/* DEBUG */
Expand Down
Loading