F64 powf detecting undefined results

My knowledge of mathematics says to me that if I attempt to perform (-5)^0.5, the result should be undefined, or some imaginary number (in this case ~ -2.23i). When I attempt to do this with rust:

-5.0f64.powf(0.5f64)

The result is -2.23606797749979, without anything to indicate that this is an imaginary number and would normally be undefined.

Is there an efficient way to determine whether or not powf() results in an imaginary number/undefined result?

The operator precedence of your code is wrong - it is calculating the square root of 5 and then negating that: -(5.0f64.powf(0.5f64)). If you correct that, you get a NaN as expected: Rust Playground

4 Likes