I have a very simple expression similar to:
a = b ^ c
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
b being positive
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!
put a print statement in. the base of the exponent is negative.
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:
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.
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!
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
b seem equal.