Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Friday February 20, 2009
05:08 PM

Known Ruby Bug?

[ #38520 ]

I suppose I should ask this in a Ruby forum, but since I'm so used to slinging other languages here ...

To find the Nth root of number is simple: raise the number to the reciprocal of N. For example, to find the cube root of 8:

$ perl -le 'print 8 ** (1/3)'
2

But you can't quite do that in Ruby:

$ ruby -e 'puts 8 ** (1/3)'
1

But this is a "feature", not a bug (*cough*) because the 1/3 is considered integer math and evaluates to 0, leaving you with 8 to the 0th power. Anything raised to the power of 0 results in 1. So far so good.

So to force floating point math, use a floating point number:

$ ruby -e 'puts 8 ** (1/3.0)'
2

And all is good. Except ...

Let's take the square root of 1:

$ ruby -e 'puts 1 ** (1/2.0)'
1.0

Now let's take the square root of -1:

$ ruby -e 'puts -1 ** (1/2.0)'
-1.0

Huh? The square root of -1 is imaginary (or i, if you want to be specific). What's going on here?

Yes, I know about Math.sqrt, which at least thoughtfully throws an exception rather than give an incorrect value:

$ ruby -e 'puts Math.sqrt(-1)'
-e:1:in `sqrt': Numerical argument out of domain - sqrt (Errno::EDOM)
    from -e:1

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • simon@alibi ~ % ruby -e 'puts -1 ** 0.5'
    -1.0
    simon@alibi ~ % ruby -e 'puts ( (-1) ** 0.5)'
    NaN
  • FrewSchmidt2 [9844] % perl -le 'print (-1) ** (1/2)'
    -1

    Am I missing something here?
    --
    --fREW
    http://blog.afoolishmanifesto.com
    • indeed you are:

      C:\Users\burak>perl -MO=Deparse -le "print (-1) ** (1/2)"
      BEGIN { $/ = "\n"; $\ = "\n"; }
      print(-1) ** 0.5;
      -e syntax OK

      try this instead:

      print( (-1) ** (1/2) )

      • Hah! The funny thing is that I was talking to someone about this bug YESTERDAY. Thanks.
        --
        --fREW
        http://blog.afoolishmanifesto.com
      • Having to liberally splatter brackets around the place looks no better than having to force something to not be interpreted as an integer. It's certainly just as obscure.