-
Notifications
You must be signed in to change notification settings - Fork 99
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.toText only displays 6 fractional digits - we probably want a representation that would roundtrip as a literal. #88
Comments
Using specifier "%.16g" not "%f" yields better result, but I don't know if that's good enough. rts/float.c
|
@rossberg any advice here? |
Yes, However, in practice, users may not want to see umteen zeroes all the time. We'll have to add some richer formatting functions; toText should probably default to There's also |
Something like |
@ggreif, I think it should be a separate function from toText, though. But then we'd probably want s.th like SML's realfmt type, maybe extended with a In the interpreter that ought to be trivial (using OCaml's Printf). |
@rossberg In OCaml we can't build the format string dynamically, can we? val fmt : string = "%.9g"
# Printf.sprintf fmt 3.14159;;
Error: This expression has type string but an expression was expected of type
('a -> 'b, unit, string) format =
('a -> 'b, unit, string, string, string, string)
CamlinternalFormatBasics.format6 |
Following SML's basis library, here is what I propose: a function
(Making precision non-optional, since that doesn't buy you much in this form of description.) |
@ggreif, no, but a simple switch over the 4 or 5 formats is enough. The precision can be turned into a dynamic argument by using |
That's what I mean by "hairier". |
How is that "hairy"? |
It has more hair :-) |
But we'll still need some supporting primitives for this to fly, right? (I'll come up with a PR in dfinity/motoko soon.) |
Perhaps just extend float_fmt with a mode and a precision arg? |
Great, thanks guys. I'll leave that in your hands then @ggreif. |
I totally misunderstood what |
Now I see why you considered it hairy. :) |
The current `floatToText` primitive is (while being fine for `debug_show`) insufficient for several reasons: - no round-trip - not adjustable precision - only decimal output. dfinity/motoko-base#88 asks for better formatting capabilities. This PR implements the underlying primitive `floatToFormattedText `, while leaving the previous one as-is. Introduces primitive `floatToFormattedText(num : Float, precision : Word8, mode : Word8)` with `mode`: - (0) fixed format `"%.*f"` - (1) exponent format `"%.*e"` - (2) generic format `"%.*g"` - (3) hexadecimal format `"%.*h"`
Precisely. |
Is there any way we can not have |
see rts/float.c
The text was updated successfully, but these errors were encountered: