Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^12: NaN output

by BrowserUk (Patriarch)
on Mar 07, 2014 at 00:38 UTC ( [id://1077302]=note: print w/replies, xml ) Need Help??


in reply to Re^11: NaN output
in thread NaN output

Hm. $x starts undefined

[0] Perl> use Devel::Peek;; [0] Perl> Dump $x;; SV = NULL(0x0) at 0x3c6d400 REFCNT = 1 FLAGS = ()

You set $x to the maximum unsigned integer value; it becomes an IV:

[0] Perl> $x = ~0;; [0] Perl> Dump $x;; SV = IV(0x3c6d3f8) at 0x3c6d400 REFCNT = 1 FLAGS = (IOK,pIOK,IsUV) UV = 18446744073709551615

You add 2 to it, it overflows the IV, so perl converts it to an NV (NB: No NaN arises from the overflow of the IV.)

[0] Perl> $x += 2;; [0] Perl> Dump $x;; SV = PVNV(0x3cb85e8) at 0x3c6d400 REFCNT = 1 FLAGS = (NOK,pNOK) IV = -1 NV = 1.84467440737096e+019 PV = 0

Then, you raise that NV to the power 35; and the NV overflows; resulting in an NV with the floating point value of positive infinity (1.#INF) (NB:the "result to large" error):

[0] Perl> $x **=35;; [Result too large] Perl> Dump $x;; SV = PVNV(0x3cb85e8) at 0x3c6d400 REFCNT = 1 FLAGS = (NOK,pNOK) IV = -1 NV = 1.#INF PV = 0

You then divide the NV 1.#INF, by itself, and you produce an indeterminate floating point value (1.#IND):

[0] Perl> $x /= $x;; [0] Perl> Dump $x;; SV = PVNV(0x3cb85e8) at 0x3c6d400 REFCNT = 1 FLAGS = (NOK,pNOK) IV = -1 NV = -1.#IND PV = 0

The (special form of) NaN was produced by operating upon (erroneous) floating point (NV) values; not integer (IV) overflow.

NaN is a purely floating point concept, and cannot arise as a result of integer overflow.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^13: NaN output
by syphilis (Archbishop) on Mar 07, 2014 at 01:56 UTC
    Nothing there that I didn't already know, though the phrase "(erroneous) floating point (NV) values" did catch my eye. I'm wondering why "(erroneous)" is in there.

    I've not previously encountered the notion that an infinity is an *erroneous* floating point value. (Or, if I have encountered it, I've not paid due attention to it.)
    Did I misconstrue ?

    Cheers,
    Rob
        Ie. A value beyond the capabilities of the binary format to represent. Ie. an erroneousness value.

        Hmmm ... "erroneous" is not a term that I would normally use here. However, it could be handy for some leverage at some stage, so I'll keep it in mind.

        Interestingly, by my reading of Wikipedia, underflow (when the result of a calculation is a smaller number than the computer can actually store in memory) is also an exception in IEEE 754.
        So it, too, pertains to "a value beyond the capabilities of the binary format to represent".

        Maybe we'll see you referring to an "erroneous zero" at some time in the future ;-)

        Cheers,
        Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1077302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found