-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
math/big: (*Float).Text always rounds #50548
Comments
I think this is working as intended, and is essentially
@griesemer might also have opinions on this. |
*big.Float supports many operations with various rounding options and This issue could be regarded as a request for a specification change if the existing behaviour is intended. If intended, the documentation should mention how rouding issues is treated, but don't. It's a bug that is cheap to fix. |
If this behaviour isn't intended, we might add |
Rounding of textual output is not "cheap to fix" (it's subtle at the very least) as rounding happens in the output base (base 10) for which only one rounding mode (to nearest even) is implemented - and it is completely unrelated to Being able to provide a rounding mode would require a new method (as suggested). That requires a proposal. But read on. The purpose of the Text function is to provide a decimal approximation (or exact hexadecimal representation) of a binary floating point value. That decimal value is supposed to be the the closest value (given precision limits in the number of digits) to the binary value. Rounding is needed when the digits past the precision are not all zero. Only if those digits are a 5 followed by all zeros (the Being able to provide a rounding mode makes sense when one does (repeated) floating-point operations, but you're not making floating-point operations with the decimal number. In summary, I don't see a bug here; but perhaps a misunderstanding of how floating-point operations are working. |
What version of Go are you using (
go version
)?I'm using go.dev/play (2022-01-01 15:00)
Does this issue reproduce with the latest release?
https://go.dev/play/p/S-nYwa_t_uV
What operating system and processor architecture are you using (
go env
)?I'm using go.dev/play (2022-01-01 15:00)
What did you do?
I want to find the cubic root of x and need the exact number up to 10 decimal points. Thus the appropriate choice for the rounding is ToZero rounding mode, not ToNearestToEven.
The cubic root of x up to 600 decimal places is
What did you expect to see?
Edit: Let z be the cubic root of x and of type *big.Float.
I expect
z.SetMode(big.ToZero).Text('f', 10)
returns"99999999999999999999999999999999999999999999999999.9999999999"
What did you see instead?
(*Float).Text has no flexible rounding options. It just rounds with NearestEven.
z.SetMode(big.ToZero).Text('f', 10)
returns"100000000000000000000000000000000000000000000000000.0000000000"
And
z.Text('f', 550)
returns the correct value because the digit after 550 decimal point do not affect.It's the problem. (*Float).Text shall handle RoundingMode.
The text was updated successfully, but these errors were encountered: