Function fixed_trigonometry::cos
source · [−]pub fn cos<T>(x: T) -> T where
T: FixedSigned + MixedNum + MixedNumSigned, Expand description
Calculate cosine using a Taylor approximation of cos(x).
Cos is calculated by adding a phase shift to x and running it through the polynomial sine method.
Argument
x- The value to apply the operation to.
x is wrapped to the -π=<x<π range in the function.
Example
use fixed_trigonometry::*;
use fixed::{types::extra::U18, FixedI32};
let mut x = FixedI32::<U18>::from_num(0);
let mut y = cos(x);
assert_eq!{ y.to_num::<f32>(), 1.0000038 };
x = FixedI32::<U18>::from_num(3.1415/2.0);
y = cos(x);
assert_eq!{ y.to_num::<f32>(), 0.00004196167 };Comparison and Error
The figure below shows the comparison between the polynomial cosine, and the std::f32::cos implementation.
The Difference between the two is plotted as the error.

The error of the method is compared to the sine implementation in the cordic crate.
The comparison is done for U22 signed fixed point.
The figure below is missing numbers on the y axis, but it is plotted on a linear scale, showing the relative error between the two methods.
