Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IEEE arithmetic and exceptions

0 views
Skip to first unread message

Bob Walton

unread,
Mar 16, 1997, 3:00:00 AM3/16/97
to

I was recently attempting to port some hairy numeric code to
Perl (currently operational on Matlab, Java, JavaScript, Excel,
and Lotus). This code takes advantage of the IEEE "Inf" and
"NaN" values to ascertain when input variables are in
"infeasible" ranges.

I note that Perl generates fatal error messages on things
like 1/0 and sqrt(-1), rather than returning Inf or NaN
values, even on machines with hardware IEEE arithmetic support.
Putting in checks around each and every division, sqrt, etc is
a big and potentially error-prone task, especially to generate
the desired behavior which is identical with what IEEE standard
arithmetic gives. I have read all the man pages, the Camel book,
and the FAQ, all without finding a solution to this. I
know that enclosing expressions with an "eval" will get
rid of the fatal error, and will return an "undef" for
the entire expression. But the "undef" is later interpreted
as a zero, and does not propagate, which will make my code
give wrong answers. I see the FAQ mentions an "idea" for a
future IEEE module.

Is there any way of getting Perl to generate Inf and NaN
values and suppress the error messages for division by zero
and domain errors in built-in functions? Thank you.

Tom Phoenix

unread,
Mar 17, 1997, 3:00:00 AM3/17/97
to Bob Walton

On Sun, 16 Mar 1997, Bob Walton wrote:

> Is there any way of getting Perl to generate Inf and NaN
> values and suppress the error messages for division by zero
> and domain errors in built-in functions? Thank you.

Hmmm.... This could become a 'use ieee;' pragma. But there's a pesky
scoping problem: How can you make my module use ieee if I didn't think
about it when I wrote it? This might happen, if it won't break too many
things. Any ideas what would break? Do you want to make a patch and
find out? :-)

-- Tom Phoenix http://www.teleport.com/~rootbeer/
root...@teleport.com PGP Skribu al mi per Esperanto!
Randal Schwartz Case: http://www.lightlink.com/fors/


Bob Walton

unread,
Mar 19, 1997, 3:00:00 AM3/19/97
to Tom Phoenix

Tom, thanks. I'm not sure if by "module" you mean Perl modules?
If so, it would seem to me like it couldn't break much -- right
now, folks are either getting fatal errors from division by zero
and domain errors, or they are enclosing their expression in an
"eval" and getting undef. If things were set up so an "eval"
simply changed NaN or Inf to undef, that behavior would continue
(since NaN and Inf propagate) and the only change would be the
absence of fatal errors (and the return of NaN or Inf) from
operations like 1/0 etc without eval.

If on the other hand, you mean by "module" internal perl code,
I can only speculate, as I am not familiar with that code. It
would seem like a fairly small number of places would be affected
-- conversion routines from numeric to string and back,
fprint/sprint, the arithmetic operators, and maybe a few other
places. I note that the package Math::BigFloat already does
what appears to be the equivalent of IEEE arithmetic in its scheme.

Regarding a patch, I am willing to do what I can, which probably
isn't very much, as I have no expertise in perl internals.

0 new messages