Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-04

real numbers infinity

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dimitrios Apostolou

Hello list,

when a real number overflows the program halts with EInvalidOp error. How can I make the program behave the more standard way, where the variable gets the value "inf" and execution continues normally? This is vital for many tasks, like fractal computation.

example program:

Show quoted text 12 lines
$ cat realover.pas program realover; Uses variants; var r:real; BEGIN r:=2; while True do begin r:=r**2; writeLn(r); end; END.

current output:

Show quoted text 16 lines
$ ./realover 4.000000000000000E+000 1.600000000000000E+001 2.560000000000000E+002 6.553600000000000E+004 4.294967296000000E+009 1.844674407370955E+019 3.402823669209385E+038 1.157920892373162E+077 1.340780792994260E+154 An unhandled exception occurred at $0805F1C6 : EInvalidOp : Invalid floating point operation $0805F1C6 $0805F895 $08053CDF $08048182

desired output:

Show quoted text 12 lines
4.000000000000000E+000 1.600000000000000E+001 2.560000000000000E+002 6.553600000000000E+004 4.294967296000000E+009 1.844674407370955E+019 3.402823669209385E+038 1.157920892373162E+077 1.340780792994260E+154 inf inf inf

...

Thanks in advance,
Dimitris

P.S. Why when the program doesn't include the variants unit the program complains at runtime? What is that unit?

Reply by email

Sasa Zeman
to Dimitrios Apostolou

Dimitris,

when a real number overflows the program halts with EInvalidOp error. How can I make the program behave the more standard way, where the variable gets the value "inf" and execution continues normally? This is vital for many tasks, like fractal computation.

You can use try..except..end block and switch -Sd when compile:

program realover;
Uses math;
var r:real;
BEGIN
   r:=2;
   while True do
   begin
     try
       r:=r**2;
       writeLn(r);
     except
       r:=infinity;
       writeLn(r);
       // break
     end;
  end;
END.

Or you may take a look IEEE specification if it is necessary to create your own big float support for larger precision with fractals.

P.S. Why when the program doesn't include the variants unit the program complains at runtime? What is that unit?

At windows, it is not necessary for this example. Variants unit provide new type definition and supported functions, which can consist any simple type (integer, string, etc).

Sasa

Reply by email

Dimitris Apostolou
to Sasa Zeman
> when a real number overflows the program halts with EInvalidOp error. > How can I make the program behave the more standard way, where the > variable gets the value "inf" and execution continues normally? This

is

> vital for many tasks, like fractal computation. You can use try..except..end block and switch -Sd when compile:

Thank you very much, this solves the problem though I think that performance-wise it is not optimal. What is the reason for freepascal being not IEEE-spec compliant? Is it backwards compatibility?

Or you may take a look IEEE specification if it is necessary to create your own big float support for larger precision with fractals.

It seems really weird that there is no such unit in the standard freepascal distribution. It would be a huge task for me to create one with C-like speed.

Dimitris

Analog-/ISDN-Nutzer sparen mit GMX SmartSurfer bis zu 70%! Kostenlos downloaden: http://www.gmx.net/de/go/smartsurfer

Reply by email

Florian Klaempfl
to Dimitris Apostolou
Dimitris Apostolou wrote 10 lines
>> when a real number overflows the program halts with EInvalidOp error. >> How can I make the program behave the more standard way, where the >> variable gets the value "inf" and execution continues normally? This is >> vital for many tasks, like fractal computation. > You can use try..except..end block and switch -Sd when compile: Thank you very much, this solves the problem though I think that performance-wise it is not optimal. What is the reason for freepascal being not IEEE-spec compliant? I

It is:
http://community.freepascal.org:10000/docs-html/rtl/math/setexceptionmask.html

You can turn off floating point exceptions using this procedure.

Show quoted text 12 lines
Is it backwards compatibility? > Or you may take a look IEEE specification if it is necessary to create > your > own big float support for larger precision with fractals. It seems really weird that there is no such unit in the standard freepascal distribution. It would be a huge task for me to create one with C-like speed. Dimitris

Reply by email

Sasa Zeman
to Dimitris Apostolou: is Thank you very much, this solves the problem though I think that performance-wise it…
Thank you very much, this solves the problem though I think that performance-wise it is not optimal. What is the reason for freepascal being not IEEE-spec compliant? Is it backwards compatibility?

FPC is IEEE-spec compliant, since all computer follow up floating point standard. It should follow up to 80 bits for double-extended precision. Note that IEEE specification is recently under revision.

It seems really weird that there is no such unit in the standard freepascal distribution. It would be a huge task for me to create one with C-like speed.

That is specific task. Take a look Alex Fihman BigFloat unit, for example: http://www.db-access.com/

Sasa

Reply by email

Jonas Maebe
to Dimitrios Apostolou: when a real number overflows the program halts with EInvalidOp error. How can I make the…
Dimitrios Apostolou wrote
when a real number overflows the program halts with EInvalidOp error. How can I make the program behave the more standard way, where the variable gets the value "inf" and execution continues normally?

I'm not sure if overflow immediately results in infinity, I think first you get some denormalss. There's also nothing particularly standard about whether or not an exception is thrown when you have an FPU overflow, since all common FPU's I know about support both.

Anyway, you can mask overflow and related exceptions by doing this:

uses math;

...

SetExceptionMask([exDenormalized,exOverflow,exPrecision]);

Jonas

Reply by email

Dimitrios Apostolou
to Jonas Maebe
Jonas Maebe wrote
SetExceptionMask([exDenormalized,exOverflow,exPrecision]);

Thank you all for your answers, that will do just fine! Too bad though that it not that obvious in the documentation, which I browsed for many hours before posting to this list.

Thank you,
Dimitris

Reply by email