I wrote a program that adds the following:
1/1 + 1/2 + 1/3 ... + 1/5000
However, by using and double and float, I got two different values. (Of course, I used proper type casting and such, and I did not receive any warnings or errors; the program runs great.) Arithmetically, the values should be the same, but I know there is something about using a float or a double that makes it different. What is it?
Thanks!
Adding numbers using Float and Double in C++?
It's because of precision and rounding. The float type has only about 7 decimal digits of precision. Double is around 16 digits. Many of the values in your series cannot be exactly represented in binary.
Small rounding errors, summed over 5000 values can make a difference as you have seen.
Reply:Float is floating point, double is set decimal value. You're result will be rounded using double.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment