PMath.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2018 Digital Barriers plc. All rights reserved.
3  * Contact: http://www.digitalbarriers.com/
4  *
5  * This file is part of the Papillon SDK.
6  *
7  * You can't use, modify or distribute any part of this file without
8  * the explicit written agreements of Digital Barriers.
9  */
10 
15 #pragma once
16 #include <PapillonCommon.h>
17 #define _USE_MATH_DEFINES
18 #include <math.h>
19 
20 PAPILLON_BEGIN_NAMESPACE
21 
32 class PAPILLON_API_CORE PMath
33 {
34 private:
35  PMath(); // private constructor, this class cannot be instantiated
36 
37 public:
41  static float NaNf() { return std::numeric_limits<float>::quiet_NaN(); }
42 
46  static double NaNd() { return std::numeric_limits<double>::quiet_NaN(); }
47 
51  static papillon::int32 IntMin();
52 
56  static papillon::int32 IntMax();
57 
61  static bool IsNaN(float v) { return v != v; }
62 
66  static bool IsNaN(double v) { return v != v; }
67 
71  static int Clamp(papillon::int32 value, papillon::int32 min, papillon::int32 max) { if (value < min) return min; if (value > max) return max; return value; }
72 
76  static float Clamp(float value, float min, float max) { if (value < min) return min; if (value > max) return max; return value; }
77 
81  static double Clamp(double value, double min, double max) { if (value < min) return min; if (value > max) return max; return value; }
82 
86  static double Sigmoid(double x) { return 1.0 / (1.0 + std::exp(-x)); }
87 
94  static float LERP(float value, float min, float max) { return value * (max - min) + min; }
95 
102  static double LERP(double value, double min, double max) { return value * (max - min) + min; }
103 
109  static bool RandBool();
110 
116  static papillon::uint32 RandUInt32();
117 
123  static papillon::uint64 RandUInt64();
124 
130  static papillon::uint64 RandUInt63();
131 
140  static bool AreAlmostEqual(float v1, float v2, float absoluteError = 1e-5f) { return ::fabs(v1 - v2) <= absoluteError; }
141 
148  static bool AreAlmostEqual(double v1, double v2, double absoluteError = 1e-7) { return ::fabs(v1 - v2) <= absoluteError; }
149 
154  static bool AreAlmostEqualULP(float v1, float v2, papillon::int32 maxULP = 4);
155 
159  static papillon::uint32 GCD(papillon::uint32 a, papillon::uint32 b) { return b ? GCD(b, a%b) : a; }
160 
164  static bool IsPow2(papillon::uint32 n) { return (n != 0) && ((n & (n - 1)) == 0); }
165 
169  static papillon::uint32 Pow2Up(papillon::uint32 n);
170 
175  static papillon::uint32 Pow2Down(papillon::uint32 n);
176 
180  static papillon::uint32 Pow2Nearest(papillon::uint32 n);
181 
185  static bool IsNthBitSet(papillon::int32 value, papillon::int32 n) { return (value & (1 << n)) != 0; }
186 
190  static papillon::int32 SetNthBit(papillon::int32 value, papillon::int32 n) { return value | (1 << n); }
191 
195  static papillon::int32 UnsetNthBit(papillon::int32 value, papillon::int32 n) { return value & ~(1 << n); }
196 };
197 
198 PAPILLON_END_NAMESPACE
static bool IsNaN(double v)
Returns true if the specified number is NaN (Not A Number), false otherwise.
Definition: PMath.h:66
static float Clamp(float value, float min, float max)
Clamps the value to the specified range.
Definition: PMath.h:76
static float NaNf()
Returns NaN (Not a Number) value for float numbers.
Definition: PMath.h:41
static bool IsNaN(float v)
Returns true if the specified number is NaN (Not A Number), false otherwise.
Definition: PMath.h:61
static float LERP(float value, float min, float max)
Performs a linear interpolation (v = v * (max - min) + min).
Definition: PMath.h:94
static double NaNd()
Returns NaN (Not a Number) value for double numbers.
Definition: PMath.h:46
The PMath class is a toolbox to do some standard numeric operations (clamp, NaN handling, linear interpolation, random numbers...)
Definition: PMath.h:32
static double Clamp(double value, double min, double max)
Clamps the value to the specified range.
Definition: PMath.h:81
static papillon::uint32 GCD(papillon::uint32 a, papillon::uint32 b)
Returns Greatest Common Divisor between 2 unsigned integers.
Definition: PMath.h:159
static papillon::int32 SetNthBit(papillon::int32 value, papillon::int32 n)
Sets the nth bit of the specified value.
Definition: PMath.h:190
static bool IsNthBitSet(papillon::int32 value, papillon::int32 n)
Returns true if the nth bit of the specified value is set, false otherwise.
Definition: PMath.h:185
static bool IsPow2(papillon::uint32 n)
Returns true if the integer argument is a power of 2.
Definition: PMath.h:164
static double Sigmoid(double x)
Computes the sigmoid (or logistic) function of x: y=1/(1+e^(-x))
Definition: PMath.h:86
static bool AreAlmostEqual(double v1, double v2, double absoluteError=1e-7)
Returns true if and only if the two specified values are equal according to the specified absolute er...
Definition: PMath.h:148
static double LERP(double value, double min, double max)
Performs a linear interpolation (v = v * (max - min) + min).
Definition: PMath.h:102
static bool AreAlmostEqual(float v1, float v2, float absoluteError=1e-5f)
Returns true if and only if the two specified values are equal according to the specified absolute er...
Definition: PMath.h:140
static int Clamp(papillon::int32 value, papillon::int32 min, papillon::int32 max)
Clamps the value to the specified range.
Definition: PMath.h:71
static papillon::int32 UnsetNthBit(papillon::int32 value, papillon::int32 n)
Unsets the nth bit of the specified value.
Definition: PMath.h:195