Cube root of sum

-5.class => Integer

-5**(1.0/3) => -1.7099759466766968

(-6+1).class => Integer

(-6 + 1)**(1.0/3) => (0.8549879733383486+1.480882609682364i)

x = (-6 + 1)

x.class => Integer

x**(1.0/3) => (0.8549879733383486+1.480882609682364i)

why am I getting a complex result like this?

The Copilot AI responds:

This behavior in Ruby is due to the way exponentiation interacts with negative numbers and floating-point division.

Why does -5**(1.0/3) return a real number?

In Ruby, the ** operator follows traditional mathematical precedence, meaning that
-5**(1.0/3) is actually interpreted as -(5**(1.0/3)). Since raising a positive number to a fractional power (such as 1.0/3 for a cube root) yields a real number, negating it results in a real number too.

Why does (-6 + 1)**(1.0/3) return a complex number?

Here, (-6 + 1) evaluates to -5, so you’re effectively computing (-5)**(1.0/3). But when a negative number is directly raised to a fractional power, Ruby determines that the result should be computed using complex numbers. This follows the mathematical principle that fractional exponents of negative numbers often lead to complex values when computed numerically.

How is (-5)**(1.0/3) different from -5**(1.0/3)?

  • -5**(1.0/3) computes -(5**(1.0/3)), giving a real result.
  • (-5)**(1.0/3) treats -5 as a single entity before exponentiation, leading to a complex result.

If you want to force real number calculations where possible, you can use
(-5).abs**(1.0/3) * (-1), ensuring the exponentiation happens on the absolute value first.

2 Likes

See Complex conjugate root theorem

Thanks Dan for this clear explanation, you really are a fountain of knowledge.

1 Like

I just pasted your question into the MS Copilot AI applet and it found all the info.

I was wondering about that.
Reality is getting more perplexing than ever…