Advertisement
michaliskambi

Test comparing Nan using FPC

Jan 10th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.95 KB | None | 0 0
  1. uses Math, CastleVectors;
  2. var
  3.   F, F2, F3: Single;
  4. begin
  5.   F := Nan;
  6.   F2 := Nan;
  7.  
  8.   { Doing some operations on Nan values produces "Invalid floating point operation".
  9.     (Unless the FPU exceptions are masked out. Which happens to be always true when
  10.     a program uses OpenGL, FPC GL unit has to mask out exceptions to protect from
  11.     OpenGL implementations producing FPU errors.)
  12.     So below we  explicitly silence them by Math.ClearExceptions(false). }
  13.  
  14.   Writeln(F = F2); // true
  15.   Math.ClearExceptions(false);
  16.  
  17.   Writeln(FloatsEqual(F, F2)); // false (reason explained below)
  18.   Math.ClearExceptions(false);
  19.  
  20.   F3 := F - F2; // Nan, because subtracting Nans is Nan
  21.   Math.ClearExceptions(false);
  22.   Writeln(F3);
  23.  
  24.   F3 := Abs(F3); // Nan, because Abs(Nan) is Nan
  25.   Math.ClearExceptions(false);
  26.   Writeln(F3);
  27.  
  28.   Writeln(F3 < 0.1); // false, because comparing using < or > normal number with Nan is false
  29.   Math.ClearExceptions(false);
  30. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement