Tamim Addari
Tamim Addari

Reputation: 7841

How to discard the garbage values of a double?

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

Answers (2)

dantuch
dantuch

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

rerun
rerun

Reputation: 25505

That is not garbage it is how floating point numbers are stored. To compair floating point numbers you always need to define an acceptable error e. When you compare x to y if the abs(x -y) < e the numbers are equal. Also take a look at this document

Upvotes: 3

Related Questions