2

Possible Duplicate:
How should I do floating point comparison?

this is a snippet of code and I can't figure out what's wrong with it.

The if statement is evaluating the numbers incorrectly and going to "else".

double m,k;

m = (y3-y1)/(x3-x1);
k = m*(-x1)+y1;

cout <<"\n\n"<< m <<"  "<< k << "\n";
cout <<"\n\n"<< ((-a)/b) <<"  "<< c/b << "\n";

if(m==((-a)/b) && k==c/b)
 {
  cout << "\nTaisne sakrīt ar kādu no trīsstūra malām!" << endl;
  goto beigas;
 }
  else
   cout << "\n\n WRONG \n\n";

The user inputs values x1,y1,x3,y3,a,b,c - all are double.

It seems to work fine only with "m". And it is strange that when I tried the following, the if statement was true with the same values:

if(m==((-a)/b) && k<c/b)

Here is a screenshot to get the whole problem:

http://i49.tinypic.com/307n88l.jpg

The code starts after user has input all the values.

Thanks, any help would be greatly appreciated!

Community
  • 1
  • 1
  • 12
    Floating-point arithmetic is not exact. – chris Oct 29 '12 at 19:02
  • what are the values of the variables? – yohannist Oct 29 '12 at 19:04
  • @chris's concern could address that by checking if c/b-k is less than some small value. – BostonJohn Oct 29 '12 at 19:04
  • You better never compare double, it rarely will give you the expected results. – linello Oct 29 '12 at 19:05
  • 3
    And another thing, please AVOID the goto statement, GOTO is evil! – linello Oct 29 '12 at 19:06
  • 4
    [What every programmer should know about floating point](http://floating-point-gui.de/) – Barmar Oct 29 '12 at 19:07
  • 2
    What can happen to you if you use [goto statement](http://xkcd.com/292/) – Yamaneko Oct 29 '12 at 19:15
  • 1
    @VictorHugo: Incautious use of goto statements leads to code that is difficult to understand and maintain, because the control flow is less apparent to humans than code that uses more regular structures, such as blocks of code with single entry points and single exits, or at least well-defined exits to a common point. This increases costs of software development and maintenance and increases bugs. – Eric Postpischil Oct 29 '12 at 19:32
  • @Kristaps_Folkmanis GOTO considered harmful - http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html – dmcgill50 Oct 29 '12 at 21:06

1 Answers1

0

when you want to compare two floating points use a threshold like that one:

double m = a/b;

if (abs(m - (a/b)) < 0.0001)
{
    // execute code where m == a/b here
}

so basically to compare float/double subtract them and make sure the result is less than certain threshold.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
  • 2
    A constant threshold value like `0.0001` may or may not be the best approach. Determining whether two floating-point values are close enough to be considered "equal" can be difficult; it can depend on how the values were computed and on what they mean. – Keith Thompson Oct 29 '12 at 19:15
  • 1
    This method reduces false negatives (incorrect rejections of equality) but increases false positives (incorrect acceptance of equality). Without knowing the requirements of the application, you cannot know whether this is the correct solution. – Eric Postpischil Oct 29 '12 at 19:16
  • Thanks, it solved the problem. Though I also had to make some alterations in the code elsewhere to make the whole program work. Also, the calculations work with a slight mistake percentage, say, 0.01% anyway, but that's affordable in my situation. Now I will know that you have to have a different approach when dealing with floating point numbers. – Kristaps Folkmanis Oct 29 '12 at 20:47