浮点数除法

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?

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.

Yes, it is the precision limit. float64 is pretty precise, but 2.4 can't be stored exactly in binary. When both the numbers are constants, the computation is done in higher precision at compile time, and when the result is rounded to float64, it comes out to exactly 3. But when one of the numbers is a variable, the calculation has to be done at runtime, and it comes out to 2.9999999999999996, which Floor truncates to 2.

Although this doesn't answer the question(why) I've found this page looking for a round function so just in case someone finds it helpful here is my own solution:

   func Round(val float64) (float64) {
        if float64(int(val)) < val {
            return float64(int(val) + 1)
        }
        return val
    }