# ERROR: DomainError, Complex numbers?

**URL:** <https://discourse.julialang.org/t/error-domainerror-complex-numbers/108923>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 17, 2024, 7:26pm UTC](https://discourse.julialang.org/t/error-domainerror-complex-numbers/108923 "2024-01-17T19:26:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gast](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gast/32/206425_2.png) [@Gast](https://discourse.julialang.org/u/Gast)\
**Post date:** [January 17, 2024, 7:26pm UTC](https://discourse.julialang.org/t/error-domainerror-complex-numbers/108923/1 "2024-01-17T19:26:41Z")

</div>

Hi all!

I have a very simple expression similar to:

```julia
a = b ^ c

```

with `b` and `c` as positive floating point numbers.

The expression above is part of nested functions (outer and inner functions) with each function defined within the scope of its parent function.

If I run my these nested functions from a `Main.jl` file, I get the following error (despite `a` and `b` being **positive** `Float64`):

```julia
ERROR: DomainError with -5.991263951486869e-16:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.

```

I tried to sort this error out with the function `real()` but didn’t work.

Interestingly, if I run my functions line by line (instead of calling the `Main.jl` file), I can compute the expression above without the error (giving me as a result a floating point number).

I would appreciate any advice/comment/help on this because I am out of ideas!

Thanks

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 17, 2024, 7:35pm UTC](https://discourse.julialang.org/t/error-domainerror-complex-numbers/108923/2 "2024-01-17T19:35:58Z")

</div>

put a print statement in. the base of the exponent is negative.

---

<div class="post-metadata">

**Author:** ![Gast](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gast/32/206425_2.png) [@Gast](https://discourse.julialang.org/u/Gast)\
**Post date:** [January 17, 2024, 7:47pm UTC](https://discourse.julialang.org/t/error-domainerror-complex-numbers/108923/3 "2024-01-17T19:47:51Z")

</div>

Thanks! I am 100% sure that the base of the exponent is a positive floating number. When I print `b` in my example, I get: `21.778641870591873`, and the value of the expression `a` is printed as: `1.2954354359893232`

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [January 17, 2024, 8:11pm UTC](https://discourse.julialang.org/t/error-domainerror-complex-numbers/108923/4 "2024-01-17T20:11:20Z")

</div>

As @Oscar_Smith suggested, you should evaluate the expression, assign it to a variable `a`, and print it and `b` just before you call `a^b`. You are 100% sure `a` is positive, but the error message tells us that `a` is negative. You need to figure out where/what the discrepancy is.

If you’re 100% sure `a` is positive because the expression is provably positive, be aware that floating point numbers don’t obey the same rules as the reals. Your value of `a` is negative but very small, suggesting that it should be zero or a small positive, but instead is negative due to rounding errors.

---

<div class="post-metadata">

**Author:** ![Gast](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gast/32/206425_2.png) [@Gast](https://discourse.julialang.org/u/Gast)\
**Post date:** [January 17, 2024, 8:58pm UTC](https://discourse.julialang.org/t/error-domainerror-complex-numbers/108923/5 "2024-01-17T20:58:57Z")

</div>

Apologies, @Oscar_Smith, I didn’t get your comment right away but with the clarification of @John_Gibson, now I see what you meant.

Printing the exponent before the expression gave me some insights. I don’t know how but, indeed, there were some very small negative numbers in there.

For the moment, I am taking the absolute value of the base of the exponent to avoid the error:

`a = abs(b) ^ c`

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [January 17, 2024, 10:35pm UTC](https://discourse.julialang.org/t/error-domainerror-complex-numbers/108923/6 "2024-01-17T22:35:49Z")

</div>

That number looks to be on the order of `eps(Float64)`, so it could easily come about in floating point calculations, even with something simple like `a-b` where `a` and `b` seem equal.
