* * *

Author Topic: Missing IsInfinite function for extended  (Read 599 times)

Gammatester

  • New member
  • *
  • Posts: 28
Missing IsInfinite function for extended
« on: May 27, 2018, 09:48:05 pm »
Why is there no IsInfinite function for extended? (Like IsNan, which is implemented for single, double, extended). The following function for Win32 for i386 crashes:
Code: Pascal  [Select]
  1. program isinfx;
  2. uses
  3.   math;
  4. var
  5.   x: extended;
  6. begin
  7.   x := 1e2345;
  8.   writeln(IsInfinite(x));
  9. end.
Here the output
Code: [Select]
C:\TMP>D:\FPC304\bin\i386-win32\fpc.exe isinfx.pas
Free Pascal Compiler version 3.0.4 [2017/10/06] for i386
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling isinfx.pas
Linking isinfx.exe
11 lines compiled, 0.2 sec, 64432 bytes code, 4132 bytes data

C:\TMP>isinfx.exe
An unhandled exception occurred at $0040156D:
EOverflow: Floating point overflow
  $0040156D
There is no such problem for singles because single-inf is compatible with double-inf.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 778
    • Burdjia
Re: Missing IsInfinite function for extended
« Reply #1 on: May 28, 2018, 01:47:05 pm »
Are you sure the exception is raised in line 8?  May be it is raised in line 7.  ::)

Anyway I've tested your code and it worked (FPC 3.0.0 + Xubuntu 16.04.4).
« Last Edit: May 28, 2018, 01:48:46 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Gammatester

  • New member
  • *
  • Posts: 28
Re: Missing IsInfinite function for extended
« Reply #2 on: May 28, 2018, 02:06:29 pm »
Yes I am sure. If you change the source to
Code: Pascal  [Select]
  1. program isinfx;
  2. uses
  3.   math;
  4. var
  5.   x: extended;
  6. begin
  7.   x := 1e2345;
  8.   writeln('Value of x:', x);
  9.   writeln('No crash up to now');
  10.   writeln(IsInfinite(x));
  11. end.
You get
Code: [Select]
Value of x: 9.99999999999999999980E+2344
No crash up to now
An unhandled exception occurred at $004015ED:
EOverflow: Floating point overflow
  $004015ED
and no  crash if you omit the IsInfinite. The reason is clear: IsInfinite expects a double and  x (=1e2345) cannot converted to double.
Anyway I've tested your code and it worked (FPC 3.0.0 + Xubuntu 16.04.4).
What does this mean? Do you get an EOverflow as decribed? Otherwise do you compile a 64-bit-Exe? Then there are no 10-Bytes-Extended and the `x := 1e2345;` may crash, I get x=inf for 64-bit.
« Last Edit: May 28, 2018, 02:13:41 pm by Gammatester »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6238
Re: Missing IsInfinite function for extended
« Reply #3 on: May 28, 2018, 02:17:10 pm »
I tested with the 32-bit development version of FPC (3.1.1 a few days old), and indeed I got an exception.

What did help for me is the extended class helper in sysutils:

Code: Pascal  [Select]
  1.     program isinfx;
  2.     uses
  3.       sysutils;
  4.     var
  5.       x: extended;
  6.     begin
  7.       x := 1e2345;
  8.       writeln('Value of x:', x);
  9.       writeln('No crash up to now');
  10.       writeln(x.IsInfinity);
  11.     end.

Could be that that is a trunk feature though.
« Last Edit: May 28, 2018, 02:20:45 pm by marcov »

PascalDragon

  • Full Member
  • ***
  • Posts: 109
  • Compiler Developer
Re: Missing IsInfinite function for extended
« Reply #4 on: May 28, 2018, 05:36:02 pm »
Just in case, I've added overloads for Single and Extended for IsInfinite() (Delphi has them as well).

Gammatester

  • New member
  • *
  • Posts: 28
Re: Missing IsInfinite function for extended
« Reply #5 on: May 28, 2018, 06:14:15 pm »
Just in case, I've added overloads for Single and Extended for IsInfinite() (Delphi has them as well).
  8-) Thank you.

Kays

  • Full Member
  • ***
  • Posts: 112
  • Whasup!?
    • KaiBurghardt.de
Re: Missing IsInfinite function for extended
« Reply #6 on: May 28, 2018, 09:02:14 pm »
I've had a glance at isInfinite – barely understanding a word. Why don't/can't I just write equals infinity/negInfinity:
Code: Pascal  [Select]
  1. program infTest(input, output, stderr);
  2.  
  3. uses
  4.         math;
  5.  
  6. var
  7.         r: extended;
  8.  
  9. begin
  10.         r := 1e2345;
  11.         writeLn(r, ' = ∞ ⇔ ', r = infinity);
  12.         r := 1e23456;
  13.         writeLn(r, ' = ∞ ⇔ ', r = infinity);
  14. end.
  15.  
The output seems legit
Code: [Select]
9.99999999999999999980E+2344 = ∞ ⇔ FALSE
                        +Inf = ∞ ⇔ TRUE
Yours Sincerely
Kai Burghardt

Gammatester

  • New member
  • *
  • Posts: 28
Re: Missing IsInfinite function for extended
« Reply #7 on: May 28, 2018, 09:33:41 pm »
You don't handle -Infinity and may get problems  if  r is Nan. A somewhat better test would be abs(r)=Infinity.

Zoran

  • Hero Member
  • *****
  • Posts: 1235
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Missing IsInfinite function for extended
« Reply #8 on: May 28, 2018, 09:37:31 pm »
Could be that that is a trunk feature though.

No, it is not. I tested on Windows FPC 3.0.4 32-bit and your code with x.IsInfinity works correctly, while Math.IsInfinite(x) crashes.

Thaddy

  • Hero Member
  • *****
  • Posts: 6116
Re: Missing IsInfinite function for extended
« Reply #9 on: May 28, 2018, 09:38:07 pm »
There are some re-declarations in frivolous units like math.
Maybe these are not correct.
A very simple program would handle negative infinity.
I might not give the answer that you want me to.. Peter Green 1969

Kays

  • Full Member
  • ***
  • Posts: 112
  • Whasup!?
    • KaiBurghardt.de
Re: Missing IsInfinite function for extended
« Reply #10 on: May 28, 2018, 11:07:49 pm »
You don't handle -Infinity […]
Yeah, I know, I wrote
[…] infinity/negInfinity […]

[You] may get problems  if  r is Nan. […]
Uargh, indeed:
Code: Pascal  [Select]
  1. program infTest(input, output, stderr);
  2.  
  3. uses
  4.         math;
  5.  
  6. var
  7.         r: valReal;
  8.  
  9. begin
  10.         r := NaN;
  11.         writeLn(r = infinity);
  12. end.
will crash with
Code: [Select]
An unhandled exception occurred at $0000000000400212:
EInvalidOp: Invalid floating point operation
  $0000000000400212
Yikes. And exception handling is expensive, so doing something like
Code: Pascal  [Select]
  1. program infTest(input, output, stderr);
  2.  
  3. // for exception handling
  4. {$mode objFPC}
  5.  
  6. uses
  7.         // math for NaN, Infinity/NegInfinity
  8.         math,
  9.         // sysUtils for EInvalidOp
  10.         sysUtils;
  11.  
  12. function isInfinity(const r: valReal): longbool;
  13. begin
  14.         try
  15.                 {$push}
  16.                 {$boolEval off}
  17.                 isInfinity := (r = infinity) or (r = negInfinity);
  18.                 {$pop}
  19.         // NaN = infinity will raise invalid operation exception
  20.         except on EInvalidOp do
  21.                 isInfinity := false;
  22.         end;
  23. end;
  24.  
  25. begin
  26.         writeLn(isInfinity(NaN));
  27.         writeLn(isInfinity(negInfinity));
  28. end.
works, but is rather counter-productive.
Yours Sincerely
Kai Burghardt

Thaddy

  • Hero Member
  • *****
  • Posts: 6116
Re: Missing IsInfinite function for extended
« Reply #11 on: May 29, 2018, 12:28:17 pm »
Both Nan and Infinity should rely on system, not anything else!
I might not give the answer that you want me to.. Peter Green 1969

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus