0

I just wrote a bit of code for my shop system and a very simple line wasn't working:

...
if (dir == "left" && rect.anchoredPosition.x != -217.5f) {...}
... 

The code in the {} would still run if the anchored position.x was -217.5. But some lines under there is this line:

...
else if(rect.anchoredPosition.x != 217.5f)
...

and that worked fine!

Later I changed the first line to:

if (dir == "left")
{
   if (rect.anchoredPosition.x != -217.5f)
     {
         ...
     }
}
...

and that worked fine, just as intended.

I checked the anchored position several times, so it wasn't it's fault. I know that dir was "left" so that one was true, but the anchored was -217.5 so it should've returned false.

I don't know why it did this. In the end it doens't matter, because with the two if-statements seperate it works, but I still want to understand why.

Thanks

  • 6
    Could it be that the value isn't `-217.5f` exactly in the situations where it fails a la [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken)? – ProgrammingLlama Apr 14 '23 at 10:21
  • 2
    I won't check equality of floating point numbers in that way, there could always be a small difference that you're not able to see but program does... – Marco Apr 14 '23 at 10:23
  • 1
    tl;dr: floating point numbers are _approximations_ - ever expect them to be _precise_ – Franz Gleichmann Apr 14 '23 at 10:26
  • As said, if I change the code so that the two stements are seperate, and I did not change any code regarding the positioning, it works. So could be that Unity somehow changed the position on it's own by a tiny bit, but probably not. So even if floats are just approximations it's strange how it works when the two are seperated and how it doesn't work if they are not. – Not Available Apr 14 '23 at 10:26
  • -217.5 can be represented perfectly in binary. But -217.49999999999999999 or -217.5000000000001 can't be. Is it not possible that the value is almost but not exactly -217.5? – ProgrammingLlama Apr 14 '23 at 10:32
  • did you use your debugger to see the exact value? – MakePeaceGreatAgain Apr 14 '23 at 10:32
  • @ProgrammingLlama I've tested it again and it's the same result: works with the two seperated and not when not seperated. So by like 0.00001% it could be off, but needed to be a very big coincedence – Not Available Apr 14 '23 at 10:39
  • @MakePeaceGreatAgain I used Inspector in Debugging-Mode and Debug.Log. But it works if the if-statements are seperated and I didn't change any values, so it's probably not the debuggers fault – Not Available Apr 14 '23 at 10:41
  • 1
    The second code is positive `217.5`, I doubt the examples you provided isn't the real situation. – shingo Apr 14 '23 at 10:46
  • @shingo It indeed is, and thre is no mistake here. Just wanted to show that != is working in other cases and in my code, like 4 lines after the thing that's not workingm, there is a line with positive 217.5f. Do you want proof with a screenshot? – Not Available Apr 14 '23 at 12:07
  • `... && MathF.Abs(rect.anchoredPosition.x + 217.5f) < 0.01f` (here I've assumed `0.01` tolerance) – Dmitry Bychenko Apr 14 '23 at 12:22
  • If possible please copy the original code. Perhaps you have omitted important information, although you are confident that they will not affect the results. – shingo Apr 14 '23 at 13:35
  • @shingo [here](https://docs.google.com/document/d/1OSM4iXDazupGhYeOfiEidbVc3tTZR08sRF6dkq1ih1k/edit?usp=sharing)'s the code (important lines formattet, unimportant unformattet) – Not Available Apr 15 '23 at 10:35

1 Answers1

0

You have two versions of the code.

Version A:

if (dir == "left" && rect.anchoredPosition.x != -217.5f)
{
    rect.anchoredPosition -= new Vector2(435f, 0f);
}
else if(rect.anchoredPosition.x != 217.5f)
{
    rect.anchoredPosition +=new Vector2(435f, 0f);
}

Version B:

if (dir == "left")
{
    if (rect.anchoredPosition.x != -217.5f)
    {
        rect.anchoredPosition -= new Vector2(435f, 0f);
    }
}
else if(rect.anchoredPosition.x != 217.5f)
{
    rect.anchoredPosition +=new Vector2(435f, 0f);
}

They are logically different.

For example you have rect.anchoredPosition.x = -217.5f and dir = "left".

In version A the first if isn't hit, but the second is, so rect.anchoredPosition.x will be changed to 217.5f after that. I guess that's why you think it still runs.

In version B the first if is hit, but the inner if isn't, nothing changes.

shingo
  • 18,436
  • 5
  • 23
  • 42
  • Yeah thanks. Is thought I didn't need if(_dir = "right" && _rect.anchoredPosition.x != 217.5f) because in the first if-statement it says if dir = "left" so in the second if has to be "right" than, but apperently I need it, because of the reasons you gave. Thanks – Not Available Apr 15 '23 at 12:06