Why comparing double and float leads to unexpected result? [closed]?

The important factors under consideration with float or double numbers are: Precision & Rounding Precision: The precision of a floating point number is how many digits it can represent without losing any information it contains. Consider the fraction 1/3. The decimal representation of this number is 0.33333333333333… with 3′s going out to infinity.An infinite length number would require infinite memory to be depicted with exact precision, but float or double data types typically only have 4 or 8 bytes.

Thus Floating point & double numbers can only store a certain number of digits, and the rest are bound to get lost. Thus, there is no definite accurate way of representing float or double numbers with numbers that require more precision than the variables can hold. Rounding: There is a non-obvious differences between binary and decimal (base 10) numbers.

Consider the fraction 1/10. In decimal, this can be easily represented as 0.1, and 0.1 can be thought of as an easily representable number. However, in binary, 0.1 is represented by the infinite sequence: 0.00011001100110011… An example: #include int main() { using namespace std; cout Whenever comparing two close float and double numbers such rounding errors kick in and eventually the comparison yields incorrect results and this is the reason you should never compare floating point numbers or double using ==.

The best you can do is to take their difference and check if it is less than an epsilon. Abs(x - y).

This is a valid answer, +1 to offset downvotes. It leaves information to be desired, but is valid all the same. – Zéychin Jul 17 at 6:33 1 They can be represented fairly accurately, just not absolutely precisely in all cases.

– aroth Jul 17 at 6:37 1 @Als: This information is much better. Converted my down-vote to an upvote, and removed my dissenting comments :) – Merlyn Morgan-Graham Jul 17 at 6:55.

Try running this code, the results will make the reason obvious. #include #include int main() { std::cout The double data type can accurately represent the contents of the float, so the comparison yields false.

In IEEE 754, fractions are stored in powers of 2: 2^(-1), 2^(-2), 2^(-3), ... 1/2, 1/4, 1/8, ... Now we need a way to represent 0.1. This is (a simplified version of) the 32-bit IEEE 754 representation (float): 2^(-4) + 2^(-5) + 2^(-8) + 2^(-9) + 2^(-12) + 2^(-13) + ... + 2^(-24) + 2^(-25) + 2^(-27) 00011001100110011001101 1.10000002384185791015625 With 64-bit double, it's even more accurate.It doesn't stop at 2^(-25), it keeps going for about twice as much. (2^(-48) + 2^(-49) + 2^(-51), maybe?

) Resources IEEE 754 Converter (32-bit).

Generally you shouldn't compare floats to floats, doubles to doubles, or floats to doubles using ==. The best practice is to subtract them, and check if the absolute value of the difference is less than a small epsilon. If(std::fabs(f - d) ::epsilon()) { // ... } One reason is because floating point numbers are (more or less) binary fractions, and can only approximate many decimal numbers.

Many decimal numbers must necessarily be converted to repeating binary "decimals", or irrational numbers. This will introduce a rounding error. From wikipedia: For instance, 1/5 cannot be represented exactly as a floating point number using a binary base but can be represented exactly using a decimal base.In your particular case, a float and double will have different rounding for the irrational/repeating fraction that must be used to represent 1.1 in binary.

You will be hard pressed to get them to be "equal" after their corresponding conversions have introduced different levels of rounding error. The code I gave above solves this by simply checking if the values are within a very short delta. Your comparison changes from "are these values equal?

" to "are these values within a small margin of error from each other? " Also, see this question: Most effective way for float and double comparison There are also a lot of other oddities about floating point numbers that break a simple equality comparison. Check this article for a description of some of them: cygnus-software.com/papers/comparingfloa....

The ε-neighbourhood is usually not the best practice, but rather a lazy workaround. Most often you do not really want == anyway, but =, and just think you need == because that happens to work fine for integers since the case > is never reached with these. €” Also, sometimes you do in fact want == for floats, though this occurs rather seldom.

– leftaroundabout Jul 17 at 11:49 @leftaroundabout: This is contrary to most of the articles I've read. Can you elaborate on these cases? Maybe add your own answer :) – Merlyn Morgan-Graham Jul 17 at 12:00.

Floats and doubles are stored in a binary format that can not represent every number exactly (it's impossible to represent the infinitely many possible different numbers in a finite space). As a result they do rounding. Float has to round more than double, because it is smaller, so 1.1 rounded to the nearest valid Float is different to 1.1 rounded to the nearest valud Double.To see what numbers are valid floats and doubles see Floating Point.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions