Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative Exponents throwing NaN in Fortran

Tags:

math

fortran

Very basic Fortran question. The following function returns a NaN and I can't seem to figure out why:

F_diameter = 1. - (2.71828**(-1.0*((-1. / 30.)**1.4)))

I've fed 2.71... in rather than using exp() but they both fail the same way. I've noticed that I only get a NaN when the fractional part (-1 / 30) is negative. Positives evaluate ok.

Thanks a lot

like image 358
micah Avatar asked Dec 27 '22 10:12

micah


1 Answers

The problem is that you are taking a root of a negative number, which would give you a complex answer. This is more obvious if you imagine e.g.

(-1) ** (3/2)

which is equivalent to

(1/sqrt(-1))**3

In other words, your fractional exponent can't trivially operate on a negative number.

like image 160
ire_and_curses Avatar answered Jan 05 '23 02:01

ire_and_curses