0

Below is a function i'm trying to use to decide if a player has moved a checkers piece up left or up right. moveTo holds a decimal like 5.2 and moveFrom would hold square number 6.3 the difference would be -1.1 and is assigned to moveValue (i'm checking it with the trace statement and it is assigning correctly).

however when it hits the ifs it's not coming out as equal and so it runs the last if statement, however if i hard code -1.1 to moveValue it does evaluate correctly. Any ideas why it's not evaluating correctly would be a big help. I have posted the output from running the program under the code :

int legalPlayerMove(float moveTo, float moveFrom)
{

    float moveValue=(moveTo-moveFrom);
    cout<<"trace move value"<<moveValue<<endl;


    if(moveValue==(-1.1))
        cout<<"moved up left"<<endl;
    if(moveValue==(-0.9))
        cout<<"moved up right"<<endl;
    if(moveValue!=-1.1||moveValue!=-.9)
        cout<<"that is not a legal move, please renter the square number you wish to move to."<<endl;

    return 0;
}
  • please enter the location of the piece you want to move
  • 6.3
  • now please enter where you would like to move the piece
  • 5.2
  • trace move value-1.1
  • that is not a legal move, please renter the square number you wish to move to.
  • Tress any key to continue . . .
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
James
  • 309
  • 1
  • 15
  • 2
    Do not use == to compare floating point values. When you subtract two floating point values, there is no guarantee that it will exactly equal a floating point constant. Why are you using floating point anyway? What's wrong with using integers? – PaulMcKenzie Apr 06 '14 at 20:33
  • possible duplicate of [Is floating-point == ever OK?](http://stackoverflow.com/questions/4682889/is-floating-point-ever-ok) – legends2k Apr 06 '14 at 20:34
  • i'm using floating point to number the checkers board, with the rows being whole numbers and the columns being decimals. – James Apr 06 '14 at 22:06

3 Answers3

3

It's always problematic to compare float or double variables against exact float or double literal values! Also using the operator== isn't a good idea in general.

Use a comparison against a minimal difference (aka epsilon) of the values instead:

if(std::numeric_limits<float>::epsilon() + moveValue > 1.1) { // ...
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

Adding to what was said, there is no exact representation of 0.1 or its multiples in binary. It's an infinite series of powers of 2.

There is no guarantee that the compiler is going to round your constant values exactly like the run time library rounds the input value.

You should use integer pairs for what you are doing.

You care confusing the textual representation of a number with its inherent value.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • thanks for the explanation of what was happening, i fixed it by multiplying by 10 and then static casting to int at the comparison. – James Apr 06 '14 at 22:08
0
#define EPS 0.000001
if(EPS + moveValue > 1.1){..}
...
NiVeR
  • 9,644
  • 4
  • 30
  • 35