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§
- Unit
NegRange - The
UnitNegRange
newtype an f64. A validUnitNegRange
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.