Recent

Author Topic: Having problems assigning Integer types to Single  (Read 946 times)

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Having problems assigning Integer types to Single
« on: December 12, 2023, 12:25:02 am »
I'm having problems assigning Integer types to Single.
In the 32bit compiler it won't allow single:= int32, but will allow single:= int64.
In the 64bit compiler it won't allow single:= int64, but will allow single:= int32.
I'm puzzled. Ignoring the inconsistency...
Is there a "correct" way to assign an integer type to a floating point type?
I know there is a restriction was the other way round,  integer:= real, requires trunc(real) or round(real).
But my understanding is that real:=integer should be allowed.

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: Having problems assigning Integer types to Single
« Reply #1 on: December 12, 2023, 02:03:01 am »
I don't see it in 3.0.4 FPC. Didn't try anything different.
The only true wisdom is knowing you know nothing

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
Re: Having problems assigning Integer types to Single
« Reply #2 on: December 12, 2023, 02:26:13 am »
Works for me without any issues:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. var
  4.   S: Single;
  5.   I8: Int8 = 114;
  6.   I16: Int16 = 31634;
  7.   I32: Int32 = 93258345;
  8.   I64: Int64 = 436438693246;
  9. begin
  10.   S := I8;
  11.   WriteLn(S);
  12.   S := I16;
  13.   WriteLn(S);
  14.   S := I32;
  15.   WriteLn(S);
  16.   S := I64;
  17.   WriteLn(S);
  18.   ReadLn;
  19. end.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Nitorami

  • Hero Member
  • *****
  • Posts: 507
Re: Having problems assigning Integer types to Single
« Reply #3 on: December 12, 2023, 09:23:17 am »
I cannot reproduce this with FPC 3.2.2 (32bit, windows), and this would indeed be VERY odd.
Can you reproduce this when you remove all own units from the uses clause ? Just in case you have defined an implicit operator which converts your int to something else before you try to convert it to single.

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: Having problems assigning Integer types to Single
« Reply #4 on: December 12, 2023, 10:04:24 am »
Thanks for your replies.
Yes, I have got lots of implicit type conversion and operator overloading going on, although I don't think it should be kicking-in.
I'll try again with a very simple piece of code.
But you've answered my question that, in theory, it should work ok.

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: Having problems assigning Integer types to Single
« Reply #5 on: December 12, 2023, 10:05:44 am »
Works for me without any issues:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. var
  4.   S: Single;
  5.   I8: Int8 = 114;
  6.   I16: Int16 = 31634;
  7.   I32: Int32 = 93258345;
  8.   I64: Int64 = 436438693246;
  9. begin
  10.   S := I8;
  11.   WriteLn(S);
  12.   S := I16;
  13.   WriteLn(S);
  14.   S := I32;
  15.   WriteLn(S);
  16.   S := I64;
  17.   WriteLn(S);
  18.   ReadLn;
  19. end.
I should have tried this for myself before posting my question   :o

dseligo

  • Hero Member
  • *****
  • Posts: 1449
Re: Having problems assigning Integer types to Single
« Reply #6 on: December 12, 2023, 10:55:15 am »
I should have tried this for myself before posting my question   :o
:)

And if it doesn't work post your test code to forum for others to try.

 

TinyPortal © 2005-2018