I think michael Jones ' explanation is correct...when we x : =1.3
Go translates this to a binary representation of 1.3 that is a very close
approximation to decimal 1.3
so when you do fmt.Printf( "\n %v ", x) you get the the binary version of x
translated back to decimal, which will not be exactly 1.3
x=1.3(decimal) is NOT== 1.3(binary) exactly but will be very close
approximation to it
So to check floating point numbers for equaliy don't use if a== 1.4
{.....} instead use if math.Abs(x-1.4) < 1.0e-8 {... }
Whether you use 1.0e-8 or something some other value depends on your
choice of 'how close to 1.4' would you consider ' is good enough' for the
code you are working with
On Monday, April 15, 2019 at 5:18:51 AM UTC-4, Miki Tebeka wrote:
>
> Hi,
>
> Can anyone explain the below?
> (When printing out b with %.20f it prints 1.20999999999999996447)
>
> Thanks
>
> package main
>
> import "fmt"
>
> func main() {
> a, b := 1.1*1.1, 1.21
> fmt.Println(a == b) // true
> fmt.Println(1.1*1.1 == 1.21) // false
> }
>
>
>
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.