Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hope your well.
When I am comparing two variables having value either 0.5 or 0.25, the program is showing the desired output but when I am comparing two variables having value either 0.333333 or 0.666667, the program is not showing the desired output. Will you please tell me where I am going wrong? Please help me in writing the correct piece of code. I am a novice and it will be a great experience to learn from you. Thank you for your concern.Here is the code with some of the outputs:

C++
#include 


OUTPUTS:

0.333333 0.333333
UNEQUAL

0.666667 0.666667
UNEQUAL

0.25 0.25
EQUAL

0.5 0.5
EQUAL


Regards,
Prateek Senapati

What I have tried:

I have tried to enter different decimal values but did not get expected results in all the cases.
Posted
Updated 8-Oct-16 23:35pm
v7

Just consider that the floating point representation cannot precisely express numbers like 1/3. The floating point number format uses a fractional representation in base 2, i.e. it can represent numbers like 1/2, 1/4, 1/8, 1/16 etc. correctly, but not 1/3 or 1/5. For those it uses the nearest approximation. Now, when you do computations in floating point format the approximation error gets bigger and bigger with every single computation. Hence, the representation of 0.3333... as constant in your program might not exactly be equal to your computation result.

The solution: Don't compare for equality, but always allow for a small approximation error.
 
Share this answer
 
Comments
Prateek Senapati 9-Oct-16 6:33am    
I could not understand the solution but it seems useful. How can I allow for a small approximation and still get the expected result?
Here is my code:
#include
nv3 9-Oct-16 12:18pm    
Your code did not show in the comment. Use the green "Improve question" button below your question to add your code.

The comparison should look like this:

if (fabs (x - y) < epsilon) {
...
}

And epsilon is the approximation margin you allow, for example 1e-8 if you are computing in double and your values are in the range of 1.
Floating point numbers can not represent most real numbers exactly. The numbers 0.25 and 0.5 can be represented exactly but not 0.33 and 0.66.

This is sourced by how these numbers are represented internally (see IEEE Floating Point - Wikipedia[^].

To check if floating points numbers are nearly equal, special checks must be performed. See for example The Floating-Point Guide - Comparison[^].
 
Share this answer
 
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 
Comments
Prateek Senapati 9-Oct-16 4:39am    
Actually, I could not post my question correctly. My question included the code and some of the outputs as well. I can't figure out how to edit it. I'm really sorry for the inconvenience caused.
Patrice T 9-Oct-16 4:40am    
Use Improve question to update your question.
Prateek Senapati 9-Oct-16 4:47am    
Here is the code:
#include
Prateek Senapati 9-Oct-16 4:48am    
I am not able to enter the piece of code after #include

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900