Recently I got a very nice question about comparing non-integer values in Pascal (thank you to Jacek Mulawka!).
The core of the question was: Does the condition below (if
) evaluate to true
or false
, and why?
MyVar := 0.1;
if MyVar = 0.1 then
Writeln('Yes, MyVar equals 0.1')
else
Writeln('No, MyVar does not equal 0.1');
I created a testcase for this (compiles with both FPC and Delphi) and I provided a lengthy explanation what happens and why in this repository:
How does comparing “MyVar = 0.1” work for different types?
As you can read there, it’s actually quite complex what happens, and depends on both the type, and compiler, and “platform” (“platform” is a term I tend to use to mean “combination of operating system and the processor, OS / CPU”). The short version of the conclusion is: use Math.SameValue
to compare non-integers, but I explain much more details (and justified exceptions to this simplified conclusion) in the linked repo, so please read it 🙂
I hope that this overview is helpful to everyone dealing with floats, in Pascal or otherwise 🙂
P.S. The screen attached to this post continues our tradition “if you don’t have anything relevant to show, post something pretty rendered using our engine”. This is a floating castle from Sketchfab created by Shahriar Shahrabi and rendered in our Castle Model Viewer.
Start the discussion at Castle Game Engine Forum