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

cast between complex return None #93

Open
serendipity-crypto opened this issue Jul 23, 2021 · 3 comments
Open

cast between complex return None #93

serendipity-crypto opened this issue Jul 23, 2021 · 3 comments

Comments

@serendipity-crypto
Copy link

I am writing a function, which uses the cast between "Complex" and other "Num"s such as "u64, f64, Complex". The cast between "Complex" and primitive will be correct. But how can I easily cast between "Complex" and "Complex"? This always return a "None" and leads panic.

    let a: Complex64;
    let b: Complex64 = Complex64 { re: (1.), im: (2.) };
    a = cast::<Complex64, Complex64>(b).unwrap();
    println!("{}", a);
@cuviper
Copy link
Member

cuviper commented Jul 23, 2021

Hmm, NumCast won't work because it goes through ToPrimitive, so you get None when there's an imaginary part.

I think we would ideally implement something like TryFrom<Complex<U>> for Complex<T> where T: TryFrom<U>, but that will probably conflict with the reflexive case, T = U. That leaves us with manually (or by macro) expanding a bunch of combinations, which is gross, but possible. Or we could just add a direct conversion method on Complex itself.

@cuviper cuviper transferred this issue from rust-num/num-traits Jul 23, 2021
@serendipity-crypto
Copy link
Author

Thanks for your idea. I will try to implement direct conversion method on Complex later. 😊

@Rikorose
Copy link

Hi, not sure what the current state is, but casting a complex value to a higher precision would be nice to have (like f32 as f64):

let a = Complex32{re: 1., im: 1.};
let b = a as Complex64;

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

3 participants