# DomainError

**URL:** <https://discourse.julialang.org/t/domainerror/99218>\
**Category:** New to Julia\
**Tags:** question, turing\
**Created:** [May 22, 2023, 1:04pm UTC](https://discourse.julialang.org/t/domainerror/99218 "2023-05-22T13:04:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 22, 2023, 1:04pm UTC](https://discourse.julialang.org/t/domainerror/99218/1 "2023-05-22T13:04:41Z")

</div>

Gettign this error while inferring using turing.jl. What could be the issue ?

```julia
nested task error: TaskFailedException
    
        nested task error: DomainError with -0.00024058756172958402:
        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] ^(x::Float64, y::Float64)
            @ Base.Math ./math.jl:1123
          [3] ^
            @ ~/.julia/packages/ForwardDiff/QdStj/src/dual.jl:524 [inlined]
          [4] CTL(du::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, u::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, p::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, t::Float64)
            @ Main ./In[7]:23
          [5] Void
            @ ~/.julia/packages/SciMLBase/hLrpl/src/utils.jl:468 [inlined]
          [6] (::FunctionWrappers.CallWrapper{Nothing})(f::SciMLBase.Void{typeof(CTL)}, arg1::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, arg2::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, arg3::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, arg4::Float64)
            @ FunctionWrappers ~/.julia/packages/FunctionWrappers/Q5cBx/src/FunctionWrappers.jl:65
          [7] macro expansion
            @ ~/.julia/packages/FunctionWrappers/Q5cBx/src/FunctionWrappers.jl:137 [inlined]
          [8] do_ccall
            @ ~/.julia/packages/FunctionWrappers/Q5cBx/src/FunctionWrappers.jl:125 [inlined]
          [9] FunctionWrapper
...
    @ ~/.julia/packages/Turing/HwTAU/src/inference/Inference.jl:197 [inlined]
 [15] #sample#5
    @ ~/.julia/packages/Turing/HwTAU/src/inference/Inference.jl:193 [inlined]
 [16] top-level scope
    @ In[18]:1

```

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [May 22, 2023, 2:29pm UTC](https://discourse.julialang.org/t/domainerror/99218/2 "2023-05-22T14:29:26Z")

</div>

\*disclaimer: I know nothing about Turing.jl

I don’t know what x or y are for you, but you appear to be raising a negative number to a non-integer power. This may make more sense to you if you represent your x value in polar form and perform the operation. I have tried to reproduce something that will give the same error:

```julia
julia> (-3)^(1.5)
ERROR: DomainError with -3.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] ^(x::Float64, y::Float64)
   @ Base.Math ./math.jl:1123
 [3] ^(x::Int64, y::Float64)
   @ Base ./promotion.jl:444
 [4] top-level scope
   @ REPL[6]:1

```

And a link to [some reference material](https://en.wikipedia.org/wiki/De_Moivre%27s_formula#Failure_for_non-integer_powers,_and_generalization) - i.e. that in general, raising complex numbers to non-integer powers results in multiple possible values.  
And [how you might find all of those values](https://phys.libretexts.org/Bookshelves/Mathematical_Physics_and_Pedagogy/Complex_Methods_for_the_Sciences_(Chong)/08:_Branch_Points_and_Branch_Cuts/8.01:_Non-Integer_Powers_as_Multi-Valued_Operations).

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 22, 2023, 3:25pm UTC](https://discourse.julialang.org/t/domainerror/99218/3 "2023-05-22T15:25:50Z")

</div>

Thank you:)

but I believe issue is due to very large and small values in states for my model. Due to overflow it may be giving a negative value and as I have x^y in my model (where x is state) resulting in domain error.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [May 22, 2023, 5:00pm UTC](https://discourse.julialang.org/t/domainerror/99218/4 "2023-05-22T17:00:32Z")

</div>

If this is a result of an issue with numerical precision but you know that whatever x you’re using here _should always be nonnegative_ then you might try using `max(x,0)^y` rather than `x^y` wherever this problematic exponentiation is happening.

Although there are limits to these sorts of corrections. For example, if you know that x should instead always be _strictly positive_ then you’re in a tougher spot because what value should you set it to? You might need to refactor your calculation of x in an attempt to make it more numerically stable. It’s probably worth trying to improve the calculation in any case.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 22, 2023, 5:22pm UTC](https://discourse.julialang.org/t/domainerror/99218/5 "2023-05-22T17:22:53Z")

</div>

Out of curiosity, what kind of model is it? And which one is always supposed to be positive, x or y?

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 22, 2023, 7:50pm UTC](https://discourse.julialang.org/t/domainerror/99218/6 "2023-05-22T19:50:49Z")

</div>

it’s more like a network of 3 different models with connected input output. Though this error is only due to one of these model. x need to be positive otherwise it goes to complex numbers.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 22, 2023, 7:53pm UTC](https://discourse.julialang.org/t/domainerror/99218/7 "2023-05-22T19:53:20Z")

</div>

What it mean to refactor my calculation ? Can you give a example ?

I tried your suggestion `max(x,0)^y` , it solves that problem but now i’m getting unstable solution warning.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [May 22, 2023, 8:30pm UTC](https://discourse.julialang.org/t/domainerror/99218/8 "2023-05-22T20:30:41Z")

</div>

The floating point numbers used by computers (not just by Julia) are _not_ a perfect implementation of the real numbers we use on paper. In particular, they only have so much precision. For “double-precision” `Float64` values, this is a little under 16 digits. However, one dangerous thing that can happen is catastrophic cancellation, where a calculation results in many fewer digits of accuracy.

For a very basic example, suppose we want to compute `(x + 1)^2 - 1`. Of course, we could equivalently have written this as `x^2 + 2x` or `x * (x + 2)`. But compare these methods with `x = 4 * 10.0^-12`:

```julia
julia> x = 4e-12
4.0e-12

julia> (x + 1)^2 - 1
7.999823026239028e-12

julia> x*(x + 2)
8.000000000015999e-12

```

The exact result should be `8.000000000016`. Notice that the second version is accurate to about 16 digits while the first is only accurate to about 4 (i.e., the first is about 10^{12} times _more wrong_ than the second).

The first version has trouble because `(x + 1)^2 == 1.0000000000079998`. Note that this, too, is accurate to almost 16 digits, but then we subtract `1` from it and our final result becomes much less precise. Smaller `x` will make the issue even worse (try `1e-16`).

The best way to structure your computations depends on what exactly your computation is. The above example was just a simple demonstration of the concept and was resolved with some trivial algebra, but in general this can be much more challenging. Catastrophic cancellation is mostly a problem of subtracting numbers that are very close to each other (or adding numbers that are very nearly opposite each other) while multiplication and division are largely immune to this phenomenon (but have their own failure modes).
