pub fn sin<T>(x: T) -> T where
    T: FixedSigned + MixedNum + MixedNumConversion<i32> + MixedOps
Expand description

Calculate sin(x) using a Taylor approximation of sin(x).

Sin is calculated using the following polynomial:

sin(x) = x -( x^3/6 )+( x^5/120 )-( x^7/5040 )+( x^9/362880 )

Argument

  • x - The value to apply the operation to.

x must be wrapped to the -π=<x<π range.

Example

use fixed_trigonometry::*;
use fixed::{types::extra::U22, FixedI32};
 
let mut x = FixedI32::<U22>::from_num(0);
let mut y = sin(x);
assert_eq!{ y.to_num::<f32>(), 0.0 };
 
x = FixedI32::<U22>::from_num(3.1415/2.0);
y = sin(x);
assert_eq!{ y.to_num::<f32>(), 1.0000036 };
 
x = FixedI32::<U22>::from_num(3.1415);
y = sin(x);
assert_eq!{ y.to_num::<f32>(), 9.250641e-5 };

Comparison and Error

The figure below shows the comparison between the polynomial sine, and the std::f32::sin implementation. The Difference between the two is plotted as the error.

Alt version

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.

Alt version