-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
use %.16g for $ float
#1130
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
use %.16g for $ float
#1130
Conversation
|
Have you written a unit test for this/checked whether this patch causes any unit tests to fail? |
|
Unit test yes, checked for failed tests, no. |
|
wont this print garbage for 4 byte floats? |
|
Could someone just translate Ruby's or Python's float formatting implementation instead? |
|
I take it back, %g is fine for float/double |
|
@fowlmouth you sure? @reactormonk showed me some float formatting code from ruby and it looks a lot more complicated than just a %g |
|
The behavior seems identical, they both switch to scientific notation as soon as it would be shorter than normal notation: Python does the same |
|
@reactormonk have you checked to see if this PR causes any current tests to fail? |
|
I found that |
|
mruby's float-to-string https://github.com/mruby/mruby/blob/master/src/numeric.c#L111 |
|
$5.0 really should produce "5.0". Can you port Ruby's solution to Nimrod please? |
|
Perhaps do something ugly for now, like this: proc c_sprintf(buf, frmt: cstring):int {.header: "<stdio.h>",
importc: "sprintf", varargs, noSideEffect.}
proc to_s(f: float): string =
var buf: array [0..64, char]
var n:int = c_sprintf(buf, "%.16g", f)
for i in 0..n-1:
if buf[i] notin {'0'..'9','-'}:
return $buf
buf[n] = '.'
buf[n+1] = '0'
buf[n+2] = '\0'
result = $buf
stdout.writeln(to_s(-0.00001))Custom more flexible formatter would be cool for the whole formatBiggestFloat shebang. EDIT: .0 Should be appended if result of %g consists only of 0..9 and - |
|
@Araq Hm, nope. Got ~500 LoC behind |
|
I like @katlogic 's solution for the time being |
|
I did a bit more extensive solution that should have the same results as @katlogic's code, except it should be more robust in cases like |
|
According to the standard, |
|
Or make it even more horrible: proc c_sprintf(buf, frmt: cstring):int {.header: "<stdio.h>",
importc: "sprintf", varargs, noSideEffect.}
proc c_localeconv():ptr cstring {.header: "<locale.h>",
importc: "localeconv", noSideEffect.}
proc to_s(f: float): string =
var buf: array [0..64, char]
var n = c_sprintf(buf, "%.16g", f)
for i in 0..n-1:
if buf[i] notin {'0'..'9','-'}:
return $buf
buf[n] = c_localeconv()[0]
buf[n+1] = '0'
buf[n+2] = '\0'
result = $buf
stdout.writeln(to_s(0))The comma vs dot is consistent with other places which uses c_sprintf so it's not that bad. |
## Summary On Windows Testament now correctly captures the exit code from commands (compile, test run, etc). ## Details On platforms like Windows closing a process before reading the exit code invalidates the exit code, returning 0, which would falsely signify a success. Instead, use `defer` to make sure that the object is kept alive until the end of scope. --------- Co-authored-by: Saem Ghani <saemghani+github@gmail.com>
This solves the trailing zero problem, but the ultimate solution would be to port ruby/python.