Unexpected powf behaviour

The following code:

fn main() {
    let v = (-0.23280871681335991_f64).powf(-0.0027397260273972603_f64);
    println!("{:?}", v);
    let v = -0.23280871681335991_f64.powf(-0.0027397260273972603_f64);
    println!("{:?}", v);
}

produces this output:

NaN
-1.004001238791221

I'd expect both to be equivalent,but apparently they are not. Is it expected?

let v = -0.23280871681335991_f64.powf(-0.0027397260273972603_f64);

is equivalent to

let v = -(0.23280871681335991_f64.powf(-0.0027397260273972603_f64));
1 Like

Right, but it doesn't explain the NaN.

The result of raising a negative number to a fractional power cannot be represented as a real number, so a NaN is returned; this is expected.

4 Likes

Now it makes sense.
Thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.