views:

165

answers:

3

Possible Duplicate:
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?

sqrt(25.0)

turns out to be 4.99999 ,instead of 5

how would I fix this problem?

+7  A: 

you won't fix this problem.

This is caused by machine precision that can't be arbitrary. Just look at wikipedia and you will discover why..

A note: this has been asked a plethora of times, just search for it before opening a new question please.

Jack
+5  A: 

As Jack said, you can't avoid this. However, a 'fix' might be to round the sqrt result. For example using the round function.

muksie
+3  A: 

Since you can't avoid it, take a look at rounding functions, such as: round() , ceil() , floor()

There's also this nice article about it.

karlphillip