Recent

Author Topic: Is this a bug in the conversion by FPC?  (Read 450 times)

dcoun

  • New member
  • *
  • Posts: 7
Is this a bug in the conversion by FPC?
« on: August 11, 2024, 05:22:46 pm »
Running the following code in Delphi 12 results to an exception. In FPC 3.2 gives a negative number.
Code: Pascal  [Select][+][-]
  1. var m:integer; n:cardinal;
  2. begin n:=high(cardinal);
  3.   m:=n;
  4.   if m<0 then showmessage('negative');
  5. end;

The following code runs without exceptions and some results in both Delphi 12 & FPC 3.2
Code: Pascal  [Select][+][-]
  1. var m:integer; n:cardinal;
  2. begin n:=high(cardinal);
  3.   m:=integer(n);
  4.   if m<0 then showmessage('negative');
  5. end;

To my way of thinking Delphi does it well. A Cardinal can not always fit to an integer. To force it to fit in the memory, it has now an other number that it is negative (-1).
In the second case, the programmer wants a conversion and is aware of the possible results.

What is your opinion?

korba812

  • Sr. Member
  • ****
  • Posts: 417
Re: Is this a bug in the conversion by FPC?
« Reply #1 on: August 11, 2024, 05:32:31 pm »
Maybe range checking is enabled by default in your Delphi? Enabling range checking in FPC also causes an exception in this case.
https://www.freepascal.org/docs-html/3.2.0/prog/progsu65.html

dcoun

  • New member
  • *
  • Posts: 7
Re: Is this a bug in the conversion by FPC?
« Reply #2 on: August 11, 2024, 05:38:15 pm »
Maybe range checking is enabled by default in your Delphi? Enabling range checking in FPC also causes an exception in this case.
https://www.freepascal.org/docs-html/3.2.0/prog/progsu65.html
Running it from the IDE has range checking enabled. That it is not the problem.
The question if this is expected or not in conversion

jamie

  • Hero Member
  • *****
  • Posts: 6587
Re: Is this a bug in the conversion by FPC?
« Reply #3 on: August 11, 2024, 05:40:58 pm »
I believe both integer and cardinal are the same size, ones an unsigned 32 bit while the other uses the MSB as the sign bit
so, both will interchange memory wise.
The only true wisdom is knowing you know nothing

dcoun

  • New member
  • *
  • Posts: 7
Re: Is this a bug in the conversion by FPC?
« Reply #4 on: August 11, 2024, 05:42:54 pm »
Maybe range checking is enabled by default in your Delphi? Enabling range checking in FPC also causes an exception in this case.
https://www.freepascal.org/docs-html/3.2.0/prog/progsu65.html
Enabling the {$R+} I have the exception as in delphi. You have right.
So, it is as expected.

jamie

  • Hero Member
  • *****
  • Posts: 6587
Re: Is this a bug in the conversion by FPC?
« Reply #5 on: August 11, 2024, 05:47:29 pm »
as a note, it only raises it as a runtime error, meaning, if the value exceeds the positive range of the integer when range is on.
The only true wisdom is knowing you know nothing

VisualLab

  • Sr. Member
  • ****
  • Posts: 499
Re: Is this a bug in the conversion by FPC?
« Reply #6 on: August 11, 2024, 05:49:32 pm »
I believe both integer and cardinal are the same size, ones an unsigned 32 bit while the other uses the MSB as the sign bit
so, both will interchange memory wise.

And aren't integers represented rather in 2's complement code?

https://en.wikipedia.org/wiki/Two%27s_complement

dcoun

  • New member
  • *
  • Posts: 7
Re: Is this a bug in the conversion by FPC?
« Reply #7 on: August 11, 2024, 05:50:20 pm »
as a note, it only raises it as a runtime error, meaning, if the value exceeds the positive range of the integer when range is on.
You have right, it is as expected.

 

TinyPortal © 2005-2018