SPH
Classes | Functions | Variables
MathUtils.h File Reference

Additional math routines (with more includes). More...

#include "common/Assert.h"
#include "math/MathBasic.h"
#include <cmath>
#include <cstring>
#include <limits>
#include <utility>

Go to the source code of this file.

Classes

struct  Eps< float >
 
struct  Eps< double >
 

Functions

template<typename T >
INLINEsqrtInv (const T &f)
 
template<typename T >
INLINEsqrtApprox (const T f)
 Returns an approximative value of square root. More...
 
template<typename T >
constexpr INLINEsqr (const T &f) noexcept
 Return a squared value. More...
 
constexpr INLINE bool isPower2 (const Size n) noexcept
 Returns true if given n is a power of 2. N must at least 1. More...
 
template<typename T >
INLINEsqrt (const T f)
 Return a squared root of a value. More...
 
INLINE Float cbrt (const Float f)
 Returns a cubed root of a value. More...
 
INLINE int positiveMod (const int i, const int n)
 Returns a positive modulo value. More...
 
template<int N>
INLINE Float root (const Float f)
 
template<>
INLINE Float root< 1 > (const Float f)
 
template<>
INLINE Float root< 2 > (const Float f)
 
template<>
INLINE Float root< 3 > (const Float f)
 
template<>
INLINE Float root< 4 > (const Float f)
 
template<int N>
constexpr INLINE Float pow (const Float v)
 Power for floats. More...
 
template<>
constexpr INLINE Float pow< 0 > (const Float)
 
template<>
constexpr INLINE Float pow< 1 > (const Float v)
 
template<>
constexpr INLINE Float pow< 2 > (const Float v)
 
template<>
constexpr INLINE Float pow< 3 > (const Float v)
 
template<>
constexpr INLINE Float pow< 4 > (const Float v)
 
template<>
constexpr INLINE Float pow< 5 > (const Float v)
 
template<>
constexpr INLINE Float pow< 6 > (const Float v)
 
template<>
constexpr INLINE Float pow< 7 > (const Float v)
 
template<>
constexpr INLINE Float pow< 8 > (const Float v)
 
template<>
constexpr INLINE Float pow<-1 > (const Float v)
 
template<>
constexpr INLINE Float pow<-2 > (const Float v)
 
template<>
constexpr INLINE Float pow<-3 > (const Float v)
 
template<>
constexpr INLINE Float pow<-4 > (const Float v)
 
template<>
constexpr INLINE Float pow<-5 > (const Float v)
 
template<>
constexpr INLINE Float pow<-8 > (const Float v)
 
template<>
constexpr INLINE Float pow<-16 > (const Float v)
 
template<int N>
constexpr INLINE Size pow (const Size v)
 Power for ints. More...
 
template<>
constexpr INLINE Size pow< 0 > (const Size)
 
template<>
constexpr INLINE Size pow< 1 > (const Size v)
 
template<>
constexpr INLINE Size pow< 2 > (const Size v)
 
template<>
constexpr INLINE Size pow< 3 > (const Size v)
 
template<>
constexpr INLINE Size pow< 4 > (const Size v)
 
template<>
constexpr INLINE Size pow< 5 > (const Size v)
 
template<>
constexpr INLINE Size pow< 6 > (const Size v)
 
template<typename T >
INLINEpow (const T value, const T power)
 Mathematical functions. More...
 
INLINE Float powFastest (const Float value, const Float power)
 
INLINE Float powFast (Float value, const Float power)
 
template<typename T >
INLINEexp (const T f)
 
template<typename T >
INLINE auto abs (const T &f)
 
template<>
INLINE auto abs (const int &f)
 
template<>
INLINE auto abs (const Size &f)
 
template<typename T >
INLINEcos (const T f)
 
template<typename T >
INLINEsin (const T f)
 
template<typename T >
INLINEtan (const T f)
 
template<typename T >
INLINEacos (const T f)
 
template<typename T >
INLINEasin (const T f)
 
template<typename T >
INLINEatan (const T f)
 
template<typename T >
INLINEatan2 (const T y, const T x)
 
template<typename T >
INLINEexp10 (const T f)
 
template<typename T >
INLINElog10 (const T f)
 
template<typename T >
INLINE int sgn (const T val)
 
template<typename T , typename TAmount >
INLINElerp (const T v1, const T v2, const TAmount amount)
 
template<typename T >
INLINE auto floor (const T &f)
 
template<typename T >
INLINE auto ceil (const T &f)
 
template<typename T >
INLINE auto round (const T &f)
 
constexpr INLINE Float sphereVolume (const Float radius)
 Computes a volume of a sphere given its radius. More...
 
constexpr INLINE Float sphereSurfaceArea (const Float radius)
 Computes a surface area of a sphere given its radius. More...
 
INLINE bool almostEqual (const Float &f1, const Float &f2, const Float &eps=EPS)
 

Variables

constexpr Float EPS = Eps<Float>::value
 
constexpr Float LARGE = 1.e20f
 
constexpr Float INFTY = std::numeric_limits<Float>::max()
 
constexpr Float PI = 3.14159265358979323846264338327950288419716939937510582097_f
 Mathematical constants. More...
 
constexpr Float PI_INV = 1._f / PI
 
constexpr Float E = 2.718281828459045235360287471352662497757247093699959574967_f
 
constexpr Float SQRT_3 = 1.732050807568877293527446341505872366942805253810380628055_f
 
constexpr Float DEG_TO_RAD = PI / 180._f
 
constexpr Float RAD_TO_DEG = 180._f / PI
 

Detailed Description

Additional math routines (with more includes).

Author
Pavel Sevecek (sevecek at sirrah.troja.mff.cuni.cz)
Date
2016-2021

Definition in file MathUtils.h.

Function Documentation

◆ abs() [1/3]

template<>
INLINE auto abs ( const int &  f)

Definition at line 281 of file MathUtils.h.

◆ abs() [2/3]

template<>
INLINE auto abs ( const Size f)

Definition at line 286 of file MathUtils.h.

◆ abs() [3/3]

template<typename T >
INLINE auto abs ( const T &  f)

Computes absolute value.

Note
Return type must be auto as abs(TracelessTensor) != TracelessTensor

Definition at line 276 of file MathUtils.h.

◆ acos()

template<typename T >
INLINE T acos ( const T  f)

Definition at line 306 of file MathUtils.h.

◆ almostEqual()

INLINE bool almostEqual ( const Float f1,
const Float f2,
const Float eps = EPS 
)

Checks if two values are equal to some given accuracy.

Note
We use <= rather than < on purpose as EPS for integral types is zero.

Definition at line 386 of file MathUtils.h.

◆ asin()

template<typename T >
INLINE T asin ( const T  f)

Definition at line 311 of file MathUtils.h.

◆ atan()

template<typename T >
INLINE T atan ( const T  f)

Definition at line 316 of file MathUtils.h.

◆ atan2()

template<typename T >
INLINE T atan2 ( const T  y,
const T  x 
)

Definition at line 321 of file MathUtils.h.

◆ cbrt()

INLINE Float cbrt ( const Float  f)

Returns a cubed root of a value.

Definition at line 84 of file MathUtils.h.

◆ ceil()

template<typename T >
INLINE auto ceil ( const T &  f)

Definition at line 351 of file MathUtils.h.

◆ cos()

template<typename T >
INLINE T cos ( const T  f)

Definition at line 291 of file MathUtils.h.

◆ exp()

template<typename T >
INLINE T exp ( const T  f)

Definition at line 269 of file MathUtils.h.

◆ exp10()

template<typename T >
INLINE T exp10 ( const T  f)

Definition at line 326 of file MathUtils.h.

◆ floor()

template<typename T >
INLINE auto floor ( const T &  f)

Definition at line 346 of file MathUtils.h.

◆ isPower2()

constexpr INLINE bool isPower2 ( const Size  n)
constexprnoexcept

Returns true if given n is a power of 2. N must at least 1.

Definition at line 72 of file MathUtils.h.

◆ lerp()

template<typename T , typename TAmount >
INLINE T lerp ( const T  v1,
const T  v2,
const TAmount  amount 
)

Definition at line 341 of file MathUtils.h.

◆ log10()

template<typename T >
INLINE T log10 ( const T  f)

Definition at line 331 of file MathUtils.h.

◆ positiveMod()

INLINE int positiveMod ( const int  i,
const int  n 
)

Returns a positive modulo value.

Definition at line 89 of file MathUtils.h.

◆ pow() [1/3]

template<int N>
constexpr INLINE Float pow ( const Float  v)
constexpr

Power for floats.

◆ pow() [2/3]

template<int N>
constexpr INLINE Size pow ( const Size  v)
constexpr

Power for ints.

◆ pow() [3/3]

template<typename T >
INLINE T pow ( const T  value,
const T  power 
)

Mathematical functions.

Definition at line 222 of file MathUtils.h.

◆ pow< 0 >() [1/2]

template<>
constexpr INLINE Float pow< 0 > ( const  Float)
constexpr

Definition at line 120 of file MathUtils.h.

◆ pow< 0 >() [2/2]

template<>
constexpr INLINE Size pow< 0 > ( const  Size)
constexpr

Definition at line 190 of file MathUtils.h.

◆ pow< 1 >() [1/2]

template<>
constexpr INLINE Float pow< 1 > ( const Float  v)
constexpr

Definition at line 124 of file MathUtils.h.

◆ pow< 1 >() [2/2]

template<>
constexpr INLINE Size pow< 1 > ( const Size  v)
constexpr

Definition at line 194 of file MathUtils.h.

◆ pow< 2 >() [1/2]

template<>
constexpr INLINE Float pow< 2 > ( const Float  v)
constexpr

Definition at line 128 of file MathUtils.h.

◆ pow< 2 >() [2/2]

template<>
constexpr INLINE Size pow< 2 > ( const Size  v)
constexpr

Definition at line 198 of file MathUtils.h.

◆ pow< 3 >() [1/2]

template<>
constexpr INLINE Float pow< 3 > ( const Float  v)
constexpr

Definition at line 132 of file MathUtils.h.

◆ pow< 3 >() [2/2]

template<>
constexpr INLINE Size pow< 3 > ( const Size  v)
constexpr

Definition at line 202 of file MathUtils.h.

◆ pow< 4 >() [1/2]

template<>
constexpr INLINE Float pow< 4 > ( const Float  v)
constexpr

Definition at line 136 of file MathUtils.h.

◆ pow< 4 >() [2/2]

template<>
constexpr INLINE Size pow< 4 > ( const Size  v)
constexpr

Definition at line 206 of file MathUtils.h.

◆ pow< 5 >() [1/2]

template<>
constexpr INLINE Float pow< 5 > ( const Float  v)
constexpr

Definition at line 140 of file MathUtils.h.

◆ pow< 5 >() [2/2]

template<>
constexpr INLINE Size pow< 5 > ( const Size  v)
constexpr

Definition at line 210 of file MathUtils.h.

◆ pow< 6 >() [1/2]

template<>
constexpr INLINE Float pow< 6 > ( const Float  v)
constexpr

Definition at line 144 of file MathUtils.h.

◆ pow< 6 >() [2/2]

template<>
constexpr INLINE Size pow< 6 > ( const Size  v)
constexpr

Definition at line 214 of file MathUtils.h.

◆ pow< 7 >()

template<>
constexpr INLINE Float pow< 7 > ( const Float  v)
constexpr

Definition at line 148 of file MathUtils.h.

◆ pow< 8 >()

template<>
constexpr INLINE Float pow< 8 > ( const Float  v)
constexpr

Definition at line 152 of file MathUtils.h.

◆ pow<-1 >()

template<>
constexpr INLINE Float pow<-1 > ( const Float  v)
constexpr

Definition at line 156 of file MathUtils.h.

◆ pow<-16 >()

template<>
constexpr INLINE Float pow<-16 > ( const Float  v)
constexpr

Definition at line 180 of file MathUtils.h.

◆ pow<-2 >()

template<>
constexpr INLINE Float pow<-2 > ( const Float  v)
constexpr

Definition at line 160 of file MathUtils.h.

◆ pow<-3 >()

template<>
constexpr INLINE Float pow<-3 > ( const Float  v)
constexpr

Definition at line 164 of file MathUtils.h.

◆ pow<-4 >()

template<>
constexpr INLINE Float pow<-4 > ( const Float  v)
constexpr

Definition at line 168 of file MathUtils.h.

◆ pow<-5 >()

template<>
constexpr INLINE Float pow<-5 > ( const Float  v)
constexpr

Definition at line 172 of file MathUtils.h.

◆ pow<-8 >()

template<>
constexpr INLINE Float pow<-8 > ( const Float  v)
constexpr

Definition at line 176 of file MathUtils.h.

◆ powFast()

INLINE Float powFast ( Float  value,
const Float  power 
)

Approximative version of pow function, slightly more precise than powFastest.

Expected error is about 2 %. Works only for positive values and positive powers.

Definition at line 246 of file MathUtils.h.

◆ powFastest()

INLINE Float powFastest ( const Float  value,
const Float  power 
)

Approximative version of pow function.

Expected error is about 5 %. Works only for positive values and positive powers.

Definition at line 229 of file MathUtils.h.

◆ root()

template<int N>
INLINE Float root ( const Float  f)

◆ root< 1 >()

template<>
INLINE Float root< 1 > ( const Float  f)

Definition at line 97 of file MathUtils.h.

◆ root< 2 >()

template<>
INLINE Float root< 2 > ( const Float  f)

Definition at line 101 of file MathUtils.h.

◆ root< 3 >()

template<>
INLINE Float root< 3 > ( const Float  f)

Definition at line 105 of file MathUtils.h.

◆ root< 4 >()

template<>
INLINE Float root< 4 > ( const Float  f)
Todo:
is this faster than pow(f, 0.25)?

Definition at line 109 of file MathUtils.h.

◆ round()

template<typename T >
INLINE auto round ( const T &  f)

Definition at line 356 of file MathUtils.h.

◆ sgn()

template<typename T >
INLINE int sgn ( const T  val)

Definition at line 336 of file MathUtils.h.

◆ sin()

template<typename T >
INLINE T sin ( const T  f)

Definition at line 296 of file MathUtils.h.

◆ sphereSurfaceArea()

constexpr INLINE Float sphereSurfaceArea ( const Float  radius)
constexpr

Computes a surface area of a sphere given its radius.

Definition at line 380 of file MathUtils.h.

◆ sphereVolume()

constexpr INLINE Float sphereVolume ( const Float  radius)
constexpr

Computes a volume of a sphere given its radius.

Definition at line 375 of file MathUtils.h.

◆ sqr()

template<typename T >
constexpr INLINE T sqr ( const T &  f)
constexprnoexcept

Return a squared value.

Definition at line 67 of file MathUtils.h.

◆ sqrt()

template<typename T >
INLINE T sqrt ( const T  f)

Return a squared root of a value.

Definition at line 78 of file MathUtils.h.

◆ sqrtApprox()

template<typename T >
INLINE T sqrtApprox ( const T  f)

Returns an approximative value of square root.

Definition at line 57 of file MathUtils.h.

◆ sqrtInv()

template<typename T >
INLINE T sqrtInv ( const T &  f)

Returns an approximate value of inverse square root.

Template Parameters
ItersNumber of iterations of the algorithm. Higher value = more accurate result.

Definition at line 44 of file MathUtils.h.

◆ tan()

template<typename T >
INLINE T tan ( const T  f)

Definition at line 301 of file MathUtils.h.

Variable Documentation

◆ DEG_TO_RAD

constexpr Float DEG_TO_RAD = PI / 180._f
constexpr

Definition at line 369 of file MathUtils.h.

◆ E

constexpr Float E = 2.718281828459045235360287471352662497757247093699959574967_f
constexpr

Definition at line 365 of file MathUtils.h.

◆ EPS

constexpr Float EPS = Eps<Float>::value
constexpr

Definition at line 30 of file MathUtils.h.

◆ INFTY

constexpr Float INFTY = std::numeric_limits<Float>::max()
constexpr

Largest value representable by float/double. Any increase (multiplication by 2, for example) will cause float overflow! Use carefully, maily for comparisons.

Definition at line 38 of file MathUtils.h.

◆ LARGE

constexpr Float LARGE = 1.e20f
constexpr

Large value (compared with 1). It's safe to do basic arithmetic operations (multiply by 2, for example) without worying about float overflow.

Definition at line 34 of file MathUtils.h.

◆ PI

constexpr Float PI = 3.14159265358979323846264338327950288419716939937510582097_f
constexpr

Mathematical constants.

Definition at line 361 of file MathUtils.h.

◆ PI_INV

constexpr Float PI_INV = 1._f / PI
constexpr

Definition at line 363 of file MathUtils.h.

◆ RAD_TO_DEG

constexpr Float RAD_TO_DEG = 180._f / PI
constexpr

Definition at line 371 of file MathUtils.h.

◆ SQRT_3

constexpr Float SQRT_3 = 1.732050807568877293527446341505872366942805253810380628055_f
constexpr

Definition at line 367 of file MathUtils.h.