Gin
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
FastMath< F > Class Template Reference

Fast approximations for trigonometric and hyperbolic functions. More...

#include <gin_fastmath.h>

Static Public Member Functions

static F fastSin (F x)
 Fast sine wave approximation.
 
static F fastTanh (const F x)
 Fast hyperbolic tangent approximation.
 

Detailed Description

template<class F>
class FastMath< F >

Fast approximations for trigonometric and hyperbolic functions.

FastMath provides optimized approximations of trigonometric and hyperbolic functions that trade accuracy for speed. These are useful in DSP applications where perceptual accuracy is more important than mathematical precision, such as waveshaping, modulation, and real-time synthesis.

Key Features:

Accuracy vs Speed: These approximations are significantly faster than std:: equivalents but introduce small errors. For most audio applications, the errors are inaudible and the speed benefit is worthwhile.

Usage:

// Fast sine for LFO or modulation
float phase = 0.5f; // 0 to 1
float x = (phase * 2.0f - 1.0f) * M_PI; // Convert to -π to π
// Fast tanh for waveshaping/saturation
float input = audioSample * 3.0f;
static F fastTanh(const F x)
Fast hyperbolic tangent approximation.
Definition gin_fastmath.h:124
static F fastSin(F x)
Fast sine wave approximation.
Definition gin_fastmath.h:89
A lightweight 2D point class for projects that don't use juce_graphics.
Definition gin_point.h:25

Credits: Based on algorithms by Mike Jarmy (MIT License)

See also
math namespace

Member Function Documentation

◆ fastSin()

template<class F >
static F FastMath< F >::fastSin ( F  x)
static

Fast sine wave approximation.

Computes an approximation of sin(x) using a polynomial approximation. Provides good accuracy with significantly better performance than std::sin.

Valid Range: -π to π

  • Values outside this range will produce incorrect results
  • For normalized phase (0 to 1), convert to radians first

Algorithm: Based on parabolic approximation with extra precision term.

Parameters
xAngle in radians (must be in range [-π, π])
Returns
Approximation of sin(x) in range [-1.0, 1.0]
See also
https://web.archive.org/web/20100613230051/http://www.devmaster.net/forums/showthread.php?t=5784
https://www.desmos.com/calculator/f0eryaepsl

◆ fastTanh()

template<class F >
static F FastMath< F >::fastTanh ( const F  x)
static

Fast hyperbolic tangent approximation.

Computes an approximation of tanh(x) using a rational polynomial. Particularly useful for waveshaping and soft-clipping in audio applications.

Characteristics:

  • Smooth S-curve from -1 to 1
  • Asymptotically approaches ±1 for large |x|
  • Good for soft saturation and distortion
  • Much faster than std::tanh
  • Valid for all input ranges

Algorithm: Rational polynomial approximation optimized for audio.

Parameters
xInput value (any real number)
Returns
Approximation of tanh(x) in range [-1.0, 1.0]
See also
https://www.kvraudio.com/forum/viewtopic.php?p=5447225#p5447225
https://www.desmos.com/calculator/bjc7zsl4ek

The documentation for this class was generated from the following file: