How do I get IEEE infinity?

S

Steve Chapel

I want to use the IEEE floating point value +Infinity in Perl. How can I
assign that value to a scalar variable? I tried looking for a way to
write a literal value representing infinity, but couldn't find one. I
tried computing +Infinity by dividing 1.0 by 0.0, but at runtime I get
an "Illegal division by zero" error. Infinity is a valid value in IEEE
floating point, so certainly there's some way to assign it to a
variable, isn't there?
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Steve Chapel
I want to use the IEEE floating point value +Infinity in Perl. How can I
assign that value to a scalar variable? I tried looking for a way to
write a literal value representing infinity, but couldn't find one. I
tried computing +Infinity by dividing 1.0 by 0.0, but at runtime I get
an "Illegal division by zero" error. Infinity is a valid value in IEEE
floating point, so certainly there's some way to assign it to a
variable, isn't there?

exp 1e300000000

Hope this helps,
Ilya
 
D

Donald King

Steve said:
I want to use the IEEE floating point value +Infinity in Perl. How can I
assign that value to a scalar variable? I tried looking for a way to
write a literal value representing infinity, but couldn't find one. I
tried computing +Infinity by dividing 1.0 by 0.0, but at runtime I get
an "Illegal division by zero" error. Infinity is a valid value in IEEE
floating point, so certainly there's some way to assign it to a
variable, isn't there?

On most systems, you can use the strings "+inf", "-inf", and "nan" to
represent positive infinity, negative infinity, and not-a-number (resp).
The details depend on your system, since Perl just calls the
underlying strtod(3) function of your C library. If you're on a modern
Linux, this is fairly likely to work. Other systems will vary (a lot).

A quick way to check is to run this one-liner:
perl -e 'print "+inf" > "-inf" ? "OK\n" : "FAIL\n"'
OK means your strtod seems to support IEEE infinities.
 
S

Steve Chapel

Ilya said:
exp 1e300000000

Weird. Just '$var = 1e1000;' sets $var to +Infinity. I would expect 1.0
/ 0.0 to silently evaluate to +Infinity, and an overflowing literal to
generate a compile-time error or warning.
 
D

Denver

Steve said:
I would expect 1.0 / 0.0 to silently evaluate to +Infinity
It mathematics, division by 0 is undefined, not infinity (infinity is not large enough).
 
X

xhoster

Steve Chapel said:
Weird. Just '$var = 1e1000;' sets $var to +Infinity. I would expect 1.0
/ 0.0 to silently evaluate to +Infinity, and an overflowing literal to
generate a compile-time error or warning.

Why would would 1/0 be plus infinity, rather than minus infinity?

Xho
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Steve Chapel
Weird. Just '$var = 1e1000;' sets $var to +Infinity.

Only if doubles are short enough. When used with 128-bit doubles, I'm
pretty sure 1e1000 will be legal.
I would expect 1.0 / 0.0 to silently evaluate to +Infinity

You mean +/- Infinity, as other posters note. It should, but the
initial versions of Perl were developed when IEEE arithmetic was not
reliably available. So there is a couple of historic special cases,
where Perl intervenes in the "IEEE flow of meaning", such as 1/0 and log 0.

This is a bug, but for backward compatibility, this behaviour is preserved.

Hope this helps,
Ilya
 
J

John W. Kennedy

Denver said:
It mathematics, division by 0 is undefined, not infinity (infinity is
not large enough).

Yes, but division by zero producing infinity is one accepted mode of
operating modern computers, for pragmatic reasons.
 
J

John W. Kennedy

Why would would 1/0 be plus infinity, rather than minus infinity?

Actually, IEEE floating point recognizes two modes, one with separate
plus and minus infinity, and the other with plus and minus infinity
being the same.

In the first mode, 1.0 / 0.0 gives plus infinity because 1.0 is
positive. -1.0 / 0.0 gives minus infinity.
 
D

Dr.Ruud

Steve Chapel schreef:

? I would expect
1.0 / 0.0 to silently evaluate to +Infinity

Never silently, it should throw an exception. Unless you turned that one
specifically off before.
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was NOT [per weedlist] sent to
Dr.Ruud
Steve Chapel schreef:

? I would expect

Never silently, it should throw an exception. Unless you turned that one
specifically off before.

All CRT environments I checked (not much, in fact) start a program
with FP exceptions disabled. Check with

perl -wle '$a = 1e1000000; print exp $a'

This should not even compile if exceptions are enabled.

Hope this helps,
Ilya
 
D

Dr.Ruud

Ilya Zakharevich schreef:
Dr.Ruud:

All CRT environments I checked (not much, in fact) start a program
with FP exceptions disabled. Check with

perl -wle '$a = 1e1000000; print exp $a'

This should not even compile if exceptions are enabled.

I meant the softer kind (like eval minus $@).

|$ perl -e '1/0'
|Illegal division by zero at -e line 1.

Can "eval" catch it?

|$ perl -e 'eval{1/0}'
|Illegal division by zero at -e line 1.
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was NOT [per weedlist] sent to
Dr.Ruud
I meant the softer kind (like eval minus $@).

|$ perl -e '1/0'
|Illegal division by zero at -e line 1.

"Exception" has a precise meaning in the context you used. It is
perpendicular to die(). (And on x86 architecture it is asyncroneous
too; sigh...) But I agree that it would be nice to have a portable
way to translate FP exceptions to die()s (instead of non-portable way
to make them generate signals).
Can "eval" catch it?
|$ perl -e 'eval{1/0}'
|Illegal division by zero at -e line 1.

You forgot about constant folding...

Hope this helps,
Ilya
 
D

Dr.Ruud

Ilya Zakharevich schreef:
Dr.Ruud:

"Exception" has a precise meaning in the context you used. It is
perpendicular to die().

OK, but "eval-then-test-$@" is also freely called Perl's
try-and-catch-the-exception. So eval() is Perl's way to "disable" (or
hide) exceptions.

(And on x86 architecture it is asyncroneous
too; sigh...)

I dislike all hardware that has a Von Neumann architecture, unless it's
human and friendly.
http://en.wikipedia.org/wiki/Von_Neumann_architecture

But I agree that it would be nice to have a portable
way to translate FP exceptions to die()s (instead of non-portable way
to make them generate signals).

I agree too.

You forgot about constant folding...

Ah, I forgot (again) to try with "-MO-Deparse" to help me catch that.

$ perl -we '$x=eval{1/($x=0)}; $@ and exit print "ERROR: $@"'
ERROR: Illegal division by zero at -e line 1.
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was NOT [per weedlist] sent to
Dr.Ruud
OK, but "eval-then-test-$@" is also freely called Perl's
try-and-catch-the-exception. So eval() is Perl's way to "disable" (or
hide) exceptions.

Nope, this is a clash of terms. eval{} catches "Perl's exceptons".
FP exceptions, first, should not be catched by default, second, would
not be catched by Perl even if enabled by CRT (unless you set
$SIG{FPE}=sub{die}).
Ah, I forgot (again) to try with "-MO-Deparse" to help me catch that.
$ perl -we '$x=eval{1/($x=0)}; $@ and exit print "ERROR: $@"'
ERROR: Illegal division by zero at -e line 1.

As I said before, this is a bug of Perl. Division by zero is legal in
FP. And Perl operates "as if" in FP.

Hope this helps,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top