Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go float number division

Tags:

go

I try to get 2.4/0.8 == 3 in Go

w:=float64(2.4)
fmt.Println(math.Floor(w/0.8),math.Floor(2.4/0.8) )

It gives me "2 3".

The question is why math.Floor(w/0.8) won't give me 3. Is it the precision limit of float number?

like image 908
Zhe Hu Avatar asked Mar 11 '13 15:03

Zhe Hu


1 Answers

The program output is correct. The representation of many real numbers in the IEEE format is not precise. The first number is actually (in the limited 64 bits version of it) less than 3 so floor returns '2' correctly. The second one is computed at compile time using greater precision.

Recomended reading.

like image 195
zzzz Avatar answered Sep 28 '22 03:09

zzzz