Plot of Cubic Root is not showing the negative f / codomain

Hi all,

I want to know why is my Cubic Root graph only showing positive f ?

using Plots, LaTeXStrings, Plots.PlotMeasures
gr()

f(x) = x^(1/3)  

plot(f,-10,10, xticks=false, xlims=(-13,13), ylims=(-13,13), 
    bottom_margin = 10mm, label=L"f(x) =  \sqrt[3]{x}", framestyle = :zerolines, 
    legend=:outerright)

annotate!([(2,0, (L"|", 11, :black))])
annotate!([(8,-3.05, (L"dom(f)=\mathbb{R}", 10, :black))])
annotate!([(8,-4.55, (L"codom(f)=\mathbb{R}", 10, :black))])

Capture d’écran_2022-08-28_14-53-35

The short answer is to use cbrt if you want the real cube root function.

The longer answer is that the general power function only works for negative reals if given as complex numbers and then you get the principal complex branch, which does not follow the negative real axis. Additionally 1/3 is not exactly representable as a binary floating point number, so it’s somewhat unreasonable to expect x^(1/3) to act as an ideal cube root.

julia> (-8)^(1/3)
ERROR: DomainError with -8.0:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
Stacktrace:
 [1] throw_exp_domainerror(x::Float64)
   @ Base.Math ./math.jl:37
 [2] ^
   @ ./math.jl:909 [inlined]
 [3] ^(x::Int64, y::Float64)
   @ Base ./promotion.jl:413
 [4] top-level scope
   @ REPL[273]:1

julia> Complex(-8)^(1/3)
1.0 + 1.7320508075688772im

julia> cbrt(-8)
-2.0
5 Likes

Thanks for the long answer, it is really enlightening for me…