rust-num / num-complex

Complex numbers for Rust
Apache License 2.0
232 stars 50 forks source link

Potential bug in complex exp() function #103

Closed JorisDeRidder closed 2 years ago

JorisDeRidder commented 2 years ago

The following code

use num::complex::Complex;

fn main() {

    let z = Complex::<f64>::new(f64::NEG_INFINITY, f64::INFINITY);
    println!("z = {:?}", z);
    println!("exp(z) = {:?}", z.exp());
}

returns

z = Complex { re: -inf, im: inf }
exp(z) = Complex { re: NaN, im: NaN }

while its C++ equivalent

#include <iostream>
#include <complex>

using namespace std;

int main() {

    complex<double> z(-INFINITY, INFINITY);
    cout << "z = " << z << endl;
    cout << "exp(z) = " << exp(z) << endl;

    return 0;
}

returns a different outcome:

z = (-inf,inf)
exp(z) = (0,0)

Might it be that the special values are not properly implemented in num?

cuviper commented 2 years ago

You're right, it is not trying to handle "special" values at all, directly computing: ea + i b = ea (cos(b) + i sin(b))

cuviper commented 2 years ago

Fixed by #104.