stephanm has asked for the wisdom of the Perl Monks concerning the following question:

I came across a funny thing when the input to a calculation involving the exponential operator turned negativ.

To demonstrate - here is some code:

print "Power of calculation test\n"; print("line 1: ", -4**1.5, "\n"); print("line 2: ", (-4)**1.5, "\n"); print("line 3: ", 1/(-4)**1.5, "\n"); print("line 4: ", 3 + (-4)**1.5, "\n");
The output looked like this:

Power of calculation test line 1: -8 line 2: 0 line 3: 0 line 4: 0

So, the line 1 output is probably explained by precedence.

However, if line 2 is a permissible operation - how does it get to zero?

And why (line 3) can I use the result as a divisor without Perl complaining about division by zero or add 3 and still get zero?

It cost me a few hours to find why my numbers did not add up - shouldn't (-4)**1.5 be an illegal statement?

Appreciate your wisdom...

Replies are listed 'Best First'.
Re: Strange Logic in Power of arithmetic
by Athanasius (Archbishop) on Jan 12, 2016 at 13:11 UTC

    Hello stephanm,

    What version of Perl are you using? On v5.14.4 and v5.20.2 I get this:

    23:07 >perl 1512_SoPW.pl Power of calculation test line 1: -8 line 2: -1.#IND line 3: -1.#IND line 4: -1.#IND 23:07 >

    On v5.22.0 and v5.22.1 I get:

    23:06 >perl 1512_SoPW.pl Power of calculation test line 1: -8 line 2: NaN line 3: NaN line 4: NaN 23:06 >

    — as you expected.

    Update: I’m using Strawberry Perl on Windows 8.1, 64-bit.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      5.10.1 linux 64 bit:
      line 1: -8 line 2: nan line 3: nan line 4: nan
      5.8.3 linux 32 bit:
      line 1: -8 line 2: -nan line 3: -nan line 4: -nan
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Perl 5.20.2 (strawberryPerl - Windows):
      Power of calculation test line 1: -8 line 2: -1.#IND line 3: -1.#IND line 4: -1.#IND
Re: Strange Logic in Power of arithmetic
by Your Mother (Archbishop) on Jan 12, 2016 at 13:50 UTC

    I want to be in the pageant! Perl 6.c on Darwin.

    moo@cow[1102]~>perl6 > print("line 1: ", -4**1.5, "\n"); print("line 2: ", (-4)**1.5, "\n"); print("line 3: ", 1/(-4)**1.5, "\n"); print("line 4: ", 3 + (-4)**1.5, "\n"); > line 1: -8 > line 2: NaN > line 3: NaN > line 4: NaN
Re: Strange Logic in Power of arithmetic
by ExReg (Priest) on Jan 12, 2016 at 15:59 UTC

    Active State 5.8.8 on Win64

    line 1: -8 line 2: -1.#IND line 3: Illegal division by zero at -e line 1 line 4: -2147483645

    Had to do each one line by line as it failed as a script on line 3 otherwise

    version 5.6.1 on Solars

    line 1: -8 line 2: NaN line 3: NaN line 4: NaN

    If I type them in on Windows as perl -e "print -4**1.5;"..., I get

    -8 -4 Illegal division by zero at -e line 1 -2147483645
      Active State 5.8.8 on Win64 line 1: -8 line 2: -1.#IND line 3: Illegal division by zero at -e line 1 line 4: -2147483645
      Interesting. Not on darwin:
      $ perlbrew exec perl 1152551.pl perl-5.8.8 ========== Power of calculation test line 1: -8 line 2: nan line 3: nan line 4: nan
      The way forward always starts with a minimal test.

        Win 7 64 bit, ActiveState 5.8.8 built for MSWin32-x86-multithread Binary build 817

Re: Strange Logic in Power of arithmetic
by VinsWorldcom (Prior) on Jan 12, 2016 at 14:50 UTC

    Windows 7 x64 with Strawberry 5.18.1 64-bit.

    VinsWorldcom@C:\Users\VinsWorldcom\tmp> perl -v This is perl 5, version 18, subversion 1 (v5.18.1) built for MSWin32-x +64-multi-thread [...] VinsWorldcom@C:\Users\VinsWorldcom\tmp> test.pl Power of calculation test line 1: -8 line 2: -1.#IND line 3: -1.#IND line 4: -1.#IND
      Wow, nice variety of different results.... My version was:
      This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x +86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2012, Larry Wall Binary build 1604 [298023] provided by ActiveState http://www.ActiveSt +ate.com Built Apr 14 2014 14:32:20
      on
      Vista Home Premium, Service Pack 2, 32 bit architecture.
      It seems to indicate there is some kind of glitch with my implementation. Maybe it's time to upgrade. Thanks for your inputs.
        Wow, nice variety of different results

        Note that -1.#IND, nan, -nan and NaN are just different ways of denoting the same value - namely "not a number".

        Cheers,
        Rob