real numbers infinity

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

real numbers infinity

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:

> $ 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:

> $ ./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:

>  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?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real numbers infinity

Sasa Zeman
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real numbers infinity

Dimitrios Apostolou
> > 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real numbers infinity

Florian Klaempfl
Dimitris 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? 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.

> 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
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real numbers infinity

Sasa Zeman
In reply to this post by Dimitrios Apostolou
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real numbers infinity

Jonas Maebe-2
In reply to this post by Dimitrios Apostolou

On 11 apr 2006, at 01:45, 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real numbers infinity

Dimitrios Apostolou
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal