[fpc-pascal] Loss of precision when using math.Max()

Santiago A. svaa at ciberpiula.net
Mon Jul 2 10:22:15 CEST 2018


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.

-- 
Saludos

Santiago A.




More information about the fpc-pascal mailing list