Not so long ago, Florian was proudly bragging about "Pascal does not
allow you to shoot yourself in the foot
<http://www.toodarkpark.org/computers/humor/shoot-self-in-foot.html>"
What about this little program:
program Project1;
var a,b: byte;
begin
a:=1;
b:=a*(-1);
writeln(b); // result: 255
end.
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.
Who is shooting whom in the foot?
Wolf
On 02/07/2018 20:22, Santiago A. wrote:
El 01/07/2018 a las 10:27, C Western escribió:
On 29/06/18 21:55, Sven Barth via fpc-pascal wrote:
More confusingly, if a single variable is used, the expected
Max(Double, Double) is called:
function Max(a, b: Double): Double; overload;
begin
WriteLn('Double');
if a > b then Result := a else Result := b;
end;
function Max(a, b: Single): Single; overload;
begin
WriteLn('Single');
if a > b then Result := a else Result := b;
end;
var
v1: Double;
v2: Single;
begin
v1 := Pi;
v2 := 0;
WriteLn(v1);
WriteLn(Max(v1,0));
WriteLn(Max(v1,0.0));
WriteLn(Max(v1,v2));
end.
Prints:
3.1415926535897931E+000
Single
3.141592741E+00
Double
3.1415926535897931E+000
Double
3.1415926535897931E+000
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.
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.
Nevertheless, I think it is a bug. It doesn't choose the right
overloaded function
But the main is this:
you have several overload options for max
1 extended, extended
2 double, double
3 single, single
4 int64, int64
5 integer, integer
When it finds (double, single), why does it choose (single, single)
instead of (double, double)?
The natural behavior should be to widen to the greater parameter, like
it does in expressions.
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal