Re: [fpc-devel] wrong result for abs(low(int64))

2024-04-04 Thread Bart via fpc-devel
On Thu, Apr 4, 2024 at 9:49 PM 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.

See https://gitlab.com/freepascal.org/fpc/source/-/issues/40694
There were several commits about Abs w.r.t. this bug lately.
-- 
Bart
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] wrong result for abs(low(int64))

2024-04-04 Thread Martin Frb via fpc-devel

On 04/04/2024 16:39, J. Gareth Moreton via fpc-devel wrote:
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.


I got that bit...

Initially I was surprised to find "abs" to return a signed number. After 
all, it will never be negative.


But, in the meantime I recalled that unsigned numbers will cause other 
issues when used in expressions.

"abs(-1)-2" would behave different.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] wrong result for abs(low(int64))

2024-04-04 Thread J. Gareth Moreton via fpc-devel
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@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] wrong result for abs(low(int64))

2024-04-04 Thread Kai Burghardt via fpc-devel
Ahoy, hoy,

On 2024‑04‑04 15:14:38 +0200, Martin Frb via fpc-devel wrote:
> The below writes:  -9223372036854775808
> 
> Shouldn't absolute return a positive number?

No, see https://wiki.freepascal.org/Integer#characteristics
-- 
Sincerely yours,
Kai Burghardt


signature.asc
Description: PGP signature
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


[fpc-devel] wrong result for abs(low(int64))

2024-04-04 Thread Martin Frb via fpc-devel

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@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel