-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
float to string to float roundtrip should be possible for every float value #7717
Comments
No. Where do we claim that? |
You can't use |
--> max precision ever required is 32. |
I was hoping to raise your interest in the matter of a lossless float->string->float roundtrip. I evidently failed completely... If you should change your opinion, here you would find an easy entry point to the relevant discussions and available implementations: |
No, not at all, you reported a bug where there is none and I closed it. Lossless roundtrips are important and we might need some better support for it, but |
I'd consider this a bug as well. In many other ecosystems, the standard string representation of a float is not just "some string representation", but one that allows to get back the literal used in the code, which makes a lot of sense. What adds to the confusion in Nim is that currently the string representation looks like it might be full precision, but in fact seems to just mess up the least significant digit. This can be very confusing: let x = 117.63331139400016
let y = 117.63331139400017
echo x, " < ", y, " = ", x < y Output:
|
It may be confusing, but it's fundamentally a problem of how floats work. |
@disruptek I can't really see the fundamental problem. The number |
@bluenote10 Since then we changed |
Maybe it's time for ryu. |
3 options:
so std/ryu would be instead imported by std/prettyprint I suggest starting with option 3 since that's actually needed for either option 1 or option 2 |
I believe the current blocker is that @Araq wants minimal growth of binaries due to the tables of constants that Ryu uses. I think the smaller tables would work for the purposes of default Ryu support/inclusion, or perhaps we could determine whether Ryu is actually used at compile-time and omit the constants otherwise. This should be something we get "for free" with my IC branch, because it has crude (but effective) tree-shaking. It's hardly tested, but I'm optimistic, anyway... |
that's a (potential) blocker for option 1 but not for option 2 nor option 3 though; and option 1 kind of needs 1 or 2 to happen first anyways; so the idea would be that default |
I'm not blocking anything, I'm happy to take what you have and maybe put it under a |
actually we can nicely solve the dependency issue as follows, so that ryu will work for c,js,vm,nimscript, and be usable from dollars.nim without causing any cyclic dependency issues: # in dollars.nim:
proc ryu*(result: var string, a: float) {.magic: "Ryu".}
proc `$`*(a: float): string = ryu(result, a)
# in vmops.nim:
register: dollars.ryu
# in ryu.nim
# here you can add more heavy dependencies as needed
proc ryuImpl(result: var string a: float) {.compilerproc.} = ...
# in cgen:
mRyu dispatches to `ryuImpl`
# in jsgen:
ditto
the mechanism is already in place in the compiler, no need to add any custom logic; |
* refs #7717 roundtrip float to string * make parseFloat more correct * improve float tests * improve float tests * cleanup
…im-lang#18248) * refs nim-lang#7717 roundtrip float to string * make parseFloat more correct * improve float tests * improve float tests * cleanup
should always return true, but the following test gives false for all example values on the c backend (
nim c -r test
) and for some on the js backend (nim js -r test
) but javascript is actually able to deal correctly with the float -> string -> float roundtrip (nim js -d:nodejs -r test
):I tried to wrap the famous dtoa.c by David Gay, but c2nim just drives me crazy. Hopefully somebody with better wrapping abilities, will try to add dtoa and strtod to Nim. And if someone has some time to spare, a replacement of the buggy
nimParseBiggestFloat
in lib/system/jssys.nim with a native javascript version would also be very welcome.The text was updated successfully, but these errors were encountered: