This behavior is how floating-point math works, which is not best addressed
in the documentation for the formatted I/O library. We see may comments
from people new to floating point who argue that there are bugs in Go's
arithmetic, but it's invariably a lack of familiarity with the—admittedly
rather tricky—properties of imprecise arithmetic.

-rob


On Tue, Oct 11, 2022 at 9:01 AM Dante Castagnoli <dante.castagn...@gmail.com>
wrote:

> Thanks!
>
> It's not lost, though.  It shows up with the %g form.
>
> Knowing about %g, my issue is not extant, but others might hit it.  I'm
> relatively new to floating point issues, and I saw numbers showing up as
> integers, and it took a while to sort out what was going on, is all.
>
> As the behavior matches "c", I would suggest at minimum making the
> documentation clearer.
>
> https://go.dev/play/p/WBlf3jltimr
>
> On Monday, October 10, 2022 at 10:27:00 AM UTC-7 Brian Candler wrote:
>
>> Whilst Go handles constants with arbitrary precision AFAIK, precision is
>> lost once values are assigned to a float64 variable (or passed as an
>> argument to a function).  There are only 53 bits of precision in the
>> "mantissa".
>>
>> For more info, you might find this helpful:
>> https://0.30000000000000004.com/
>>
>> On Monday, 10 October 2022 at 17:48:56 UTC+1 dante.ca...@gmail.com wrote:
>>
>>> Hi,
>>>
>>> I'm wondering what folks think about the behavior of %f and precision.
>>>
>>> The go fmt documentation states:
>>> > For floating-point values, width sets the minimum width of the field
>>> and precision sets the number of places after the decimal,
>>>
>>> What I discovered is there is rounding going on in %f as shown below.
>>>
>>> https://go.dev/play/p/23AfRtwOqcD
>>>
>>> I found this behavior as I'm converting floats to integers, and was
>>> testing fine regions between digits, and experienced the behavior.   I
>>> didn't expect it.
>>>
>>> I expected the behavior of printing "n" characters of a string.
>>> However, I note that "c" behaves the same way:
>>>
>>> $ cat math.c
>>> #include<stdio.h>
>>> int main(int argc, char *argv[]) {
>>>         printf("%0.3f\n", 0.9999999999);
>>> }
>>> $ ./math
>>> 1.000
>>> $
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/4a15ac2c-e30d-4d6e-be8f-251996fb01cen%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/4a15ac2c-e30d-4d6e-be8f-251996fb01cen%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOXNBZRq9U4O-cCFxugZSFmJf-z0rR10HAE5Do2HGDPB30nuXQ%40mail.gmail.com.

Reply via email to