Skip to content

Commit 2e80112

Browse files
ajfrantzalexcrichton
authored andcommitted
Fix sincosf for interval (7*pi/4, 9*pi/4) (#233)
A mistake was made in porting musl's implementation which caused the sin and cos components to be reversed. closes rust-lang/libm#232
1 parent ef9feeb commit 2e80112

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

libm/src/math/sincosf.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ pub fn sincosf(x: f32) -> (f32, f32) {
8989
}
9090
} else {
9191
if sign {
92-
s = k_cosf((x + S4PIO2) as f64);
93-
c = k_sinf((x + S4PIO2) as f64);
92+
s = k_sinf((x + S4PIO2) as f64);
93+
c = k_cosf((x + S4PIO2) as f64);
9494
} else {
95-
s = k_cosf((x - S4PIO2) as f64);
96-
c = k_sinf((x - S4PIO2) as f64);
95+
s = k_sinf((x - S4PIO2) as f64);
96+
c = k_cosf((x - S4PIO2) as f64);
9797
}
9898
}
9999

@@ -133,4 +133,22 @@ mod tests {
133133
_eqf(s.abs(), 0.0).unwrap();
134134
_eqf(c, -1.0).unwrap();
135135
}
136+
137+
#[test]
138+
fn rotational_symmetry() {
139+
use core::f32::consts::PI;
140+
const N: usize = 24;
141+
for n in 0..N {
142+
let theta = 2. * PI * (n as f32) / (N as f32);
143+
let (s, c) = sincosf(theta);
144+
let (s_plus, c_plus) = sincosf(theta + 2. * PI);
145+
let (s_minus, c_minus) = sincosf(theta - 2. * PI);
146+
147+
const TOLERANCE: f32 = 1e-6;
148+
assert!((s - s_plus).abs() < TOLERANCE);
149+
assert!((s - s_minus).abs() < TOLERANCE);
150+
assert!((c - c_plus).abs() < TOLERANCE);
151+
assert!((c - c_minus).abs() < TOLERANCE);
152+
}
153+
}
136154
}

0 commit comments

Comments
 (0)