# Trigonometry API

Source: https://sh4zam.com/api/trig/

> Trig functions and utilities built around the SH4 FSCA instruction

The following API provides a series of routines implementing trigonometric functions. While some of these are specialized, many are meant to be drop-in replacements for the equivalent functions provided by `<math.h>`. The SH4 provides a single instruction, `FSCA`, for retrieving fast approximations of both the sine and cosine of a given angle, so when you need both values it is most efficient to grab them at once as a `shz_sincos_t` pair. Angles are in radians by default; `_deg` variants take degrees and `u16` variants take the 16-bit fixed-point angle that `FSCA` consumes natively.

- **C header:** `#include <sh4zam/shz_trig.h>`
- **C++ header:** `#include <sh4zam/shz_trig.hpp>`
- **C++ types:** [`shz::sincos`](https://sh4zam.com/api/cpp/sincos/)
- **Symbols:** 51

## Suffixes

Suffixes used in this module. The full list is in [Naming and Suffixes](https://sh4zam.com/concepts/naming-and-suffixes/).

- `_deg`: Angle argument is in degrees. The default is radians.
- `u16`: Angle is a 16-bit fixed-point value: 0..65535 maps to 0..2π, the native `FSCA` input format.
- `_fsrra`: Uses `FSRRA` (fast approximate 1/sqrt(x)). The argument or denominator must be positive. Faster, less precise.

## Sin/Cos Pairs

Routines involving precomputed sine + cosine pairs.

| Symbol | Description |
| --- | --- |
| [`shz_sincosu16`](https://sh4zam.com/api/trig/shz_sincosu16/) | Returns sinf()/cosf() pairs for the given unsigned 16-bit angle in radians. |
| [`shz_sincosf`](https://sh4zam.com/api/trig/shz_sincosf/) | Returns sinf()/cosf() pairs for the given floating-point angle in radians. |
| [`shz_sincosf_deg`](https://sh4zam.com/api/trig/shz_sincosf_deg/) | Returns sinf/cosf() pairs for the given floating-point angle in degrees. |
| [`shz_sincos_tanf`](https://sh4zam.com/api/trig/shz_sincos_tanf/) | Returns tanf() from the given pre-computed `sincos` pair. |
| [`shz_sincos_secf`](https://sh4zam.com/api/trig/shz_sincos_secf/) | Returns the floating-point secant (1.0f / cosf(x)) from the given pre-computed `sincos` pair. |
| [`shz_sincos_cscf`](https://sh4zam.com/api/trig/shz_sincos_cscf/) | Returns the floating-point cosecant (1.0f / sinf(x)) from the given pre-computed `sincos` pair. |
| [`shz_sincos_cotf`](https://sh4zam.com/api/trig/shz_sincos_cotf/) | Returns the floating-point cotangent (1.0f / tanf(x)) from the given pre-computed `sincos` pair. |

## Independent Functions

Routines providing single trigonometric functions.

| Symbol | Description |
| --- | --- |
| [`shz_sinf`](https://sh4zam.com/api/trig/shz_sinf/) | One-off routine for returning only sinf() from an angle in radians. |
| [`shz_sinf_deg`](https://sh4zam.com/api/trig/shz_sinf_deg/) | One-off routine for returning only sinf() from an angle in degrees. |
| [`shz_cosf`](https://sh4zam.com/api/trig/shz_cosf/) | One-off routine for returning only cosf() from an angle in radians. |
| [`shz_cosf_deg`](https://sh4zam.com/api/trig/shz_cosf_deg/) | One-off routine for returning only cosf() from an angle in degrees. |
| [`shz_tanf`](https://sh4zam.com/api/trig/shz_tanf/) | One-off routine for returning only tanf() from an angle in radians. |
| [`shz_tanf_deg`](https://sh4zam.com/api/trig/shz_tanf_deg/) | One-off routine for returning only tanf() from an angle in degrees. |
| [`shz_secf`](https://sh4zam.com/api/trig/shz_secf/) | One-off routine for returning only secant (1 / cos(x)) from an angle in radians. |
| [`shz_secf_deg`](https://sh4zam.com/api/trig/shz_secf_deg/) | One-off routine for returning only secant (1 / cos(x)) from an angle in degrees. |
| [`shz_cscf`](https://sh4zam.com/api/trig/shz_cscf/) | One-off routine for returning only cosecant (1 / sin(x)) from an angle in radians. |
| [`shz_cscf_deg`](https://sh4zam.com/api/trig/shz_cscf_deg/) | One-off routine for returning only cosecant (1 / sin(x)) from an angle in degrees. |
| [`shz_cotf`](https://sh4zam.com/api/trig/shz_cotf/) | One-off routine for returning only cotangent (1 / tan(x)) from an angle in radians. |
| [`shz_cotf_deg`](https://sh4zam.com/api/trig/shz_cotf_deg/) | One-off routine for returning only cotangent (1 / cot(x)) from an angle in degrees. |

## Inverse Functions

Routines providing inverse trigonometric functions.

| Symbol | Description |
| --- | --- |
| [`shz_asinf`](https://sh4zam.com/api/trig/shz_asinf/) | Fast arcsine approximation; equivalent to C's asinf(). |
| [`shz_acosf`](https://sh4zam.com/api/trig/shz_acosf/) | Fast arccosine approximation; equivalent to C's acosf(). |
| [`shz_atanf_unit`](https://sh4zam.com/api/trig/shz_atanf_unit/) | Fast arctangent approximation for unit values between `0.0f` and `1.0f`. |
| [`shz_atanf_q1`](https://sh4zam.com/api/trig/shz_atanf_q1/) | Fast arctangent approximation for values lying within the first quadrant (`>= 0.0f`). |
| [`shz_atanf`](https://sh4zam.com/api/trig/shz_atanf/) | Fast arctangent approximation; equvalent to C's atanf(). |
| [`shz_atan2f`](https://sh4zam.com/api/trig/shz_atan2f/) | Computes arctangent of `y / x`, using the signs of arguments to determine correct quadtrant. Equivalent to C's atan2f(). |
| [`shz_asecf`](https://sh4zam.com/api/trig/shz_asecf/) | Fast arcsecant/inverse secant approximation, taking units in radians. |
| [`shz_acscf`](https://sh4zam.com/api/trig/shz_acscf/) | Fast arccosecant/inverse cosecant approximation; taking units in radians. |
| [`shz_acotf`](https://sh4zam.com/api/trig/shz_acotf/) | Fast arccotangent/inverse cotangent approximation; taking units in radians. |

## Hyperbolic Functions

Trigonometric functions for hyperbolas

| Symbol | Description |
| --- | --- |
| [`shz_sinhf`](https://sh4zam.com/api/trig/shz_sinhf/) | Fast hyperbolic sine function. |
| [`shz_coshf`](https://sh4zam.com/api/trig/shz_coshf/) | Fast hyperbolic cosine function. |
| [`shz_tanhf`](https://sh4zam.com/api/trig/shz_tanhf/) | Fast hyperbolic tangent function. |
| [`shz_cschf`](https://sh4zam.com/api/trig/shz_cschf/) | Fast hyperbolic cosecant function. |
| [`shz_sechf`](https://sh4zam.com/api/trig/shz_sechf/) | Fast hyperbolic secant function. |
| [`shz_cothf`](https://sh4zam.com/api/trig/shz_cothf/) | Fast hyperbolic cotangent function. |

## Inverse Hyperbolic Functions

Inverse trigonometric functions for hyperbolas

| Symbol | Description |
| --- | --- |
| [`shz_asinhf`](https://sh4zam.com/api/trig/shz_asinhf/) | Fast hyperbolic arcsine function. |
| [`shz_acoshf`](https://sh4zam.com/api/trig/shz_acoshf/) | Fast hyperbolic arccosine function. |
| [`shz_atanhf`](https://sh4zam.com/api/trig/shz_atanhf/) | Fast hyperbolic arctangent function. |
| [`shz_acschf`](https://sh4zam.com/api/trig/shz_acschf/) | Fast hyperbolic arccosecant function. |
| [`shz_asechf`](https://sh4zam.com/api/trig/shz_asechf/) | Fast hyperbolic arcsecant function. |
| [`shz_acothf`](https://sh4zam.com/api/trig/shz_acothf/) | Fast hyperbolic arccotangent function. |

## Macros

| Symbol | Description |
| --- | --- |
| [`SHZ_F_PI`](https://sh4zam.com/api/trig/SHZ_F_PI/) | Single-precision floating-point PI approximation (do not use M_PI!). |
| [`SHZ_F_TAU`](https://sh4zam.com/api/trig/SHZ_F_TAU/) | Single-precision FP PI approximation times 2. |
| [`SHZ_F_PI_2`](https://sh4zam.com/api/trig/SHZ_F_PI_2/) | Single-precision FP PI approximation divided by 2. |
| [`SHZ_F_PI_4`](https://sh4zam.com/api/trig/SHZ_F_PI_4/) | Single-precision FP PI approximation divided by 4. |
| [`SHZ_FSCA_RAD_FACTOR`](https://sh4zam.com/api/trig/SHZ_FSCA_RAD_FACTOR/) | Multiplicative factor for passing the FSCA instruction angles in radians. |
| [`SHZ_FSCA_DEG_FACTOR`](https://sh4zam.com/api/trig/SHZ_FSCA_DEG_FACTOR/) | Multiplicative factor for passing the FSCA instrution angles in degrees. |
| [`SHZ_FSCA_ERROR_MAX`](https://sh4zam.com/api/trig/SHZ_FSCA_ERROR_MAX/) | Maximum FP error for FSCA instruction. |
| [`SHZ_DEG_TO_RAD`](https://sh4zam.com/api/trig/SHZ_DEG_TO_RAD/) | Converts the given angle in degrees to radians. |
| [`SHZ_RAD_TO_DEG`](https://sh4zam.com/api/trig/SHZ_RAD_TO_DEG/) | Converts the given angle in radians to degrees. |

## Types

| Symbol | Description |
| --- | --- |
| [`shz_sincos_t`](https://sh4zam.com/api/trig/shz_sincos_t/) | Pair representing the sine and cosine of a given angle. |
| [`shz_sincos`](https://sh4zam.com/api/trig/shz_sincos/) | Alternate C [`shz_sincos_t`](https://sh4zam.com/api/trig/shz_sincos_t/) typedef for those who hate POSIX style. |
