<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Am 29.06.2018 um 18:45 schrieb Alan
Krause:<br>
</div>
<blockquote type="cite"
cite="mid:CAKKZKVL=Gnvw1x+qbVQQXJrmnYY-3M43x0utxGAfGpBnaT2G4A@mail.gmail.com">
<div dir="ltr">I stumbled upon something the other day that was
causing numerical differences between compiled Delphi and FPC
code. Executing the following sample console application
illustrates the issue clearly:
<div><br>
</div>
<div>
<div><font face="monospace">program test;</font></div>
<div><font face="monospace"><br>
</font></div>
<div><font face="monospace">uses</font></div>
<div><font face="monospace"> math, SysUtils;</font></div>
<div><font face="monospace"><br>
</font></div>
<div><font face="monospace">var</font></div>
<div><font face="monospace"> arg1 : double;</font></div>
<div><font face="monospace"> arg2 : double;</font></div>
<div><font face="monospace"> res : double;</font></div>
<div><font face="monospace">begin</font></div>
<div><font face="monospace"> arg1 := 100000.00;</font></div>
<div><font face="monospace"> arg2 := 72500.51;</font></div>
<div><font face="monospace"> writeln( 'arg1 = ' +
FormatFloat( '0.00000000', arg1 ) );</font></div>
<div><font face="monospace"> writeln( 'arg2 = ' +
FormatFloat( '0.00000000', arg2 ) );</font></div>
<div><font face="monospace"><br>
</font></div>
<div><font face="monospace"> res := arg1 - arg2;</font></div>
<div><font face="monospace"> writeln( 'arg1 - arg2 = ' +
FormatFloat( '0.00000000', res ) );</font></div>
<div><font face="monospace"> writeln( 'Max( arg1 - arg2, 0 )
= ' + FormatFloat( '0.00000000', Max( res, 0 ) ) );</font></div>
<div><font face="monospace"> writeln( 'Max( arg1 - arg2, 0.0
) = ' + FormatFloat( '0.00000000', Max( res, 0.0 ) ) );</font></div>
<div><font face="monospace">end.</font></div>
</div>
<div><br>
</div>
<div>--- begin output (Linux x86_64) ---</div>
<div><br>
</div>
<div>
<div><font face="monospace">arg1 = 100000.00000000</font></div>
<div><font face="monospace">arg2 = 72500.51000000</font></div>
<div><font face="monospace">arg1 - arg2 = 27499.49000000</font></div>
<div><font face="monospace"><b>Max( res, 0 ) = 27499.49023438</b></font></div>
<div><font face="monospace">Max( res, 0.0 ) = 27499.49000000</font></div>
</div>
<div><br>
</div>
<div>--- end output ---</div>
<div><br>
</div>
<div>I am guessing that the integer value of zero is causing the
wrong overloaded function to be called? I was able to solve
the problem in my code by replacing the 0 with 0.0.</div>
</div>
</blockquote>
<br>
The compiler converts the 0 to the type with the lowest precision
that can hold the value (or the largest if none can hold it
exactly). For 0 this is already satisfied by Single, so the compiler
essentially has the parameter types Double and Single. For some
reason (I don't know whether it's due to a bug or by design) it
picks the Single overload instead of the Double one.<br>
Someone who knows more about the compiler's overload handling would
need to answer why it favors (Single, Single) over (Double, Double)
for (Double, Single) parameters (or (Single, Double), the order
doesn't matter here).<br>
<br>
Regards,<br>
Sven<br>
</body>
</html>