use super::Float;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
pub struct TrigTable<T: Float> {
pub sin: Vec<T>,
pub cos: Vec<T>,
pub size: usize,
}
impl<T: Float> TrigTable<T> {
#[must_use]
pub fn new(n: usize) -> Self {
let mut sin = Vec::with_capacity(n);
let mut cos = Vec::with_capacity(n);
let theta_step = T::TWO_PI / T::from_usize(n);
for k in 0..n {
let theta = theta_step * T::from_usize(k);
let (s, c) = Float::sin_cos(theta);
sin.push(s);
cos.push(c);
}
Self { sin, cos, size: n }
}
#[inline]
#[must_use]
pub fn sin(&self, k: usize) -> T {
self.sin[k % self.size]
}
#[inline]
#[must_use]
pub fn cos(&self, k: usize) -> T {
self.cos[k % self.size]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trig_table() {
let table: TrigTable<f64> = TrigTable::new(4);
assert!(table.sin(0).abs() < 1e-10);
assert!((table.cos(0) - 1.0).abs() < 1e-10);
assert!((table.sin(1) - 1.0).abs() < 1e-10);
assert!(table.cos(1).abs() < 1e-10);
}
}