float x=0; x+=0.0000000 x+=0.0000000 x+=0.0000000 x+=0.0000000 x+=0.0000000 x+=0.0000000 x+=0.0000000 ... Then, x is not 0 Why? I don't know differ of float and double... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

float x=0; x+=0.0000000 x+=0.0000000 x+=0.0000000 x+=0.0000000 x+=0.0000000 x+=0.0000000 x+=0.0000000 ... Then, x is not 0 Why? I don't know differ of float and double...

float double

14th Sep 2016, 3:05 PM
윤석진
윤석진 - avatar
2 Answers
+ 2
It is true that when using floats, you may get rounding errors. Prefer using doubles whenever possible. http://code.sololearn.com/cg5I858hOWHJ #include <iostream> using namespace std; int main() { float x=0.0; double y=0.0; int i; for (i = 0; i < 10000000; i++) { x+=0.0000001; y+=0.0000001; } cout << x << endl; cout << y << endl; return 0; }
14th Sep 2016, 3:19 PM
Zen
Zen - avatar
0
If I understood your question well, you don't get the difference between double and float. actually, double is like a long float, it can accommodate up to 14 numbers after the decimal fraction point while float can only accommodate up to 6 numbers. hope this clarifies it.
14th Sep 2016, 4:48 PM
Waleed