[fpc-pascal] Loss of precision when using math.Max()
Vojtěch Čihák
vojtech.cihak at atlas.cz
Tue Jul 3 18:43:49 CEST 2018
Hi,
I agree with the original topic that overload function Math.Max(Double, Double) shoud be chosen instead of Math.Max(Single, Single) when one of parameters is Integer.
But your criticism make no sense to me. You can as well point that this code:
var a, b: Byte;
begin
a:=5;
b:=a div 2;
writeln(b); // result: 2
end;
will not give any warning even if correct result is 2.5.
It is simply absurd because it is not about shooting your own foot. Compiler is not a crystal ball, it does what you tell him.
If you need floating point, use floating point types and floating point division (my example) and if you need signed results, use signed types (your example).
Vojtěch
______________________________________________________________
> Od: Wolf <wv99999 at gmail.com>
> Komu: fpc-pascal at lists.freepascal.org
> Datum: 03.07.2018 14:33
> Předmět: Re: [fpc-pascal] Loss of precision when using math.Max()
>
On 03/07/2018 11:26, Jim Lee wrote:
On 07/02/18 15:13, Wolf wrote: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
Should the compiler balk at this as well?
program Project1;
var a,b,c: byte;
begin
a:=5;
b:=6;
c:=a-b;
writeln(c); // result: 255
end.
Without the implicit conversion of signed/unsigned values, the utility of the language is greatly diminished.
-Jim
_______________________________________________fpc-pascal maillist - fpc-pascal at lists.freepascal.org <fpc-pascal at lists.freepascal.org>http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>
----------
_______________________________________________
fpc-pascal maillist - fpc-pascal at lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180703/9cf401ad/attachment.html>
More information about the fpc-pascal
mailing list