Link to home
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.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Sorry jkr, forgot to update before I posted my comment ...