0

I have problem with an if statement not being executed. The corners below are fixed, const. Now I want a static variable alphadrone move from one corner to the other.

The first 'if' works, the second not.

LATLON_TO_CM is 1.113195f. I tried to put f behind every number, made all float, double, the statement don't gets executed. This isn't logic, I put the same number into two variables (that have different names but the same datatype) and it doesn't work.

const int32_t corner_1_X = 47.590000 * 1e3 * LATLON_TO_CM;
const int32_t corner_1_Y = 7.646000  * 1e3 * LATLON_TO_CM; //bigger
const int32_t corner_2_X = 47.590000 * 1e3 * LATLON_TO_CM;
const int32_t corner_2_Y = 7.644000  * 1e3 * LATLON_TO_CM; //smaller

static int32_t alphadroneXposition = corner_1_X;
static int32_t alphadroneYposition = corner_1_Y;

// if (corner_1_Y > corner_2_Y)
    // moveAlphadroneYneg(&alphadroneYposition);
if (alphadroneYposition > corner_2_Y)
    moveAlphadroneYneg(&alphadroneYposition);
  • 3
    Please provide a minimal but complete example. It's impossible to say anything with the little info you provide. I'd guess integer overflow as cause of confusion though. – Ulrich Eckhardt Nov 29 '15 at 19:53
  • Defining `LATLON_TO_CM = 1`, the if executed fine here. Watch out for overflow or bad conversions. – villasv Nov 29 '15 at 19:57
  • any change in the value of `alphadroneYposition` in your code should be shared, otherwise the conditions are similar – Naman Nov 29 '15 at 19:59
  • 1
    You are aware that `int32_t` is an integer type, but you're mixing with `double` precisions that can't be represented there and simply are lost? – πάντα ῥεῖ Nov 29 '15 at 19:59
  • In my experience, ghosts like the one you're seeing were consequence of code and executable not being in sync. Try rebuilding your project (all build types, i.e. Debug, Release, etc). – LavaScornedOven Nov 29 '15 at 20:05
  • If this code is in a function that is executed more than once, the initialization of static variables will only occur the first time the function is executed. After that, `alphadroneYposition` will retain its value from the previous invocation. – M.M Nov 29 '15 at 22:05
  • @M.M: this was the solution! Thanks! I decreased too much! so much trees! – Marek Duchac Nov 29 '15 at 22:49

1 Answers1

-1

So for some reason your code isn't saying that alphadroneYposition is greater than the corner position, even though it is a little bit bigger. I looked at it like three times myself just to confirm that yes, that first number is a little bit bigger.

alphadroneYposition = 7.646000 * 1e3

corner_2_Y = 7.644000 * 1e3

Usually this happens because of floating point precision and the errors that result from comparing two floating point numbers. Check out this related topic from stackoverflow on comparing floats.

What is the most effective way for float and double comparison?

Hope this helps!

Community
  • 1
  • 1
reidjs
  • 149
  • 1
  • 10
  • There's no problem with the floating-point values you've given here. Superstition is no substitute for knowledge. – Pete Becker Nov 29 '15 at 20:26