<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>Not so long ago, Florian was proudly bragging about "Pascal does
not allow you to <a moz-do-not-send="true"
href="http://www.toodarkpark.org/computers/humor/shoot-self-in-foot.html">shoot
yourself in the foot</a>"</p>
<p>What about this little program:</p>
<p>program Project1;<br>
<br>
var a,b: byte;<br>
begin<br>
a:=1;<br>
b:=a*(-1);<br>
writeln(b); // result: 255<br>
end.<br>
<br>
</p>
<p>The result is obviously correct, given how the variables are
declared. But there are no compiler warnings / errors that the
assignment b:=a*(-1) is fishy, to put it mildly. And if you are
serious about strong typing, it ought to be illegal, with a
suitable complaint from the compiler.</p>
<p>Who is shooting whom in the foot?</p>
<p>Wolf<br>
</p>
<br>
<div class="moz-cite-prefix">On 02/07/2018 20:22, Santiago A. wrote:<br>
</div>
<blockquote type="cite"
cite="mid:46b878ed-ba64-833d-18c2-375ea411a86e@ciberpiula.net">El
01/07/2018 a las 10:27, C Western escribió:
<br>
<blockquote type="cite">On 29/06/18 21:55, Sven Barth via
fpc-pascal wrote:
<br>
<br>
More confusingly, if a single variable is used, the expected
Max(Double, Double) is called:
<br>
<br>
function Max(a, b: Double): Double; overload;
<br>
begin
<br>
WriteLn('Double');
<br>
if a > b then Result := a else Result := b;
<br>
end;
<br>
<br>
function Max(a, b: Single): Single; overload;
<br>
begin
<br>
WriteLn('Single');
<br>
if a > b then Result := a else Result := b;
<br>
end;
<br>
<br>
var
<br>
v1: Double;
<br>
v2: Single;
<br>
begin
<br>
v1 := Pi;
<br>
v2 := 0;
<br>
WriteLn(v1);
<br>
WriteLn(Max(v1,0));
<br>
WriteLn(Max(v1,0.0));
<br>
WriteLn(Max(v1,v2));
<br>
end.
<br>
<br>
Prints:
<br>
3.1415926535897931E+000
<br>
Single
<br>
3.141592741E+00
<br>
Double
<br>
3.1415926535897931E+000
<br>
Double
<br>
3.1415926535897931E+000
<br>
<br>
If this is not a bug, it would be very helpful if the compiler
could print a warning whenever a value is implicitly converted
from double to single.
<br>
</blockquote>
Well, pascal is a hard typed language, but not that hard in
numeric issues. I think it is a little inconsistent that it
implicitly converts '0.0' to double but '0 to single.
<br>
<br>
Nevertheless, I think it is a bug. It doesn't choose the right
overloaded function
<br>
<br>
But the main is this:
<br>
you have several overload options for max
<br>
1 extended, extended
<br>
2 double, double
<br>
3 single, single
<br>
4 int64, int64
<br>
5 integer, integer
<br>
<br>
When it finds (double, single), why does it choose (single,
single) instead of (double, double)?
<br>
The natural behavior should be to widen to the greater parameter,
like it does in expressions.
<br>
<br>
</blockquote>
<br>
</body>
</html>