Recent

Author Topic: Odd range check warnings  (Read 1043 times)

RememberTP

  • New Member
  • *
  • Posts: 20
Odd range check warnings
« on: June 13, 2024, 09:01:39 pm »
When I compile the following..

Code: Pascal  [Select][+][-]
  1. program canIReproduceThis;
  2.  
  3. {$R-}
  4.  
  5. const
  6.   grand = 1000;
  7.  
  8. function xxx(i: word): integer;
  9. var
  10.   n: word;
  11. begin
  12.   n := 1;
  13.   result := -grand + (n + i);  (* produces warning?? *)
  14. end;
  15.  
  16. function yyy(i: word): integer;
  17. var
  18.   n: word;
  19. begin
  20.   n := 1;
  21.   exit(-grand + (n + i));      (* produces warning?? *)
  22. end;
  23.  
  24. function zzz(i: word): integer;
  25. var
  26.   n: word;
  27. begin
  28.   n := 1;
  29.   zzz := -grand + (n + i);     (* produces warning?? *)
  30. end;
  31.  
  32. function aaa(i: word): integer;
  33. var
  34.   n: word;
  35. begin
  36.   n := 1 + i;
  37.   result := -grand + (n);      (* no warning?? *)
  38. end;
  39.  
  40. function bbb(i: word): integer;
  41. var
  42.   n: word;
  43. begin
  44.   n := 1;
  45.   i := i + n ;
  46.   result := -grand + (i);      (* no warning?? *)
  47. end;
  48.  
  49. function ccc(i: word): integer;
  50. var
  51.   n: word;
  52. begin
  53.   n := 1;
  54.   result := -grand + n + i;    (* no warning?? *)
  55. end;
  56.  
  57. function ddd(i: word): integer;
  58. var
  59.   n: word;
  60. begin
  61.   n := 1;
  62.   result := -grand + (n) + (i);    (* no warning?? *)
  63. end;
  64.  
  65. begin
  66.   writeln(xxx(1));
  67.  
  68.   writeln(yyy(1));
  69.  
  70.   writeln(zzz(1));
  71.  
  72.   writeln(aaa(1));
  73.  
  74.   writeln(bbb(1));
  75.  
  76.   writeln(ccc(1));
  77.  
  78.   writeln(ddd(1));
  79. end.

Code: Pascal  [Select][+][-]
  1. $ fpc -S2 cirt.pas
  2. Free Pascal Compiler version 3.2.2 [2021/07/09] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling cirt.pas
  6. fpc-poss-bug.pas(13,13) Warning: range check error while evaluating constants (-1000 must be between 0 and 18446744073709551615)
  7. fpc-poss-bug.pas(21,8) Warning: range check error while evaluating constants (-1000 must be between 0 and 18446744073709551615)
  8. fpc-poss-bug.pas(29,10) Warning: range check error while evaluating constants (-1000 must be between 0 and 18446744073709551615)
  9. Linking fpc-poss-bug
  10. 79 lines compiled, 0.1 sec
  11. 3 warning(s) issued

Why the warnings?

If I have range-checking on, they're fatal errors.

RememberTP

  • New Member
  • *
  • Posts: 20
Re: Odd range check warnings
« Reply #1 on: June 13, 2024, 09:04:25 pm »
The program that this is boiled down from has a slightly more complicated expression for the result - the parentheses are there for my benefit to make sure I have the operator precedence right!

Joanna

  • Hero Member
  • *****
  • Posts: 869
Re: Odd range check warnings
« Reply #2 on: June 13, 2024, 09:14:23 pm »
Word type is for positive numbers only.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

RememberTP

  • New Member
  • *
  • Posts: 20
Re: Odd range check warnings
« Reply #3 on: June 13, 2024, 09:15:11 pm »
Ah, it works if I cast the problematic bit of the expression to be an integer, but it's odd (to me) that the compiler adds two words and then assumes the whole expression must be type word unless I do this.

Code: Pascal  [Select][+][-]
  1. function www(i: word): integer;
  2. var
  3.   n: word;
  4. begin
  5.   n := 1;
  6.   result := -grand + integer(n + i);  (* no warning?? *)
  7. end;

RememberTP

  • New Member
  • *
  • Posts: 20
Re: Odd range check warnings
« Reply #4 on: June 13, 2024, 09:17:36 pm »
Word type is for positive numbers only.

Mmm, yes indeed, but the function for which this is a result returns an integer.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5585
  • Compiler Developer
Re: Odd range check warnings
« Reply #5 on: June 13, 2024, 09:32:48 pm »
Ah, it works if I cast the problematic bit of the expression to be an integer, but it's odd (to me) that the compiler adds two words and then assumes the whole expression must be type word unless I do this.

Of course it assumes the whole expression is unsigned, because grand does not have a type and both n and i are unsigned. The result of an expression does not depend on the result type, only on the involved parts.

RememberTP

  • New Member
  • *
  • Posts: 20
Re: Odd range check warnings
« Reply #6 on: June 14, 2024, 12:11:18 pm »
Yes, constants don't have a type, but if you 'type' a constant, it's no longer a constant but an initialised variable. (Thank you, Turbo Pascal...)

it's interesting to me that

Code: Pascal  [Select][+][-]
  1.   result := (n + i) -1000;

is fine, even when it's known at compile time that both n and i = 1.

(And I still think the compiler should realise that the end result is an integer expression and not jump to conclusions just because evaluation of it starts with two words.)

Joanna

  • Hero Member
  • *****
  • Posts: 869
Re: Odd range check warnings
« Reply #7 on: June 14, 2024, 03:11:49 pm »
I’m curious what kind of programming problem you are trying to solve ?Maybe it would be better to use integer, it is More versetile.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

RememberTP

  • New Member
  • *
  • Posts: 20
Re: Odd range check warnings
« Reply #8 on: June 14, 2024, 07:40:37 pm »
The function is working out if something is good (+ve result) or bad (-ve result) so returns an integer. But the things it uses to work that out should all be positive.

If they're not, something's gone wrong so I'd like range checking to catch that, hence having them as words.

Does that make sense?

MarkMLl

  • Hero Member
  • *****
  • Posts: 7114
Re: Odd range check warnings
« Reply #9 on: June 14, 2024, 08:19:34 pm »
What if the result is zero?

I suggest that if you want a go/nogo result then you should be using a Boolean, and that would give you scope for using a larger variable type (or for that matter a log scale) internally.

FPC constants default to Int64, and IMO it would be better practice to finish of with an explicit comparison than by trying to base the returned value on casts.

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

 

TinyPortal © 2005-2018