Recent

Author Topic: Round invalid FLT/ invalid floating point operation  (Read 569 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 343
Round invalid FLT/ invalid floating point operation
« on: January 26, 2025, 10:10:24 am »
Code: Pascal  [Select][+][-]
  1. var
  2.    y:  ValReal;
  3. begin
  4.    showmessage(floattostr(y));//2.15494916873971E33
  5.    showmessage(IntToStr(Round(y)));//error  
  6.  

How to handle this ?


lazarus 3.2-fpc-3.2.2-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2374
Re: Round invalid FLT/ invalid floating point operation
« Reply #1 on: January 26, 2025, 10:26:02 am »
Code: Pascal  [Select][+][-]
  1. var
  2.    y:  ValReal;
  3. begin
  4.    showmessage(floattostr(y));//2.15494916873971E33
  5.    showmessage(IntToStr(Round(y)));//error  
  6.  
How to handle this ?
May be y := 2.15494916873971E33 before?
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$IFDEF WINDOWS}
  3.   {$APPTYPE CONSOLE}
  4. {$ENDIF}
  5.  
  6. uses SysUtils;
  7.  
  8. var
  9.   Y: ValReal;
  10.   Q: Int64;
  11. begin
  12.   Y := 2.15494916873971E33;
  13.   Writeln(FloatToStr(Y));
  14.   try
  15.     Q := Round(Y); // Error, because 10^33 > 2^64
  16.     Writeln(IntToStr(Q));
  17.   except
  18.     on E: Exception do
  19.       Writeln('Error: class: ', E.ClassName, ', message: ', E.Message);
  20.   end;
  21.   Readln;
  22. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Round invalid FLT/ invalid floating point operation
« Reply #2 on: January 26, 2025, 12:31:43 pm »
maybe a 32 bit compile helps, because there valreal is 2^80, real extended.
on 64 bit the error is correct, because there valreal is double.
But I am sure they don't want the Trumps back...

wp

  • Hero Member
  • *****
  • Posts: 12579
Re: Round invalid FLT/ invalid floating point operation
« Reply #3 on: January 26, 2025, 12:44:37 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.    y:  ValReal;
  3. begin
  4.    showmessage(floattostr(y));//2.15494916873971E33
  5.    showmessage(IntToStr(Round(y)));//error  
  6.  
Reading the "Round" here in a string-conversion I assume that you only want to get a string without the decimal places. You can use the Format instructions with appropriate mask definition specifying zero decimals:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   SysUtils;
  4. var
  5.   y: Double;
  6. begin
  7.   y := 2.15494916873971E33;
  8.   WriteLn(Format('%.0f', [y]));
  9.   WriteLn(Format('%.0n', [y]));
  10.   Readln;
  11. end.  

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Round invalid FLT/ invalid floating point operation
« Reply #4 on: January 26, 2025, 04:36:50 pm »
or simply:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var
  3.   i:valreal = 2.15494916873971E33;
  4. begin
  5.   writeln(i:0:0);
  6. end.
outputs 2154949168739709900000000000000000 without exception and length 34 on a 64/64 windows system. The error margin is in de expected range for the lsb (100 vs 099, one bit off)

If you really need the string, this also works with writestr.
« Last Edit: January 26, 2025, 04:58:42 pm by Thaddy »
But I am sure they don't want the Trumps back...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5851
  • Compiler Developer
Re: Round invalid FLT/ invalid floating point operation
« Reply #5 on: January 26, 2025, 05:06:01 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.    y:  ValReal;
  3. begin
  4.    showmessage(floattostr(y));//2.15494916873971E33
  5.    showmessage(IntToStr(Round(y)));//error  
  6.  

How to handle this ?

2.15 * 10^33 is simply too large to be represented as an integer type. The maximum value that can be returned by Round is High(Int64) (because Round returns a signed type) and as a floating point value that is 9.22 * 10^18.

 

TinyPortal © 2005-2018