Skip to content

Commit c13bf87

Browse files
rafaelhaRafael Haenel
andauthored
bug: Fix issue in Scalar::phase (zxcalc#75)
In my previous PR I used `num::integer::sqrt(2)` which simply rounded to 1. This is fixed now. Co-authored-by: Rafael Haenel <[email protected]>
1 parent 9a6c6cb commit c13bf87

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

quizx/src/scalar.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
use approx::AbsDiffEq;
1818
use num::complex::Complex;
19-
use num::integer::sqrt;
2019
pub use num::traits::identities::{One, Zero};
2120
use num::{integer, Integer, Rational64};
2221
use std::cmp::min;
@@ -175,8 +174,8 @@ impl<T: Coeffs> Scalar<T> {
175174
if coeffs.len() == 4 {
176175
// cases where the phase is a multiple of 1/4 are handled exactly
177176
match coeffs.iter_coeffs().collect::<Vec<_>>().as_slice() {
178-
[a, b, 0, c] if -b == *c => {
179-
return Phase::new(((-a - b * sqrt(2)).signum() as i64 + 1) / 2)
177+
[_, b, 0, c] if -b == *c => {
178+
return Phase::new(if self.complex_value().re > 0.0 { 0 } else { 1 })
180179
}
181180
[0, c, 0, 0] => {
182181
return Phase::new(Rational64::new(if *c > 0 { 1 } else { 5 }, 4))
@@ -810,8 +809,16 @@ mod tests {
810809
#[case(ScalarN::from_int_coeffs(&[-2, 0, -2, 0]))]
811810
#[case(ScalarN::from_int_coeffs(&[0, 1, 0, 1]))]
812811
#[case(ScalarN::from_int_coeffs(&[0, 1, 0, -1]))]
812+
#[case(ScalarN::from_int_coeffs(&[0, -1, 0, 1]))]
813813
#[case(ScalarN::from_int_coeffs(&[0, -2, 0, -2]))]
814814
#[case(ScalarN::from_int_coeffs(&[0, 2, 0, -2]))]
815+
#[case(ScalarN::from_int_coeffs(&[1, 1, 0, -1]))]
816+
#[case(ScalarN::from_int_coeffs(&[1, 1, 0, 1]))]
817+
#[case(ScalarN::from_int_coeffs(&[1, -1, 0, 1]))]
818+
#[case(ScalarN::from_int_coeffs(&[1, 1, 0, -1]))]
819+
#[case(ScalarN::from_int_coeffs(&[2, -1, 0, 1]))]
820+
#[case(ScalarN::from_int_coeffs(&[-2, 1, 0, 1]))]
821+
#[case(ScalarN::from_int_coeffs(&[2, 2, 0, -2]))]
815822
#[case(ScalarN::from_int_coeffs(&[-1, 2, 3, -4]))]
816823
fn get_phase(#[case] s: ScalarN) {
817824
assert_abs_diff_eq!(s.phase().to_f64(), s.complex_value().arg() / PI);

0 commit comments

Comments
 (0)