Numerical classes provide all sort of number operations and functions. The hermes::Constants
, for example, offer you common constants:
hermes::Constants::pi;
hermes::Constants::pi_over_four;
hermes::Constants::machine_epsilon;
Numbers
The hermes::Numbers
namespace, included by hermes/numeric/numeric.h
, provides lots of functions to work with floating-point, integer and binary number representations. Most functions are template functions. Here is an incomplete list of what you can find there, for more functions, please check the documentation of hermes::Numbers
.
- Type representation limits ```cpp hermes::Numbers::lowest<T>(); hermes::Numbers::greatest<T>(); ```
- Functions
hermes::Numbers::pow<N>(x);
static HERMES_DEVICE_CALLABLE real_t fract(real_t x)
Extract decimal fraction from x.
Definition: numeric.h:524
static constexpr HERMES_DEVICE_CALLABLE T cube(T a)
Computes square.
Definition: numeric.h:197
static HERMES_DEVICE_CALLABLE T clamp(const T &n, const T &l, const T &u)
Clamps value to closed interval.
Definition: numeric.h:176
static HERMES_DEVICE_CALLABLE int sign(T a)
Computes sign.
Definition: numeric.h:202
static constexpr HERMES_DEVICE_CALLABLE T sqr(T a)
Definition: numeric.h:192
static constexpr HERMES_DEVICE_CALLABLE T max(const T &a, const T &b)
Computes maximum between two numbers.
Definition: numeric.h:127
static HERMES_DEVICE_CALLABLE f32 safe_sqrt(f32 x)
Computes square root with clamped input.
Definition: numeric.h:693
- Binary representations
static HERMES_DEVICE_CALLABLE int floatSignificand(f32 v)
Extracts significand bits.
Definition: numeric.h:332
static HERMES_DEVICE_CALLABLE f32 bitsToFloat(uint32_t ui)
Fills a f32 variable data.
Definition: numeric.h:356
static HERMES_DEVICE_CALLABLE u8 countHexDigits(T n)
Counts hexadecimal digits.
Definition: numeric.h:161
static HERMES_DEVICE_CALLABLE int floatExponent(f32 v)
Extracts exponent from floating-point number.
Definition: numeric.h:326
static HERMES_DEVICE_CALLABLE uint32_t floatToBits(f32 f)
Interprets a floating-point value into a integer type.
Definition: numeric.h:344
static HERMES_DEVICE_CALLABLE uint32_t floatSignBit(f32 v)
Extracts sign bit.
Definition: numeric.h:338
static HERMES_DEVICE_CALLABLE u32 interleaveBits(u32 x, u32 y, u32 z)
Interleaves bits of three integers.
Definition: numeric.h:311
- Rounding and precision
hermes::Numbers::ceilToInt(x);
hermes::Numbers::floorToInt(x);
hermes::Numbers::roundToInt(x);
...
hermes::Numbers::divRoundDonw(x, y);
static HERMES_DEVICE_CALLABLE f32 nextFloatUp(f32 v)
Computes the next greater representable floating-point value.
Definition: numeric.h:392
static HERMES_DEVICE_CALLABLE real_t subRoundUp(real_t a, real_t b)
Subtracts and rounds up to the next float value.
Definition: numeric.h:639
static constexpr HERMES_DEVICE_CALLABLE real_t gamma(i32 n)
Computes conservative bounds in error.
Definition: numeric.h:747
static HERMES_DEVICE_CALLABLE f32 nextFloatDown(f32 v)
Computes the next smaller representable floating-point value.
Definition: numeric.h:411
static HERMES_DEVICE_CALLABLE real_t sqrtRoundDown(real_t a)
Computes square root rounded down to the next smaller float value.
Definition: numeric.h:653
Trigonometry
A few trigonometric functions are listed in hermes::Trigonometry
:
static constexpr HERMES_DEVICE_CALLABLE real_t radians2degrees(real_t a)
Converts radians to degrees.
Definition: numeric.h:798
static HERMES_DEVICE_CALLABLE f32 safe_acos(f32 x)
Computes acos with clamped input.
Definition: numeric.h:810
Checks
You can make some floating-number comparisons as well using hermes::Check
:
float x, y;
static constexpr bool is_between(T x, T a, T b)
Checks if a number is in a open interval.
Definition: numeric.h:866
static constexpr bool is_between_closed(T x, T a, T b)
Checks if a number is in a closed interval.
Definition: numeric.h:875
static constexpr HERMES_DEVICE_CALLABLE bool is_zero(T a)
Checks if number is 0.
Definition: numeric.h:834
static HERMES_DEVICE_CALLABLE std::enable_if_t< std::is_floating_point< T >::value, bool > is_nan(T v)
Checks if number representation is nan
Definition: numeric.h:884
static constexpr HERMES_DEVICE_CALLABLE bool is_equal(T a, T b)
Checks if two numbers are at most 1e-8 apart.
Definition: numeric.h:847
Interpolation
Check hermes::interpolation
namespace for interpolation functions.
hermes::interpolation::smooth(a, b);
hermes::interpolation::smoothStep(a, b, v);
hermes::interpolation::sharpen(a, b);
hermes::interpolation::linearStep(v, a, b);
hermes::interpolation::mix(v, a, b);
hermes::interpolation::lerp(t, a, b);
hermes::interpolation::nearest(t, a, b);
hermes::monotonicCubicInterpolation(...);
Arithmetic Interval
hermes::Interval
represents a numeric interval that supports interval arithmetic. It is very useful when you want to represent your values considering the uncertainty that comes from the limitations of the computer representations of floating-point numbers. This way, hermes::Interval
represents a closed interval [low, high]
that guarantees to enclose your real numerical value, by providing all sorts of arithmetic operations of intervals.
int main() {
auto c = a * b;
return 0;
}
HERMES_DEVICE_CALLABLE Interval sqrt() const
Computes arithmetic interval square root.
Definition: interval.h:157
HERMES_DEVICE_CALLABLE Interval sqr() const
Computes arithmetic interval square.
Definition: interval.h:147
You can also make some queries:
a.contains(0);
a.center();
a.radius();
a.width();
a.isExact();
EFloat
For certain calculations, you may want to keep track of floating-point operation errors accumulated over time. The hermes::EFloat
class uses the hermes::Interval
to carry error bounds during floating-point operations.
int main() {
auto c = a * 3 + a * b - 4 * b + a / b;
c.absoluteError();
c.upperBound();
c.lowerBound();
return 0;
}
Represents a value with error bounds.
Definition: e_float.h:47
Floating-point with attached error.