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/e3dfb9d8-285f-486c-a487-04a2a4410c77n%40googlegroups.com.

Reply via email to