From Castle Game Engine: link to original post
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” (which is a term I tend to use to mean “operating system and processor”). The short version is: use Math.SameValue
, but I explain much more details 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.
You must be logged in to post a comment.