0

Why is this happening?

#include<iostream> #include<conio.h> using namespace std; int main(){ int n; double x=87.19856; float y=87.19856; n=x; cout<<endl<<"x= "<<x-n; //in this case x=0.19856 n=y; cout<<endl<<"y= "<<y-n; //in this case y=0.198563 the number "3" is wrong. return 0; } /* this happen with others numbers too. my english is pretty bad*/

20th May 2018, 2:02 AM
Link Skwor
Link Skwor - avatar
2 Antworten
+ 6
http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inaccurate.htm tl;dr - There is no guarantee for computers to represent floating-point values accurately.
20th May 2018, 3:53 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
woooo "Some decimal numbers can’t be represented exactly in binary, resulting in small roundoff errors." and "double has more precision than floats." I didn't have idea. thanks for the link and the answer.
20th May 2018, 11:36 AM
Link Skwor
Link Skwor - avatar