Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current complex division leads to wrong results #84

Closed
daingun opened this issue Sep 8, 2020 · 2 comments
Closed

Current complex division leads to wrong results #84

daingun opened this issue Sep 8, 2020 · 2 comments

Comments

@daingun
Copy link

daingun commented Sep 8, 2020

Current complex number division implementation causes intermediate values to over/underflow leading to NaN.

use crate::num_complex::Complex;
use num_complex;
fn main() {
    let a = Complex::<f32>::new(489286830000000000000.0, 0.);
    let b = Complex::<f32>::new(73718980000000000000.0, 0.);
    println!("{}", a / b); // prints NaN+0i, should be around 6.6+0i
}

Please see this papers for a better implementation:
https://www.scilab.org/tutorials/scilab-not-naive-%E2%80%93-tutorial
https://arxiv.org/abs/1210.4539

Here a link to rust playground with a comparison of the two implementations: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1b4d0ea769f0704f0902718796effce4

@cuviper
Copy link
Member

cuviper commented Sep 8, 2020

The problem is mostly that Div is generic for all T: Num, and Rust doesn't have specialization (yet) for us to do anything better for floating point types. This was discussed in #23, and as a result #41 added simple finv() and fdiv() methods for T: Float that do get much closer.

In this playground, the real-only division gets 6.6371894, as does your smith implementation, while fdiv gets 6.63719. I would be open to your improvements in fdiv if you'd like to open a pull request.

@daingun
Copy link
Author

daingun commented Sep 9, 2020

Thank you for the answer, I checked the old issues but i didn't found #23. At the moment I can switch to fdiv().
I'm closing this issue since it is a duplicate, if I implement the Smith's method I will open a pull request.

@daingun daingun closed this as completed Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants