Module trig

Source
Expand description

The trig module contains functions for performing accurate trigonometry calculations.

The accuracy of the libm::sin function is poor for angles >= π/4 and the accuracy of the libm::cos function is poor for small angles, i.e., < π/4. So sin π/4 is explicitly set to 1/√2 and cos values are calculated from sin values using Pythagoras’ theorem.

The sincos function accurately calculates the sine and cosine of angles in radians by using remquo to reduce an angle into the range: -π/4 <= angle <= π/4; and its quadrant: along the positive or negative, x or y axis of the unit circle. The sincos_diff function reduces the round-off error of the difference of two angles in radians using the 2Sum algorithm.

The sincosd function is the degrees equivalent of sincos and sincosd_diff is the degrees equivalent of sincos_diff.

The sines and cosines of angles are represented by the UnitNegRange struct which ensures that they lie in the range: -1.0 <= value <= 1.0.

The functions arctan2 and arctan2d are the reciprocal of sincos and sincosd, transforming sine and cosines of angles into radians or degrees respectively.

The module contains the other trigonometric functions: tan, cot, sec and csc as functions taking sin and/or cos and returning an Option<f64> to protect against divide by zero.

The module also contains functions for:

Structs§

UnitNegRange
The UnitNegRange newtype an f64. A valid UnitNegRange value lies between -1.0 and +1.0 inclusive.

Constants§

COS_30_DEGREES
The cosine of 30 degrees: √3/2
MAX_COS_ANGLE_IS_ONE
The maximum angle in Radians where: swap_sin_cos(libm::sin(value)) == 1.0
MAX_LINEAR_SIN_ANGLE
The maximum angle in Radians where: libm::sin(value) == value
SQRT_3
core::f64::consts::SQRT_3 is currently a nightly-only experimental API, see https://doc.rust-lang.org/core/f64/consts/constant.SQRT_3.html
SQ_EPSILON
ε * ε, a very small number.

Functions§

arctan2
Accurately calculate an angle in Radians from its sine and cosine.
arctan2d
Accurately calculate an angle in Degrees from its sine and cosine.
calculate_adjacent_length
Calculates the length of the other side in a right angled triangle, given the length of one side and the hypotenuse.
cosine
Calculate the cosine of an angle in Radians using the sine of the angle.
cosine_diff
Calculate the cosine of the difference of two angles: a - b.
cosine_from_sine
Calculate the cosine of an angle from it’s sine and the sign of the cosine.
cosine_sum
Calculate the cosine of the sum of two angles: a + b.
cot
The cotangent of an angle.
csc
The cosecant of an angle.
sec
The secant of an angle.
sincos
Calculate the sine and cosine of an angle from a value in Radians.
sincos_diff
Calculate the sine and cosine of an angle from the difference of a pair of values in Radians.
sincosd
Calculate the sine and cosine of an angle from a value in Degrees.
sincosd_diff
Calculate the sine and cosine of an angle from the difference of a pair of values in Degrees.
sine
Calculate the sine of an angle in Radians.
sine_diff
Calculate the sine of the difference of two angles: a - b.
sine_sum
Calculate the sine of the sum of two angles: a + b.
spherical_adjacent_length
Calculates the length of the other side in a right angled spherical triangle, given the length of one side and the hypotenuse.
spherical_cosine_rule
Calculate the length of the adjacent side of a right angled spherical triangle, given the cosine of the angle and length of the hypotenuse.
spherical_hypotenuse_length
Calculates the length of the hypotenuse in a right angled spherical triangle, given the length of both sides.
sq_cosine_half
Square of the cosine of half the Angle.
sq_sine_half
Square of the sine of half the Angle.
swap_sin_cos
Swap the sine into the cosine of an angle and vice versa.
tan
The tangent of an angle.