Hello, silly question but the following snippet contains an error that I can’t find
var (
an float64
n, avg = 0, 0.
)
for ; ; n++ {
fmt.Print("number? ")
fmt.Scan(&an)
if an == 0. {
break
}
avg += an
}
if n != 0 {
fmt.Println(avg / float64(n))
}
Because of the known imprecision that floats are accomponied with.
If you need precise values, you need a precise type.
There are a lot of blog posts available around this topic, I picked the first three hits on google about “floating point money” (as money is the most common example used as “precise value”):
You can’t round anything up or down to a value that is not representable.
All links we gave you, tell about the inherint imprecission of floating point computations, and either you are in a field where you can safely allow this imprecissions (eg. computer graphics) or not (eg. money).