Reputation: 7841
I have a long double value x. which value is actually 3.00. But in the debugger I see the value is 3.00000000000012312414 that is some garbage value after 13th decimal point. How do I discard this garbage? If I had to print, I can just write printf("%.10llF",x);
but I have to compare if the number is integer. One way to compare is long long xt = x; if(x == xt)
I will just take the values before 12th place. But for the garbage values, this isn't working. How do I do that?
Upvotes: 1
Views: 1820
Reputation: 9293
you should use some epsilon to compare floating point values.
3.00000000000012312414
can not be represented just as 3.000000000000000000
because computer representation of numbers is not that precise.
If it should be exactly 3, you have to declare it as integer.
Upvotes: 0