GoLang for loop with floats creates error

Viewed 1905

Can someone explain the following. I have a function in go that accepts a couple of float64 and then uses this value to calculate a lot of other values. The function looks like

func (g *Geometry) CalcStresses(x, zmax, zmin float64)(Vertical)

the result is put into a struct like

type Vertical struct {
    X float64
    Stresses []Stress
}

Now the funny thing is this. If I call the function like this;

for i:=14.0; i<15.0; i+=0.1{
    result := geo.CalcStresses(i, 10, -10)
}

then I get a lot of results where the Stress array is empty, antoher interesting detail is that x sometimes shows like a number with a LOT of decimals (like 14.3999999999999999998)

However, if I call the function like this;

for i:=0; i<10; i++{
    x := 14.0 + float64(i) * 0.1
    result := geo.CalcStresses(x,10,-10)
}

then everything is fine.

Does anyone know why this happens?

Thanks in advance, Rob

1 Answers
Related