Recent

Author Topic: Implicit converison, literals and QWORD variable warning  (Read 10132 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Implicit converison, literals and QWORD variable warning
« Reply #60 on: August 09, 2020, 11:26:03 pm »
Try

Code: Pascal  [Select][+][-]
  1.    procedure something (a:int64;b:uint64);
  2.    begin
  3.     writeln(a+b);
  4.    end;
  5.  

And then for varying (small and large, negative and positive) a and b.

And testing $FFFFFFFFFFFFFFFF with VAL() of course
« Last Edit: August 09, 2020, 11:37:11 pm by marcov »

emailx45

  • New member
  • *
  • Posts: 8
Re: Implicit converison, literals and QWORD variable warning
« Reply #61 on: August 09, 2020, 11:42:52 pm »
Try

Code: Pascal  [Select][+][-]
  1.    procedure something (a:int64;b:uint64);
  2.    begin
  3.     writeln(a+b);
  4.    end;
  5.  

And then for varying (small and large, negative and positive) a and b.

And testing $FFFFFFFFFFFFFFFF with VAL() of course

Int64 (Negative and Positive) / UInt64 accept only POSITIVE values = if negative resulted violate resulted accept

Quote
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.   System.SysUtils;
  8.  
  9. procedure something(a: int64; b: uint64);
  10. begin
  11.   writeln(a + b);
  12. end;
  13.  
  14. begin
  15.   try
  16.     writeln('');
  17.     something(1, 1);
  18.     something(int64.MaxValue, uint64.MaxValue);
  19.     readln;
  20.   except
  21.     on E: Exception do
  22.       writeln(E.ClassName, ': ', E.Message);
  23.   end;
  24.  
  25. end.
  26.  

READ the message by compile about using Assigned and UnAssigned types on bottom of screenshots

Dont exist  -UInt64 values

[dcc32 Warning] Project1.dpr(11): W1073 Combining signed type and unsigned 64-bit type - treated as an unsigned type

[dcc64 Warning] Project1.dpr(11): W1073 Combining signed type and unsigned 64-bit type - treated as an unsigned type
« Last Edit: August 10, 2020, 12:14:23 am by emailx45 »

emailx45

  • New member
  • *
  • Posts: 8
Re: Implicit converison, literals and QWORD variable warning
« Reply #62 on: August 10, 2020, 12:45:29 am »
Try
And testing $FFFFFFFFFFFFFFFF with VAL() of course

VAL() function of System.pas
Quote
Code: Pascal  [Select][+][-]
  1. Converts a string that represents an integer (decimal or hex notation) into a number.
  2.  
  3. In Delphi code, Val converts the string value S to its numeric representation,
  4. as if it were read from a text file with Read. Both 0$1234 and 0x1234 are
  5. the hexadecimal notations supported.
  6.  
  7. S is a string-type expression; it must be a sequence of characters that form
  8. a signed real number, such as "1", "-2" or "+3". Other than the optional sign
  9. at the beginning, all characters must be digits; decimal or thousands
  10. separators are not supported.
  11.  
  12. V is an integer-type or real-type variable. If V is an integer-type variable, S
  13. must form a whole number.
  14.  
  15. Code is a variable of type Integer.
  16.  
  17. If the string is invalid, the index of the offending character is stored in Code;
  18. otherwise, Code is set to zero. For a null-terminated string, the error
  19. position returned in Code is one larger than the actual zero-based index of
  20. the character in error.
  21.  

RAD STUDIO 10.3.3  Rio or RAD 10.4 Sydney same procedure
no debugging
no range checking


In all case tested, the values was:
S  $FFFFFFFFFFFFFFFF
V  18446744073709551615
Code   0


Quote
Code: Pascal  [Select][+][-]
  1. program Project2;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.   System.SysUtils;
  8.  
  9. var
  10.   s   : string;
  11.   V   : UInt64; // my choice
  12.   Code: Integer; // should be Interger
  13.  
  14. begin
  15.   try
  16.     writeln('');
  17.     writeln('');
  18.     s := '$FFFFFFFFFFFFFFFF'; // should be Int64 type
  19.     val(s, V, Code);
  20.     writeln('s := $FFFFFFFFFFFFFFFF - val(s, V, Code);');
  21.     writeln('');
  22.     writeln(V);
  23.     readln;
  24.   except
  25.     on E: Exception do
  26.       writeln(E.ClassName, ': ', E.Message);
  27.   end;
  28.  
  29. end.
  30.  
« Last Edit: August 10, 2020, 12:53:38 am by emailx45 »

FPK

  • Full Member
  • ***
  • Posts: 118
Re: Implicit converison, literals and QWORD variable warning
« Reply #63 on: August 11, 2020, 08:13:04 pm »

Code: Pascal  [Select][+][-]
  1. program Project2;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.   System.SysUtils;
  8.  
  9. var
  10.   s   : string;
  11.   V   : UInt64; // my choice
  12.   Code: Integer; // should be Interger
  13.  
  14. begin
  15.   try
  16.     writeln('');
  17.     writeln('');
  18.     s := '$FFFFFFFFFFFFFFFF'; // should be Int64 type
  19.     val(s, V, Code);
  20.     writeln('s := $FFFFFFFFFFFFFFFF - val(s, V, Code);');
  21.     writeln('');
  22.     writeln(V);
  23.     readln;
  24.   except
  25.     on E: Exception do
  26.       writeln(E.ClassName, ': ', E.Message);
  27.   end;
  28.  
  29. end.
  30.  

It must be int64 of course! The "root" of the "problem" is that FPC's val (not StrToInt, not any thing else but val) for int64 (!) accepts $ffffffffffffffff. If it does not, the compiler will not read $ffffffffffffffff as int64. Of course, changing this will probably open pandoras box and probably break a lot of code, but this is how it could be changed. Everything else is a hack.

nanobit

  • Full Member
  • ***
  • Posts: 154
Re: Implicit converison, literals and QWORD variable warning
« Reply #64 on: August 11, 2020, 09:38:14 pm »
Delphi val( '$ffffffffffffffff', intVar, code)
just behaves like overloaded functions:
1) If intVar has type int64, then intVar becomes -1,
because val() implements implicit typecast from uint64 input to int64.
2) If intVar has type uint64, then intVar becomes high(uint64).
3) Both get (code = 0).

It's like in Delphi assignment:
i64 := -$1;  // fully compatible
i64 := $ffffffffffffffff; // implicit cast with out-of-range warning, but -1 still is assigned
u64 := $ffffffffffffffff; // fully compatible

I mentioned the Delphi implicit typecast in:
https://forum.lazarus.freepascal.org/index.php/topic,50968.0.html

FPK

  • Full Member
  • ***
  • Posts: 118
Re: Implicit converison, literals and QWORD variable warning
« Reply #65 on: August 11, 2020, 10:27:06 pm »
Delphi val( '$ffffffffffffffff', intVar, code)
just behaves like overloaded functions:
1) If intVar has type int64, then intVar becomes -1,
because val() implements implicit typecast from uint64 input to int64.

Whatever it does, it accepts $ffffffffffffffff as a valid int64.

 

TinyPortal © 2005-2018