3

I have a very simple if statement that is not working as expected.

My main problem is, that the immediate window evaluates the if statement differently then the code execution:

   if( FreeProductStorageVolume < product.Volume * quantity )
   {
      Debug.Log( FreeProductStorageVolume );
      Debug.Log( product.Volume );
      Debug.Log( quantity );
      Debug.Log( product.Volume * quantity );
      canProduce = false;
   }

all the variables are floats

enter image description here

everything suggests that the breakpoint in line 824 should not be hit.

even the Immediate window evaluates the if() statement as false.

has anything like this happened to anyone else?

Jinjinov
  • 2,554
  • 4
  • 26
  • 45

1 Answers1

6

This looks like a floating point precision issue. This expression:

FreeProductStorageVolume - quantity * product.Volume.

seems to evaluate to a positive,however, very small number, for example 1E-20

Alex
  • 21,273
  • 10
  • 61
  • 73
  • My main problem is, that the immediate window evaluates the if statement differently then the code execution. Why would that happen? Even if the result is 1E-20, it should be the same in the code and in the immediate window. – Jinjinov Apr 22 '15 at 13:10
  • The immediate window may evaluate the results in a slightly different way, for example use less precision. – Alex Apr 22 '15 at 13:15
  • That bothers me a lot more than it should :) They should not afford such mistakes! – Jinjinov Apr 22 '15 at 19:30