[fpc-devel] wrong result for abs(low(int64))
    J. Gareth Moreton 
    gareth at moreton-family.com
       
    Thu Apr  4 16:39:53 CEST 2024
    
    
  
Essentially, an arithmetic overflow is happening.  Since the largest 
Int64 possible is 9,223,372,036,853,775,807, going one above that (the 
result to abs(low(int64))) wraps back around to -9,223,372,036,853,775,808.
Internally, you can think about negating (positing?) a negative number 
as inverting the bits and then adding one (aka. two's complement), so 
low(int64) is 1000...000, which inverted becomes 0111...111, and then 
adding one results in 1000...000 again.
Kit
On 04/04/2024 14:14, Martin Frb via fpc-devel wrote:
> The below writes:  -9223372036854775808
>
> Shouldn't absolute return a positive number?
>
> program Project1;
> begin
>   writeln( abs(low(int64)) );
> end.
>
>
>
> Seems
> writeln( abs(low(longint)) );
>
> also returns negative...
>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
    
    
More information about the fpc-devel
mailing list