Recent

Author Topic: Warning: range check error while evaluating constants  (Read 638 times)

domasz

  • Sr. Member
  • ****
  • Posts: 413
Warning: range check error while evaluating constants
« on: November 27, 2022, 12:45:57 pm »
Interesting warning:
Code: Pascal  [Select][+][-]
  1. Warning: range check error while evaluating constants (-4942790177534073029 must be between 0 and 18446744073709551615)

The code
Code: Pascal  [Select][+][-]
  1. var Hash: QWord;
  2. begin
  3. Hash := $BB67AE8584CAA73B;

Message is gone when I change QWord to Int64.
Does it mean Lazarus handles Int64s better than QWords?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Warning: range check error while evaluating constants
« Reply #1 on: November 27, 2022, 12:54:26 pm »
No, it means that the compiler's default representation of 64-bit constants is signed.

Use

Code: Pascal  [Select][+][-]
  1.     var Hash: QWord;
  2.     begin
  3.     Hash := QWord($BB67AE8584CAA73B);
  4.  

...or for that matter

Code: Pascal  [Select][+][-]
  1.     var Hash: QWord=  QWord($BB67AE8584CAA73B);
  2.     begin
  3.  

...and so on.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Warning: range check error while evaluating constants
« Reply #2 on: November 27, 2022, 12:59:16 pm »
Untyped constants are always signed.
Specialize a type, not a var.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: Warning: range check error while evaluating constants
« Reply #3 on: November 27, 2022, 01:04:08 pm »
Untyped constants are always signed.

Yes, although in most cases the signed type is large enough to hold the number concerned. It's just this case which is the problem, since there is no standard integer type larger than 64 bits.

Considering another facet of OP's question, qwords are handled well and (subject to endianness concerns) are entirely adequate for bit-twiddling etc., but there might be one or two conversion routines which aren't implemented for the larger unsigneds.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

domasz

  • Sr. Member
  • ****
  • Posts: 413
Re: Warning: range check error while evaluating constants
« Reply #4 on: November 27, 2022, 01:04:44 pm »
Thank you both!


Code: Pascal  [Select][+][-]
  1.     var Hash: QWord=  QWord($BB67AE8584CAA73B);
  2.     begin
  3.  

MarkMLl

Is this new in Lazarus? In Delphi (at least the old versions I used) you can't define value of a variable in the var section.

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Warning: range check error while evaluating constants
« Reply #5 on: November 27, 2022, 01:06:57 pm »
It is introduced in later Delphi versions and adopted by Freepascal. Note that untyped consts are always signed, though. That has not changed a single bit.
Delphi has very new versions that are free for personal use, btw.
So don't make comparisons between Freepascal and Delphi unless you are current.
Properly phrased inconsistencies between the two compilers will always have the attention of the compiler developers. (the reverse is also true: Delphi developers follow Freepascal)
« Last Edit: November 27, 2022, 01:14:45 pm by Thaddy »
Specialize a type, not a var.

domasz

  • Sr. Member
  • ****
  • Posts: 413
Re: Warning: range check error while evaluating constants
« Reply #6 on: November 27, 2022, 01:11:39 pm »
Great, thanks!

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Warning: range check error while evaluating constants
« Reply #7 on: November 27, 2022, 01:45:20 pm »
Delphi has very new versions that are free for personal use, btw.
Yes. This is the "Community edition". You must register and get a free license for one year. After that year, the license always has been renewed for me without any problems. But of course, Embarcadero could change this policy at any time, you thus should not base a serious project on this, but for testing or comparing with FPC/Lazarus the Community Edition is perfect.
« Last Edit: November 27, 2022, 01:58:23 pm by wp »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Warning: range check error while evaluating constants
« Reply #8 on: November 27, 2022, 01:54:01 pm »

Yes. This is the "Community edition". You must register and get a license for one year. After that year, the license always has been renewed for me without any problems. But of course, Embarcadero could change this at any time, you thus should not base a serious project on this, but for testing or comparing with FPC/Lazarus the Community Editition is perfect.

Hi!

Use the good old Turbo Delphi (2006 .. 2010). The register key is valid for 100 years.
Not vailable anymore.

Winni

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Warning: range check error while evaluating constants
« Reply #9 on: November 27, 2022, 09:45:53 pm »
It is introduced in later Delphi versions and adopted by Freepascal.

From the code posted by domasz it's not clear whether this is about a global or a local var section. Both are supported in FPC for a very long time already. I don't know when Delphi added the former (probably something like Delphi 4 or maybe even 2 already) and the later was only added in recent versions with the introduction of the inline variables.

 

TinyPortal © 2005-2018