Home > Archive > PERL Miscellaneous > May 2006 > How do I get IEEE infinity?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How do I get IEEE infinity?
|
|
| Steve Chapel 2006-05-18, 7:04 pm |
| 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?
| |
| Robert Sedlacek 2006-05-18, 7:04 pm |
| Steve Chapel <schapel@pixelgate.net> wrote
> I want to use the IEEE floating point value +Infinity in Perl. How can I
> assign that value to a scalar variable?
This seems to have something, never used it tho.
<http://search.cpan.org/~tels/Math-B...ath/BigFloat.pm>
hth, p
--
Thru the Darkness of Futures past the Magician longs to see.
One chants out between two Worlds: Fire, walk with me ..
-- Twin Peaks, Bob
| |
| Steve Chapel 2006-05-18, 7:04 pm |
| Robert Sedlacek wrote:
> Steve Chapel <schapel@pixelgate.net> wrote
>
> This seems to have something, never used it tho.
>
> <http://search.cpan.org/~tels/Math-B...ath/BigFloat.pm>
Nice. Yes, that seems to work:
use Math::BigFloat;
my $val = Math::BigFloat->binf();
# ...
Thanks!
| |
| Ilya Zakharevich 2006-05-18, 7:04 pm |
| [A complimentary Cc of this posting was sent to
Steve Chapel
<schapel@pixelgate.net>], who wrote in article <Qg%ag.89274$dW3.43881@newssvr21.news.prodigy.com>:
> 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
| |
| Donald King 2006-05-19, 4:01 am |
| Steve Chapel wrote:
> 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.
--
Donald King, a.k.a. Chronos Tachyon
http://chronos-tachyon.net/
| |
| Steve Chapel 2006-05-19, 8:01 am |
| Ilya Zakharevich wrote:
>
> 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.
| |
| Denver 2006-05-19, 8:01 am |
| Steve Chapel wrote:
> 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).
| |
| xhoster@gmail.com 2006-05-19, 7:01 pm |
| Steve Chapel <schapel@pixelgate.net> wrote:
> Ilya Zakharevich wrote:
>
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| Denver 2006-05-19, 7:01 pm |
| xhoster wrote:
> Steve Chapel wrote:
>
> Why would would 1/0 be plus infinity, rather than minus infinity?
Perhaps he reserves that for 1/-0.
| |
| Ilya Zakharevich 2006-05-19, 9:59 pm |
| [A complimentary Cc of this posting was sent to
Steve Chapel
<schapel@pixelgate.net>], who wrote in article <_Jjbg.73836$F_3.59044@newssvr29.news.prodigy.net>:
[color=darkred]
> 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
| |
| John W. Kennedy 2006-05-20, 7:00 pm |
| Denver wrote:
> Steve Chapel wrote:
> 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.
--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
| |
| John W. Kennedy 2006-05-20, 7:00 pm |
| xhoster@gmail.com wrote:
> Steve Chapel <schapel@pixelgate.net> wrote:
>
> 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.
--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
| |
| Denver 2006-05-20, 7:00 pm |
| John W. Kennedy wrote:
> In the first mode, 1.0 / 0.0 gives plus infinity because 1.0 is
> positive. -1.0 / 0.0 gives minus infinity.
How about -1.0 / -0.0 ?
| |
| Dr.Ruud 2006-05-21, 4:00 am |
| 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.
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| John W. Kennedy 2006-05-21, 8:02 am |
| In-Reply-To: <126v45mact0kl42@corp.supernews.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 14
Message-ID: <IFZbg.2273$Ow7.571@fe11.lga>
Date: Sun, 21 May 2006 09:10:58 -0400
NNTP-Posting-Host: 68.193.246.216
X-Complaints-To: abuse@cv.net
X-Trace: fe11.lga 1148217064 68.193.246.216 (Sun, 21 May 2006 06:11:04 MST)
NNTP-Posting-Date: Sun, 21 May 2006 06:11:04 MST
Organization: Optimum Online
Xref: number1.nntp.dca.giganews.com comp.lang.perl.misc:604165
Denver wrote:
> John W. Kennedy wrote:
>
> How about -1.0 / -0.0 ?
Probably plus infinity, but I don't know offhand for certain.
--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
| |
| Ilya Zakharevich 2006-05-21, 7:04 pm |
| [A complimentary Cc of this posting was NOT [per weedlist] sent to
Dr.Ruud
<rvtol+news@isolution.nl>], who wrote in article <e4pgum.168.1@news.isolution.nl>:
> 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
| |
| Dr.Ruud 2006-05-21, 10:02 pm |
| Ilya Zakharevich schreef:
> Dr.Ruud:
[color=darkred]
>
> 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.
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| Ilya Zakharevich 2006-05-22, 4:09 am |
| [A complimentary Cc of this posting was NOT [per weedlist] sent to
Dr.Ruud
<rvtol+news@isolution.nl>], who wrote in article <e4r9jm.1is.1@news.isolution.nl>:
>
> 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
| |
| Dr.Ruud 2006-05-22, 4:09 am |
| 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.
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| Ilya Zakharevich 2006-05-22, 7:05 pm |
| [A complimentary Cc of this posting was NOT [per weedlist] sent to
Dr.Ruud
<rvtol+news@isolution.nl>], who wrote in article <e4s6rv.oo.1@news.isolution.nl>:
[color=darkred]
[color=darkred]
[color=darkred]
[color=darkred]
[color=darkred]
[color=darkred]
>
> 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
| |
| Steve Chapel 2006-05-29, 7:03 pm |
| Denver wrote:
> Steve Chapel wrote:
> It mathematics, division by 0 is undefined, not infinity (infinity is
> not large enough).
Yes, I understand that 1/0 is undefined in the real numbers.
However, in IEEE arithmetic, there is +0.0 which represents both 0 and
positive numbers that are too small to be represented with any
precision. in IEEE floating point, +1.0/+0.0 is +infinity. In most
programming environments, 1.0/0.0 is +infinity. That's what I would have
expected in Perl.
| |
| Steve Chapel 2006-05-29, 7:03 pm |
| Steve Chapel wrote:
> I want to use the IEEE floating point value +Infinity in Perl.
Sorry to open this can of worms again, but now I find that it would be
convenient in my Perl program to have a floating point number that
represents NaN (not a number), which is used to represent the result of
expressions such as 0/0 and sqrt(-1). Math::BigFloat has a way to
represent NaN, but is there a way to do it in native Perl?
| |
| Ilya Zakharevich 2006-05-29, 7:03 pm |
| [A complimentary Cc of this posting was sent to
Steve Chapel
<schapel@pixelgate.net>], who wrote in article <HlGeg.88052$H71.16189@newssvr13.news.prodigy.com>:
> Steve Chapel wrote:
>
> Sorry to open this can of worms again, but now I find that it would be
> convenient in my Perl program to have a floating point number that
> represents NaN (not a number), which is used to represent the result of
> expressions such as 0/0 and sqrt(-1). Math::BigFloat has a way to
> represent NaN, but is there a way to do it in native Perl?
Proceed as adviced before:
~->perl -wle "print exp 1e1000000"
Infinity
~->perl -wle "print sin exp 1e1000000"
NaN
Hope this helps,
Ilya
|
|
|
|
|