Avatar of Chrysaor
Chrysaor asked on

Error in comparing two double values in C++ Builder

I've created an application , and I have two classes, let's name them ClassA , ClassB, and two objects respectively, ObjectA , ObjectB. In each class, I've created a public double, e.g doubleA, doubleB. Somewhere in the application, doubleA gets a value, e.g 0.15 and doubleB gets a value also. At some point, I try to compare both these doubles. And there is a very strange error. The first statement I use, is if (ObjectA.doubleA > ObjectB.doubleB) {.... return } , which if true ,always works . The next statement is :  if (ObjectA.doubleA == ObjectB.doubleB) {.... return }  and the third is : if (ObjectA.doubleA < ObjectB.doubleB) {.... return } . But, if the doubleA is indeed equal to doubleB, it doesn't recognise it, and it moves to the third statement which finds true (which is not!). In order to test this, I write each time the values of doubleA and doubleB in a txt file, and indeed, it says doubleA is 0.15, doubleB is 0.15 , but still, doesn't recognize the == statement , and moves forward and recognizes the < statement! Why that thappens? Is thit possible that there is a third digit (e.g 0.153 < 0.157) which is not shown it the text file? Thanks a lot.
Editors IDEsC++

Avatar of undefined
Last Comment
jkr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Zoppo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Zoppo

Sorry jkr, forgot to update before I posted my comment ...
jkr

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes