fixed_point128 1.0.1.0
128 bit integer, fixed point and floating point arithmetic for C++20
Loading...
Searching...
No Matches
fp128::fixed_point128< I > Class Template Reference

Google test class (friend). More...

#include <fixed_point128.h>

Public Types

typedef fixed_point128< I > type
 Self type alias.
typedef fixed_point128< I > * ptr_type
 Pointer type alias.
typedef fixed_point128< I > & ref_type
 Reference type alias.

Public Member Functions

constexpr void get_components (uint64_t &l, uint64_t &h) const noexcept
 Reads the raw representation: the two QWORDs of the 128 bit two's complement value.
Constructors
constexpr fixed_point128 () noexcept
 Default constructor, creates an instance with a value of zero.
constexpr fixed_point128 (const fixed_point128 &rhs) noexcept
 Copy constructor.
template<int32_t I2>
constexpr fixed_point128 (const fixed_point128< I2 > &rhs) noexcept
 cross-template Copy constructor, can be used between two different fixed_point128 templates A value too large for the destination wraps silently, as everywhere else in this class. Fraction bits the destination cannot hold are rounded to nearest, a tie going away from zero.
constexpr fixed_point128 (fixed_point128 &&rhs) noexcept
 Move constructor Doesn't modify the right hand side object. Acts like a copy constructor.
constexpr fixed_point128 (double x) noexcept
 Constructor from the double type Underflow goes to zero. Overflow, NaN and +-INF saturate to the extreme value carrying the sign of the input: max() for a positive one and lowest() for a negative one. Not constexpr: the exponent and mantissa are read through the Double union, and reading the inactive member of a union is not allowed during constant evaluation.
constexpr fixed_point128 (uint64_t x) noexcept
 Constructor from uint64_t type A value of 2^I or above overflows into the sign and wraps silently.
constexpr fixed_point128 (int64_t x) noexcept
 Constructor from int64_t type The shift is done in the unsigned domain, where it wraps rather than overflowing, and a negative value carries its sign bits along with it: scaling a two's complement value by a power of two is the same left shift its magnitude needs.
constexpr fixed_point128 (uint32_t x) noexcept
 Constructor from uint32_t type.
constexpr fixed_point128 (int32_t x) noexcept
 Constructor from int32_t type.
 fixed_point128 (const char *x) noexcept
 Constructor from const char* (C string).
 fixed_point128 (const std::string &x) noexcept
 Constructor from std::string. Accurate to 37 digits after the decimal point. Allows creating very high precision values. Much slower than the rhs constructors.
constexpr fixed_point128 (uint64_t l, uint64_t h) noexcept
 Constructor from the two QWORDs of the raw value, useful for creating very small constants. The pair is read as a 128 bit two's complement integer scaled by 2^-F, so a negative value is spelled with the MSB of h set rather than with a separate sign.
constexpr ~fixed_point128 () noexcept=default
 Destructor.
constexpr fixed_point128 & operator= (const fixed_point128 &rhs) noexcept
 Assignment operator.
constexpr fixed_point128 & operator= (fixed_point128 &&rhs) noexcept
 Move assignment operator.
template<int32_t I2>
constexpr fixed_point128< I > & operator= (const fixed_point128< I2 > &rhs) noexcept
 cross-template assignment operator, can be used between two different fixed_point128 templates Behaves as the cross-template constructor: a value too large for this instantiation wraps, and fraction bits it cannot hold are rounded to nearest, a tie going away from zero.
Conversion Operators
constexpr operator int64_t () const noexcept
 operator int64_t - converts to a int64_t The fraction is truncated towards zero, as the builtin floating point to integer conversions do, rather than shifted away - which would round a negative value towards minus infinity.
constexpr operator uint64_t () const noexcept
 operator uint64_t - converts to a uint64_t A negative value wraps modulo 2^64, the way a builtin signed to unsigned conversion does.
constexpr operator int32_t () const noexcept
 operator int32_t - converts to a int32_t
constexpr operator uint32_t () const noexcept
 operator uint32_t - converts to a uint32_t
constexpr operator float () const noexcept
 operator float - converts to a float Not constexpr: the result is assembled through the Float union, and writing one member of a union and reading another is not allowed during constant evaluation.
constexpr operator double () const noexcept
 operator double - converts to a double The whole value range is within reach of a double's exponent, so nothing overflows or goes denormal. Not constexpr: the result is assembled through the Double union, and writing one member of a union and reading another is not allowed during constant evaluation.
constexpr operator long double () const noexcept
 operator long double - converts to a long double
 operator std::string () const noexcept
 Converts to a std::string (slow) string holds all meaningful fraction bits.
 operator char * () const noexcept
 Converts to a C string (slow) string holds all meaningful fraction bits.
Arithmetic Operators
template<typename T>
constexpr fixed_point128 operator>> (T shift) const noexcept
 Performs right shift operation.
template<typename T>
constexpr fixed_point128 operator<< (T shift) const noexcept
 Performs left shift operation. Hinted rather than forced, see operator>> above.
constexpr fixed_point128 & operator+= (const fixed_point128 &rhs) noexcept
 Add a value to this object.
template<typename T>
constexpr fixed_point128 & operator+= (const T &rhs) noexcept
 Add a value to this object.
constexpr fixed_point128 & operator-= (const fixed_point128 &rhs) noexcept
 Subtract a value to this object.
template<typename T>
constexpr fixed_point128 & operator-= (const T &rhs) noexcept
 Subtract a value to this object.
constexpr fixed_point128 & operator*= (const fixed_point128 &rhs) noexcept
 Multiplies a value to this object.
constexpr fixed_point128 & square () noexcept
 Squares this object in place.
template<typename T>
constexpr fixed_point128 & operator*= (T x) noexcept
 Multiplies a value to this object.
template<>
constexpr fixed_point128 & operator*= (uint64_t x) noexcept
 Multiplies a 64 bit value to this object The product is taken modulo 2^128, which is the signed product of a two's complement value by an unsigned one: multiplying is repeated addition and addition already wraps correctly.
fixed_point128 & operator/= (const fixed_point128 &rhs)
 Divide this object by x. The quotient is rounded to nearest, a tie going away from zero, whichever of the three paths below computes it.
template<typename T>
fixed_point128 & operator/= (T x)
 Divide this object by x.
template<>
fixed_point128 & operator/= (double x)
 Divide this object by x.
template<>
fixed_point128 & operator/= (uint64_t x)
 Divide this object by x. The quotient is rounded to nearest, a tie going away from zero: the same result as dividing by fixed_point128(x), which the integer divisor only makes cheaper.
fixed_point128 & operator%= (const fixed_point128 &rhs)
 %= operator, the remainder of a truncating division: x - y * trunc(x / y).
template<typename T>
fixed_point128 & operator%= (T rhs)
 Performs modulo with a generic type by converting to fixed_point128.
constexpr fixed_point128 & operator>>= (int32_t shift) noexcept
 Shift right this object. The shift is arithmetic: the sign bit is replicated, so the result is the value divided by 2^shift and rounded to nearest, a tie going away from zero - the rounding rule of the multiply and the divisions.
constexpr fixed_point128 & operator<<= (int32_t shift) noexcept
 Shift left this object. A value shifted past the range of the type wraps silently, sign included.
constexpr fixed_point128 & operator&= (const fixed_point128 &rhs) noexcept
 Bitwise AND= The operators below work on all 128 bits, the sign bit included.
template<typename T>
constexpr fixed_point128 & operator&= (const T &rhs)
 Bitwise AND=.
constexpr fixed_point128 & operator|= (const fixed_point128 &rhs) noexcept
 Bitwise OR=.
template<typename T>
constexpr fixed_point128 & operator|= (const T &rhs)
 Bitwise OR=.
constexpr fixed_point128 & operator^= (const fixed_point128 &rhs) noexcept
 Bitwise XOR=.
template<typename T>
constexpr fixed_point128 & operator^= (const T &rhs)
 Bitwise XOR=.
constexpr fixed_point128 & operator++ () noexcept
 Prefix ++ operation (++a)
constexpr fixed_point128 operator++ (int32_t) noexcept
 Postfix ++ operation (a++)
constexpr fixed_point128 & operator-- () noexcept
 Prefix – operation (–a)
constexpr fixed_point128 operator-- (int32_t) noexcept
 Postfix – operation (a–)
constexpr operator bool () const noexcept
 Convert to bool.
constexpr bool operator! () const noexcept
 Logical not (!). Opposite of operator bool.
constexpr fixed_point128 operator~ () const noexcept
 Bitwise not (~). Flips all 128 bits, the sign included, which makes it -x - epsilon().
constexpr fixed_point128 operator+ () const noexcept
 Unary +. Returns a copy of the object.
constexpr fixed_point128 operator- () const noexcept
 Unary -. Returns the two's complement of the object. The most negative value has no positive counterpart and is returned unchanged, the same silent wrap the builtin integer types perform.

Static Public Attributes

static constexpr int32_t F = 127 - I
 Fraction bit count (127 - I). The remaining bit is the sign.
static constexpr uint64_t max_int_value = FP128_MAX_VALUE_64(I)
 Largest representable positive integer, 2^I - 1.

Friends

template<int32_t I2>
class fixed_point128
class fp128_gtest
template<int32_t J>
std::istream & operator>> (std::istream &is, fixed_point128< J > &value)
Binary Math Operators (Friend)
template<typename T>
constexpr fixed_point128 operator+ (fixed_point128 lhs, const T &rhs) noexcept
 Adds 2 values and returns the result.
template<typename T>
constexpr fixed_point128 operator- (fixed_point128 lhs, const T &rhs) noexcept
 subtracts the right hand side operand to this object to and returns the result.
template<typename T>
constexpr fixed_point128 operator* (fixed_point128 lhs, const T &rhs) noexcept
 Multiplies the right hand side operand with this object to and returns the result.
template<typename T>
fixed_point128 operator/ (fixed_point128 lhs, const T &rhs)
 Divides this object by the right hand side operand and returns the result.
template<typename T>
fixed_point128 operator% (fixed_point128 lhs, const T &rhs)
 Calculates modulo.
Binary Bitwise Operators (Friend)
template<typename T>
constexpr fixed_point128 operator& (fixed_point128 lhs, const T &rhs)
 Performs bitwise AND (&)
template<typename T>
constexpr fixed_point128 operator| (fixed_point128 lhs, const T &rhs)
 Performs bitwise OR (|)
template<typename T>
constexpr fixed_point128 operator^ (fixed_point128 lhs, const T &rhs)
 Performs bitwise XOR (^)
Binary Operators With The Scalar On The Left Hand Side (Friend)

Overloads that accept a builtin arithmetic type as the left operand.

Without these, an expression like (1 + x) is ambiguous: converting the literal to fixed_point128 and converting x to a builtin type are both one user defined conversion, so neither overload wins. Restricting the left operand to the arithmetic types keeps these from competing with the fixed_point128 on the left versions above, which would otherwise be an equally good match. The comparison operators already carry the same pair of overloads.

The scalar is widened to fixed_point128 rather than the object being narrowed to a builtin type, so (1 - x) keeps the fraction of x instead of truncating it away.

template<typename T>
requires std::is_arithmetic_v<T>
constexpr fixed_point128 operator+ (const T &lhs, const fixed_point128 &rhs) noexcept
 Adds a scalar and a fixed_point128, in that order.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr fixed_point128 operator- (const T &lhs, const fixed_point128 &rhs) noexcept
 Subtracts a fixed_point128 from a scalar.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr fixed_point128 operator* (const T &lhs, const fixed_point128 &rhs) noexcept
 Multiplies a scalar and a fixed_point128, in that order.
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator/ (const T &lhs, const fixed_point128 &rhs)
 Divides a scalar by a fixed_point128.
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator% (const T &lhs, const fixed_point128 &rhs)
 Performs modulo of a scalar by a fixed_point128.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr fixed_point128 operator& (const T &lhs, const fixed_point128 &rhs)
 Performs bitwise AND of a scalar and a fixed_point128.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr fixed_point128 operator| (const T &lhs, const fixed_point128 &rhs)
 Performs bitwise OR of a scalar and a fixed_point128.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr fixed_point128 operator^ (const T &lhs, const fixed_point128 &rhs)
 Performs bitwise XOR of a scalar and a fixed_point128.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr fixed_point128 operator<< (const T &lhs, const fixed_point128 &rhs) noexcept
 Shifts a scalar left by a fixed_point128 shift count.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr fixed_point128 operator>> (const T &lhs, const fixed_point128 &rhs) noexcept
 Shifts a scalar right by a fixed_point128 shift count. The left operand is widened to fixed_point128 first, see operator<< above.
Comparison Operators
constexpr bool operator== (const fixed_point128 &lhs, const fixed_point128 &rhs) noexcept
 Compare logical/bitwise equal.
template<typename T>
constexpr bool operator== (const fixed_point128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator== (const T &lhs, const fixed_point128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator!= (const fixed_point128 &lhs, const fixed_point128 &rhs) noexcept
 Return true when objects are not equal. Can be used as logical XOR.
template<typename T>
constexpr bool operator!= (const fixed_point128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator!= (const T &lhs, const fixed_point128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator< (const fixed_point128 &lhs, const fixed_point128 &rhs) noexcept
 Return true if this object is small than the rhs.
template<typename T>
constexpr bool operator< (const fixed_point128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator< (const T &lhs, const fixed_point128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator<= (const fixed_point128 &lhs, const fixed_point128 &rhs) noexcept
 Return true this object is small or equal than the rhs.
template<typename T>
constexpr bool operator<= (const fixed_point128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator<= (const T &lhs, const fixed_point128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator> (const fixed_point128 &lhs, const fixed_point128 &rhs) noexcept
 Return true this object is larger than the rhs.
template<typename T>
constexpr bool operator> (const fixed_point128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator> (const T &lhs, const fixed_point128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator>= (const fixed_point128 &lhs, const fixed_point128 &rhs) noexcept
 Return true this object is larger or equal than the rhs.
template<typename T>
constexpr bool operator>= (const fixed_point128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator>= (const T &lhs, const fixed_point128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Friend Math Functions

CRT-style math functions implemented as friend functions.

constexpr fixed_point128 fabs (const fixed_point128 &x)
 Returns the absolute value.
constexpr fixed_point128 floor (const fixed_point128 &x)
 Performs the floor() function, similar to libc's floor(), rounds down towards -infinity.
constexpr fixed_point128 ceil (const fixed_point128 &x)
 Performs the ceil() function, similar to libc's ceil(), rounds up towards infinity.
constexpr fixed_point128 trunc (const fixed_point128 &x)
 Rounds towards zero.
constexpr fixed_point128 round (const fixed_point128 &x)
 Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.
constexpr int32_t ilogb (const fixed_point128 &x)
 Retrieves an integer that represents the base-2 exponent of the specified value. floor(log2(|x|)), as the C function: the sign of x makes no difference.
constexpr fixed_point128 copysign (const fixed_point128 &x, const fixed_point128 &y)
 returns the value of x with the sign of y.
fixed_point128 fmod (const fixed_point128 &x, const fixed_point128 &y)
 Performs the fmod() function, similar to libc's fmod(): x - y * trunc(x / y). Exact, and the result keeps the sign of x. See operator%=().
constexpr fixed_point128 modf (const fixed_point128 &x, fixed_point128 *iptr)
 Split into integer and fraction parts. Both results carry the sign of the input variable.
constexpr fixed_point128 fdim (const fixed_point128 &x, const fixed_point128 &y)
 Determines the positive difference between the first and second values.
constexpr fixed_point128 fmin (const fixed_point128 &x, const fixed_point128 &y)
 Returns the minimum between x and y.
constexpr fixed_point128 fmax (const fixed_point128 &x, const fixed_point128 &y)
 Returns the maximum between x and y.
fixed_point128 hypot (const fixed_point128 &x, const fixed_point128 &y)
 Calculates the hypotenuse. i.e. sqrt(x^2 + y^2)
constexpr fixed_point128 sqr (fixed_point128 x) noexcept
 Calculates the square of a value. i.e. x^2.
constexpr uint64_t lzcnt128 (const fixed_point128 &x)
 Calculates the left zero count of value x, ignoring the sign. Counted on the magnitude, so a negative value gives the same answer as its opposite rather than the zero its two's complement bits would produce.
fixed_point128 sqrt (const fixed_point128 &x, uint32_t iterations=3)
 Calculates the square root, correctly rounded.
void fact_reciprocal (int x, fixed_point128 &res)
 Factorial reciprocal (inverse). Calculates 1 / x! Maximum value of x that may produce non zero values is 34. This value depends on the amount of fraction bits. The table is parsed from decimal strings, so it carries the accuracy of the string constructor: within 0.5 + 23 / 2^(I-1) ulp of the exact 1 / x!, measured 17.5 ulp at I = 1 and 0.5 from I = 16 up.
fixed_point128 reciprocal (const fixed_point128 &x)
 Calculates the reciprocal of a value. y = 1 / x.
void _sincos_cordic (fixed_point128 x, fixed_point128 &sin_x, fixed_point128 &cos_x, bool apply_scale_factor) noexcept
 Calculate Sine and Cosine using CORDIC using a limited range of [-pi/2, pi/2] 64 iterations, one per entry of the arctangent table, which give an absolute accuracy of about 2^-63: measured within 1.1e-19 of mpmath for |x| < 1. The table used to skip arctan(2^-62), which held the error near 1.8e-19. Not used by tan(), which divides sin() by cos().
fixed_point128 sin1 (fixed_point128 x) noexcept
 Calculate the sine function over a limited range [-0.5pi, 0.5pi] Using the Maclaurin series expansion, the formula is: x^3 x^5 x^7 sin(x) = x - — + — - — + ... 3! 5! 7! The series runs in a wider fraction than this instantiation has, see TrigSeries().
fixed_point128 cos1 (const fixed_point128 &x) noexcept
 Calculate the cosine function over a limited range [-0.5pi, 0.5pi] Using the Maclaurin series expansion, the formula is: x^2 x^4 x^6 cos(x) = 1 - — + — - — + ... 2! 4! 6! Evaluated directly rather than as sin1(pi/2 - x), which carried the rounding error of pi/2 into every result; cos1(0) is now exactly one. See TrigSeries().
fixed_point128 sin (fixed_point128 x)
 Calculate the Sine function Ultimately uses sin1() or cos1() with a reduced range of [-pi/4, pi/4].
fixed_point128 asin (fixed_point128 x)
 Calculate the inverse sine function Newton's method on sin() for |x| up to one half, see AsinNewton(). Beyond that the identity asin(x) = pi/2 - 2 asin(sqrt((1 - x) / 2)) keeps the iteration away from pi/2, where the derivative it divides by vanishes and the refinement stalled at double precision: asin(1) was 4e16 ulp off at fixed_point128<16>. The square root is correctly rounded, so the identity costs a few ulp at most.
fixed_point128 cos (fixed_point128 x)
 Calculate the cosine function Ultimately uses sin1() or cos1() with a reduced range of [-pi/4, pi/4]. cos(0) is exactly one.
fixed_point128 acos (fixed_point128 x)
 Calculate the inverse cosine function pi/2 - asin(x) for |x| up to one half. Beyond that acos(x) = 2 asin(sqrt((1 - x) / 2)) for a positive x and pi - 2 asin(sqrt((1 + x) / 2)) for a negative one, which keep Newton's method away from the ends of the domain: acos(-1) was 8e16 ulp off at fixed_point128<16>.
fixed_point128 tan (fixed_point128 x)
 Calculate the tangent function tan(x) = sin(x)/cos(x)
fixed_point128 atan (fixed_point128 x)
 Calculate the inverse tangent function Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 2.2 ulp.
fixed_point128 atan2 (fixed_point128 y, fixed_point128 x)
 Calculate the inverse tangent function of the ratio y / x Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 3.3 ulp.
fixed_point128 sinh (const fixed_point128 &x)
 Calculate the hyperbolic sine function Use the exponent function which produces more accurate results than the power series. e^x - e^(-x) sinh(x) = ---------— 2 Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 10 * max(1, |sinh(x)|) ulp at I = 4 and 3 * max(1, |sinh(x)|) ulp from I = 16 up. e^x itself has to fit the range, so a result between 2^(I-1) and 2^I overflows.
fixed_point128 asinh (const fixed_point128 &x)
 Calculates the inverse hyperbolic sine For positive x: asinh(x) = log(x + sqrt(x^2 + 1)) For negative x, the function returns the result with the sign inverted. The root is exact before it is rounded (RootOfSquareAndOne()) and the sum is kept from overflowing (LogOfSum()), so every argument works, lowest() included. Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 over the whole range: within 2.8 ulp.
fixed_point128 cosh (const fixed_point128 &x)
 Calculate the hyperbolic cosine function e^x + e^(-x) cosh(x) = ---------— 2 Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 10 * max(1, cosh(x)) ulp at I = 4 and 3 * max(1, cosh(x)) ulp from I = 16 up. e^|x| itself has to fit the range, see sinh().
fixed_point128 acosh (const fixed_point128 &x)
 Calculates the inverse hyperbolic cosine For x >= 1: acosh(x) = log(x + sqrt(x^2 - 1)) For x < 1, the function return zero Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 over the whole range: within 2.9 ulp. It was out by up to 5e8 ulp just above one, where x^2 - 1 cancels.
fixed_point128 tanh (const fixed_point128 &x)
 Calculates the hyperbolic tangent e^x - e^(-x) 1 - e^(-2|x|) tanh(x) = ---------— = ------------—, with the sign of x e^x + e^(-x) 1 + e^(-2|x|) The second form is the one evaluated, for every argument. Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 4.2 ulp.
fixed_point128 atanh (const fixed_point128 &x)
 Calculates the inverse hyperbolic tangent 1 + x atanh(x) = 0.5 * log( --—) = 0.5 * (log(1 + x) - log(1 - x)) 1 - x The second form is the one evaluated. Both of its logarithms take an argument in (0, 2), inside the range of every instantiation, while the quotient of the first form leaves the range as x approaches one - beyond 0.88 at fixed_point128<4>, within 2^-15 of one at fixed_point128<16> - and the wrapped value made log() throw out of this noexcept function, terminating the process. Accuracy, measured against mpmath at I = 4, 16, 32, 62 and 63: within 1.4 ulp. Below I = 4 the same holds where log(1 - x) fits the range, which log() needs for its log2 (see log()): for |x| up to 0.75 at I = 1 and 0.9375 at I = 2.
fixed_point128 exp (const fixed_point128 &x)
 Calculates the exponent of x: e Using the Maclaurin series expansion, the formula is: 1 2 3 x x x exp(x) = 1 + — + — + — + ... 1! 2! 3!
fixed_point128 expm1 (const fixed_point128 &x)
 Calculates the exponent of x and reduces 1 from the result: (e^x) - 1 Evaluated as exp(x) - 1, which in a fixed point type loses nothing to cancellation: the absolute error is that of exp(), within 9.3 * max(1, |e^x - 1|) ulp measured at I = 4 and 6.2 * max(1, e^x) ulp from I = 16 up.
fixed_point128 exp2 (const fixed_point128 &x)
 Computes 2 to the power of x exp(x * ln(2)), the constant applied with 128 fraction bits. Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 5.5 * max(1, 2^x) ulp.
fixed_point128 pow (const fixed_point128 &x, const fixed_point128 &y)
 Computes x to the power of y exp(y * log(x)). Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 7.3 * max(1, x^y) ulp at I = 4 and 4.4 * max(1, x^y) ulp from I = 16 up. Exact powers are not exact: pow(2, 3) is 8 - 15 ulp at I = 16. y * log(x) has to fit the range.
constexpr fixed_point128 log2 (fixed_point128 x)
 Calculates the Log base 2 of x: y = log2(x) Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 2 ulp at I = 1, 1.7 ulp at I = 2 and 1.1 ulp from I = 4 up. The compile time evaluation takes a different, bitwise path that truncates instead: it can be one ulp below the runtime result. A result outside the range wraps, which a narrow instantiation meets for small arguments: log2(x) needs x >= 2^(-2^I).
fixed_point128 log (fixed_point128 x)
 Calculates the natural Log (base e) of x: log(x) log2(x) * ln(2), the constant applied with 128 fraction bits by MulByFraction128() - a constant held to this instantiation's F bits, multiplied by a log2 of up to 63, would cost tens of ulp. Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 1.9 ulp. log2(x) has to fit the range as well, so a narrow instantiation cannot take the log of an argument below 2^(-2^I) even when its log would fit: at I = 1 that is x below 0.25.
fixed_point128 log1p (fixed_point128 x)
 Calculates the natural Log (base e) of 1 + x: log(1 + x) Not noexcept: it forwards to log(), which rejects a non positive argument. Marking it noexcept would turn that domain_error into a call to std::terminate. 1 + x is exact in a fixed point type, so the error is that of log(): within 1.2 ulp measured, with log()'s limit on small arguments at I < 7.
fixed_point128 log10 (fixed_point128 x)
 Calculates Log base 10 of x: log10(x) log2(x) * log10(2), the constant applied with 128 fraction bits, see log(). Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 1 ulp, with log()'s limit on small arguments at I < 7.
constexpr fixed_point128 logb (fixed_point128 x)
 Calculates Log base 2 of x as an integer ignoring the sign of x. floor(log2(fabs(x))), as the C function: logb(-8) is 3. A narrow instantiation cannot hold the exponent of its smallest values - logb(epsilon()) is -126 for fixed_point128<1> - and the result then wraps like any other overflow.

Query and Utility Functions

constexpr bool is_int () const noexcept
 Returns true if the value is an int (fraction is zero)
constexpr bool is_positive () const noexcept
 Returns true if the value is positive (including zero)
constexpr bool is_negative () const noexcept
 Returns true if the value negative (smaller than zero)
constexpr bool is_zero () const noexcept
 Returns true if the value is zero.
constexpr int32_t get_bit (uint32_t bit) const noexcept
 get a specific bit within the 128 fixed point data
constexpr int32_t get_exponent () const noexcept
 Returns the exponent of the object - like the base 2 exponent of a floating point A value of 2.1 would return 1, values in the range [0.5,1.0) would return -1. Reads the magnitude, so the sign of the value makes no difference.
static const fixed_point128 & pi () noexcept
 Returns an instance of fixed_point128 with the value of pi Needs I to be 2 or more.
static const fixed_point128 & pi2 () noexcept
 Returns an instance of fixed_point128 with the value of pi * 2 Needs I to be 3 or more.
static const fixed_point128 & half_pi () noexcept
 Returns an instance of fixed_point128 with the value of pi / 2.
static const fixed_point128 & golden_ratio () noexcept
 Returns an instance of fixed_point128 with the value of the golden ratio.
static const fixed_point128 & e () noexcept
 Returns an instance of fixed_point128 with the value of e Needs I to be 2 or more.
static const fixed_point128 & sqrt_2 () noexcept
 Returns an instance of fixed_point128 with the value of sqrt(2)
static constexpr fixed_point128 one () noexcept
 Return an instance of fixed_point128 with the value of 1 These three exact constants are returned by value rather than by reference like the transcendental ones above: they are representable in every instantiation without a string conversion, so a constexpr function can build them on the spot and callers get a compile time constant instead of a lazily initialized static.
static constexpr fixed_point128 half () noexcept
 Return an instance of fixed_point128 with the value of 0.5 Shifting one() right is exact for any I. Note that 0.5 lands in the low QWORD when I is 63, which rules out spelling it as a constant of the upper QWORD alone.
static constexpr fixed_point128 epsilon () noexcept
 Return an instance of fixed_point128 with the smallest positive value possible.

Detailed Description

template<int32_t I>
class fp128::fixed_point128< I >

Google test class (friend).

128 bit fixed point class template.

This template provides a floating point-like type that performs various math operations quickly compared to traditional high precision libraries. The only template parameter I is the bit count of the integer part, not counting the sign. The sign takes one bit and the fraction takes the remaining 127 - I.
I is limited to the range [1,63] in order to simplify the implementation and increase performance.
This restriction is enforced at compile time. All of fixed_point128's methods are inline for maximum performance.

Representation:
The object is exactly 128 bits wide and holds nothing but the value. Those bits are a two's complement integer, held as the QWORD pair (high:low) with the sign in the MSB of high, and the value they stand for is that integer divided by 2^F:

value = (int128)(high:low) / 2^F, where F = 127 - I
static constexpr int32_t F
Fraction bit count (127 - I). The remaining bit is the sign.

The representable range is therefore [-2^I, 2^I - 2^-F], which is the asymmetric range of every two's complement type: the most negative value has no positive counterpart, so negating it (and fabs of it) wraps back to itself.

Minimum integer bits:
Some of the constants and functions cannot work in the narrowest instantiations, and each one says so with a static_assert in its body rather than by overflowing quietly or failing to link. The bound is the smallest I that works, not a safe margin:

Everything else works for every I, including half_pi(), sqrt_2() and golden_ratio(), which are all below two. A function whose result leaves the range - exp() of a large argument, log() of a tiny one - is a matter of the argument rather than of the type, and overflows silently like any other operation.

Implementation notes:

  • Overflow is handled silently, similar to builtin integer operations.
  • The sign is part of the value rather than a separate field, so addition, subtraction, the comparisons and the bitwise operators are the plain 128 bit integer operations and there is no negative zero to normalize away.
  • Multiplication and division work on magnitudes internally and restore the sign afterwards.
  • Rounding: every result that does not fit the grid is rounded to the nearest grid value, and an exact tie goes away from zero (the magnitude is rounded half up). This holds for the multiplication, the division by a fixed_point128 or by an integer, the right shifts, and the conversion to an instantiation with more integer bits. The conversion from a floating point value is the exception: it truncates towards zero, as the conversion to an integer does.
  • A fixed_point128 object is not thread safe. Accessing a const object from multiple threads is safe.
  • fixed_point128 is conditionally safe, 2 different non const objects can be accessed concurrently.
  • Only 64 bit builds are supported.

Compile time evaluation:
Everything that stays within integer arithmetic is constexpr:

  • Construction from and conversion to the integer types and the floating point types, copy, move and the cross-template conversions between two fixed_point128 instantiations.
  • Addition, subtraction, multiplication, square(), the increment and decrement operators, the shifts, the bitwise operators, the unary operators and the comparisons.
  • The queries: is_int(), is_positive(), is_negative(), is_zero(), get_bit() and get_exponent(), and the exact constants one(), half() and epsilon().
  • The math functions fabs, floor, ceil, trunc, round, copysign, modf, fmin, fmax, fdim, sqr, ilogb, lzcnt128, log2 and logb.

The bit counting and extended arithmetic intrinsics these rest on are not constant expressions, so fp128_shared.h wraps each one in a constexpr function that serves a constant evaluated call from a portable implementation of the same operation. A runtime call still reaches the bare intrinsic and generates the same code it did before.

The rest cannot be constexpr, for one of two reasons:

  • Division and modulo: div_64bit rests on the _udiv128 intrinsic. div_128bit does not, and nothing in it is barred from a constant expression, but the function that calls it is still bound by the rest of this list.
  • Function local statics, which a constexpr function may not declare. The transcendental constants (pi, e, sqrt_2, ...) and the factorial reciprocal table are parsed from strings into them. reciprocal is a division. sqrt and hypot seed their integer square root with the CRT's sqrt().

Definition at line 302 of file fixed_point128.h.

Member Typedef Documentation

◆ type

template<int32_t I>
typedef fixed_point128<I> fp128::fixed_point128< I >::type

Self type alias.

Definition at line 582 of file fixed_point128.h.

◆ ptr_type

template<int32_t I>
typedef fixed_point128<I>* fp128::fixed_point128< I >::ptr_type

Pointer type alias.

Definition at line 583 of file fixed_point128.h.

◆ ref_type

template<int32_t I>
typedef fixed_point128<I>& fp128::fixed_point128< I >::ref_type

Reference type alias.

Definition at line 584 of file fixed_point128.h.

Constructor & Destructor Documentation

◆ fixed_point128() [1/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( )
inlineconstexprnoexcept

Default constructor, creates an instance with a value of zero.

Definition at line 592 of file fixed_point128.h.

◆ fixed_point128() [2/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( const fixed_point128< I > & rhs)
inlineconstexprnoexcept

Copy constructor.

Parameters
rhsObject to copy from

Definition at line 597 of file fixed_point128.h.

◆ fixed_point128() [3/12]

template<int32_t I>
template<int32_t I2>
fp128::fixed_point128< I >::fixed_point128 ( const fixed_point128< I2 > & rhs)
inlineconstexprnoexcept

cross-template Copy constructor, can be used between two different fixed_point128 templates A value too large for the destination wraps silently, as everywhere else in this class. Fraction bits the destination cannot hold are rounded to nearest, a tie going away from zero.

Parameters
rhsfixed_point128 instance with from a different template instance.

Definition at line 604 of file fixed_point128.h.

◆ fixed_point128() [4/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( fixed_point128< I > && rhs)
inlineconstexprnoexcept

Move constructor Doesn't modify the right hand side object. Acts like a copy constructor.

Parameters
rhsObject to copy from

Definition at line 633 of file fixed_point128.h.

◆ fixed_point128() [5/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( double x)
inlineconstexprnoexcept

Constructor from the double type Underflow goes to zero. Overflow, NaN and +-INF saturate to the extreme value carrying the sign of the input: max() for a positive one and lowest() for a negative one. Not constexpr: the exponent and mantissa are read through the Double union, and reading the inactive member of a union is not allowed during constant evaluation.

Parameters
xInput value

Definition at line 642 of file fixed_point128.h.

◆ fixed_point128() [6/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( uint64_t x)
inlineconstexprnoexcept

Constructor from uint64_t type A value of 2^I or above overflows into the sign and wraps silently.

Parameters
xInput value

Definition at line 709 of file fixed_point128.h.

◆ fixed_point128() [7/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( int64_t x)
inlineconstexprnoexcept

Constructor from int64_t type The shift is done in the unsigned domain, where it wraps rather than overflowing, and a negative value carries its sign bits along with it: scaling a two's complement value by a power of two is the same left shift its magnitude needs.

Parameters
xInput value

Definition at line 721 of file fixed_point128.h.

◆ fixed_point128() [8/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( uint32_t x)
inlineconstexprnoexcept

Constructor from uint32_t type.

Parameters
xInput value

Definition at line 730 of file fixed_point128.h.

◆ fixed_point128() [9/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( int32_t x)
inlineconstexprnoexcept

Constructor from int32_t type.

Parameters
xInput value

Definition at line 739 of file fixed_point128.h.

◆ fixed_point128() [10/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( const char * x)
inlinenoexcept

Constructor from const char* (C string).

Reads optional leading white space, an optional sign, decimal digits and an optional point followed by more digits, and stops at the first character that is not part of that. The value is the grid value nearest the decimal one, a tie going away from zero - correctly rounded for any number of digits and any I, which is what lets every value survive the trip through its own text form.

The digits after the point are read as one exact integer and scaled by 2^F / 10^digits in big integer arithmetic, see ParseDecimal(). They used to be summed as a series of powers of ten held to 126 bits each, which was 22.8 ulp off at I = 1 and 3 ulp off at I = 4.

Much slower than the other constructors. A value too large for the type wraps silently, modulo 2^(I+1) in its integer part, which unlike the saturation the double constructor performs can land on either sign; the stream extractor reports it instead.

Parameters
xInput string

Definition at line 763 of file fixed_point128.h.

◆ fixed_point128() [11/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( const std::string & x)
inlinenoexcept

Constructor from std::string. Accurate to 37 digits after the decimal point. Allows creating very high precision values. Much slower than the rhs constructors.

Parameters
xInput string

Definition at line 783 of file fixed_point128.h.

◆ fixed_point128() [12/12]

template<int32_t I>
fp128::fixed_point128< I >::fixed_point128 ( uint64_t l,
uint64_t h )
inlineconstexprnoexcept

Constructor from the two QWORDs of the raw value, useful for creating very small constants. The pair is read as a 128 bit two's complement integer scaled by 2^-F, so a negative value is spelled with the MSB of h set rather than with a separate sign.

Parameters
lLow QWORD
hHigh QWORD, sign included

Definition at line 795 of file fixed_point128.h.

◆ ~fixed_point128()

template<int32_t I>
fp128::fixed_point128< I >::~fixed_point128 ( )
constexprdefaultnoexcept

Destructor.

Member Function Documentation

◆ get_components()

template<int32_t I>
void fp128::fixed_point128< I >::get_components ( uint64_t & l,
uint64_t & h ) const
inlineconstexprnoexcept

Reads the raw representation: the two QWORDs of the 128 bit two's complement value.

Parameters
lReceives the low QWORD
hReceives the high QWORD, sign included

Definition at line 575 of file fixed_point128.h.

◆ operator=() [1/3]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator= ( const fixed_point128< I > & rhs)
inlineconstexprnoexcept

Assignment operator.

Parameters
rhsObject to copy from
Returns
This object.

Definition at line 806 of file fixed_point128.h.

◆ operator=() [2/3]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator= ( fixed_point128< I > && rhs)
inlineconstexprnoexcept

Move assignment operator.

Parameters
rhsObject to copy from
Returns
This object.

Definition at line 817 of file fixed_point128.h.

◆ operator=() [3/3]

template<int32_t I>
template<int32_t I2>
fixed_point128< I > & fp128::fixed_point128< I >::operator= ( const fixed_point128< I2 > & rhs)
inlineconstexprnoexcept

cross-template assignment operator, can be used between two different fixed_point128 templates Behaves as the cross-template constructor: a value too large for this instantiation wraps, and fraction bits it cannot hold are rounded to nearest, a tie going away from zero.

Parameters
rhsfixed_point128 instance with from a different template instance.
Returns
This object.

Definition at line 830 of file fixed_point128.h.

◆ operator int64_t()

template<int32_t I>
fp128::fixed_point128< I >::operator int64_t ( ) const
inlinenodiscardconstexprnoexcept

operator int64_t - converts to a int64_t The fraction is truncated towards zero, as the builtin floating point to integer conversions do, rather than shifted away - which would round a negative value towards minus infinity.

Returns
Object value.

Definition at line 867 of file fixed_point128.h.

◆ operator uint64_t()

template<int32_t I>
fp128::fixed_point128< I >::operator uint64_t ( ) const
inlinenodiscardconstexprnoexcept

operator uint64_t - converts to a uint64_t A negative value wraps modulo 2^64, the way a builtin signed to unsigned conversion does.

Returns
Object value.

Definition at line 881 of file fixed_point128.h.

◆ operator int32_t()

template<int32_t I>
fp128::fixed_point128< I >::operator int32_t ( ) const
inlinenodiscardconstexprnoexcept

operator int32_t - converts to a int32_t

Returns
Object value, truncated towards zero and wrapped to 32 bits.

Definition at line 886 of file fixed_point128.h.

◆ operator uint32_t()

template<int32_t I>
fp128::fixed_point128< I >::operator uint32_t ( ) const
inlinenodiscardconstexprnoexcept

operator uint32_t - converts to a uint32_t

Returns
Object value, truncated towards zero and wrapped to 32 bits.

Definition at line 891 of file fixed_point128.h.

◆ operator float()

template<int32_t I>
fp128::fixed_point128< I >::operator float ( ) const
inlinenodiscardconstexprnoexcept

operator float - converts to a float Not constexpr: the result is assembled through the Float union, and writing one member of a union and reading another is not allowed during constant evaluation.

Returns
Object value.

Definition at line 898 of file fixed_point128.h.

◆ operator double()

template<int32_t I>
fp128::fixed_point128< I >::operator double ( ) const
inlinenodiscardconstexprnoexcept

operator double - converts to a double The whole value range is within reach of a double's exponent, so nothing overflows or goes denormal. Not constexpr: the result is assembled through the Double union, and writing one member of a union and reading another is not allowed during constant evaluation.

Returns
Object value, rounded to nearest with ties going to even.

Definition at line 933 of file fixed_point128.h.

◆ operator long double()

template<int32_t I>
fp128::fixed_point128< I >::operator long double ( ) const
inlinenodiscardconstexprnoexcept

operator long double - converts to a long double

Returns
Object value.

Definition at line 961 of file fixed_point128.h.

◆ operator std::string()

template<int32_t I>
fp128::fixed_point128< I >::operator std::string ( ) const
inlinenodiscardnoexcept

Converts to a std::string (slow) string holds all meaningful fraction bits.

Returns
object string representation

Definition at line 966 of file fixed_point128.h.

◆ operator char *()

template<int32_t I>
fp128::fixed_point128< I >::operator char * ( ) const
inlineexplicitnodiscardnoexcept

Converts to a C string (slow) string holds all meaningful fraction bits.

Returns
object string representation

Definition at line 971 of file fixed_point128.h.

◆ operator>>()

template<int32_t I>
template<typename T>
fixed_point128 fp128::fixed_point128< I >::operator>> ( T shift) const
inlinenodiscardconstexprnoexcept

Performs right shift operation.

Hinted rather than forced, unlike the other one line forwarders (see FP128_FORCE_INLINE). operator>>= is itself forced, so forcing this one too would expand the whole shift, its rounding included, at every use of the by value form. The equivalent pair in float128 measurably hurt a loop that shifts alongside other arithmetic; this one is left hinted for the same reason. The callee being forced means the shift is inlined either way once the compiler decides to inline this wrapper.

Parameters
shiftbits to shift
Returns
Temporary object with the result of the operation

Definition at line 1070 of file fixed_point128.h.

◆ operator<<()

template<int32_t I>
template<typename T>
fixed_point128 fp128::fixed_point128< I >::operator<< ( T shift) const
inlinenodiscardconstexprnoexcept

Performs left shift operation. Hinted rather than forced, see operator>> above.

Parameters
shiftbits to shift
Returns
Temporary object with the result of the operation

Definition at line 1081 of file fixed_point128.h.

◆ operator+=() [1/2]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator+= ( const fixed_point128< I > & rhs)
inlineconstexprnoexcept

Add a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1091 of file fixed_point128.h.

◆ operator+=() [2/2]

template<int32_t I>
template<typename T>
fixed_point128 & fp128::fixed_point128< I >::operator+= ( const T & rhs)
inlineconstexprnoexcept

Add a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1104 of file fixed_point128.h.

◆ operator-=() [1/2]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator-= ( const fixed_point128< I > & rhs)
inlineconstexprnoexcept

Subtract a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1110 of file fixed_point128.h.

◆ operator-=() [2/2]

template<int32_t I>
template<typename T>
fixed_point128 & fp128::fixed_point128< I >::operator-= ( const T & rhs)
inlineconstexprnoexcept

Subtract a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1123 of file fixed_point128.h.

◆ operator*=() [1/3]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator*= ( const fixed_point128< I > & rhs)
inlineconstexprnoexcept

Multiplies a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1129 of file fixed_point128.h.

◆ square()

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::square ( )
inlineconstexprnoexcept

Squares this object in place.

Cheaper than operator*=(*this): a square is symmetric, so the two cross products low*high and high*low are the same value and only one 64x64->128 bit multiply is needed instead of two. The sign is also known in advance - a magnitude times itself is the answer as it stands - which removes the sign combination and the negation.

The 256 bit intermediate product is accumulated exactly as in operator*=, so the result is bit identical to (*this) * (*this), including rounding.

Returns
This object.

Definition at line 1213 of file fixed_point128.h.

◆ operator*=() [2/3]

template<int32_t I>
template<typename T>
fixed_point128 & fp128::fixed_point128< I >::operator*= ( T x)
inlineconstexprnoexcept

Multiplies a value to this object.

Parameters
xRight hand side operand
Returns
This object.

Definition at line 1267 of file fixed_point128.h.

◆ operator*=() [3/3]

template<int32_t I>
template<>
fixed_point128 & fp128::fixed_point128< I >::operator*= ( uint64_t x)
inlineconstexprnoexcept

Multiplies a 64 bit value to this object The product is taken modulo 2^128, which is the signed product of a two's complement value by an unsigned one: multiplying is repeated addition and addition already wraps correctly.

Parameters
xRight hand side operand
Returns
This object.

Definition at line 1300 of file fixed_point128.h.

◆ operator/=() [1/4]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator/= ( const fixed_point128< I > & rhs)
inline

Divide this object by x. The quotient is rounded to nearest, a tie going away from zero, whichever of the three paths below computes it.

Parameters
rhsRight hand side operator (denominator)
Returns
this object.
Exceptions
std::logic_errorwhen rhs is zero - zero divided by zero included - leaving this object in an unspecified state: the magnitude of what it held, the sign not yet put back.

Definition at line 1318 of file fixed_point128.h.

◆ operator/=() [2/4]

template<int32_t I>
template<typename T>
fixed_point128 & fp128::fixed_point128< I >::operator/= ( T x)
inline

Divide this object by x.

Parameters
xDenominator.
Returns
This object.

Definition at line 1388 of file fixed_point128.h.

◆ operator/=() [3/4]

template<int32_t I>
template<>
fixed_point128 & fp128::fixed_point128< I >::operator/= ( double x)
inline

Divide this object by x.

Parameters
xDenominator.
Returns
This object.

Definition at line 1418 of file fixed_point128.h.

◆ operator/=() [4/4]

template<int32_t I>
template<>
fixed_point128 & fp128::fixed_point128< I >::operator/= ( uint64_t x)
inline

Divide this object by x. The quotient is rounded to nearest, a tie going away from zero: the same result as dividing by fixed_point128(x), which the integer divisor only makes cheaper.

Parameters
xDenominator.
Returns
This object.
Exceptions
std::logic_errorwhen x is zero.

Definition at line 1444 of file fixed_point128.h.

◆ operator%=() [1/2]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator%= ( const fixed_point128< I > & rhs)
inline

%= operator, the remainder of a truncating division: x - y * trunc(x / y).

The result is exact and keeps the sign of the numerator, as fmod() does. Two values on the same grid of 2^-F are integers scaled by the same power of two, so their remainder is the remainder of the raw 128 bit integers, scaled the same way: no quotient is formed, so it can neither overflow nor be rounded onto the wrong integer.

Parameters
rhsModulo operand.
Returns
This object.
Exceptions
std::logic_errorwhen rhs is zero, zero modulo zero included.

Definition at line 1480 of file fixed_point128.h.

◆ operator%=() [2/2]

template<int32_t I>
template<typename T>
fixed_point128 & fp128::fixed_point128< I >::operator%= ( T rhs)
inline

Performs modulo with a generic type by converting to fixed_point128.

Template Parameters
TRight hand side type
Parameters
rhsDivisor
Returns
This object.

Definition at line 1516 of file fixed_point128.h.

◆ operator>>=()

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator>>= ( int32_t shift)
inlineconstexprnoexcept

Shift right this object. The shift is arithmetic: the sign bit is replicated, so the result is the value divided by 2^shift and rounded to nearest, a tie going away from zero - the rounding rule of the multiply and the divisions.

That rule makes +-epsilon() a fixed point of a shift by one: epsilon() >> 1 is exactly half an epsilon, a tie, and rounds back up to epsilon(). A loop that halves a value until it reaches zero has to bound its iterations or test for epsilon() instead.

Parameters
shiftBits to shift. A negative count causes undefined behavior; 128 or more leaves zero, apart from lowest() >> 128, which is the tie -epsilon() / 2 and so -epsilon().
Returns
This object.

Definition at line 1530 of file fixed_point128.h.

◆ operator<<=()

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator<<= ( int32_t shift)
inlineconstexprnoexcept

Shift left this object. A value shifted past the range of the type wraps silently, sign included.

Parameters
shiftBits to shift. Negative or very high values cause undefined behavior.
Returns
This object.

Definition at line 1541 of file fixed_point128.h.

◆ operator&=() [1/2]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator&= ( const fixed_point128< I > & rhs)
inlineconstexprnoexcept

Bitwise AND= The operators below work on all 128 bits, the sign bit included.

Parameters
rhsAND mask.
Returns
This object.

Definition at line 1552 of file fixed_point128.h.

◆ operator&=() [2/2]

template<int32_t I>
template<typename T>
fixed_point128 & fp128::fixed_point128< I >::operator&= ( const T & rhs)
inlineconstexpr

Bitwise AND=.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1563 of file fixed_point128.h.

◆ operator|=() [1/2]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator|= ( const fixed_point128< I > & rhs)
inlineconstexprnoexcept

Bitwise OR=.

Parameters
rhsOR mask.
Returns
This object.

Definition at line 1569 of file fixed_point128.h.

◆ operator|=() [2/2]

template<int32_t I>
template<typename T>
fixed_point128 & fp128::fixed_point128< I >::operator|= ( const T & rhs)
inlineconstexpr

Bitwise OR=.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1580 of file fixed_point128.h.

◆ operator^=() [1/2]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator^= ( const fixed_point128< I > & rhs)
inlineconstexprnoexcept

Bitwise XOR=.

Parameters
rhsXOR mask.
Returns
This object.

Definition at line 1586 of file fixed_point128.h.

◆ operator^=() [2/2]

template<int32_t I>
template<typename T>
fixed_point128 & fp128::fixed_point128< I >::operator^= ( const T & rhs)
inlineconstexpr

Bitwise XOR=.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1597 of file fixed_point128.h.

◆ operator++() [1/2]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator++ ( )
inlineconstexprnoexcept

Prefix ++ operation (++a)

Returns
This object.

Definition at line 1602 of file fixed_point128.h.

◆ operator++() [2/2]

template<int32_t I>
fixed_point128 fp128::fixed_point128< I >::operator++ ( int32_t )
inlineconstexprnoexcept

Postfix ++ operation (a++)

Returns
This object.

Definition at line 1611 of file fixed_point128.h.

◆ operator--() [1/2]

template<int32_t I>
fixed_point128 & fp128::fixed_point128< I >::operator-- ( )
inlineconstexprnoexcept

Prefix – operation (–a)

Returns
This object.

Definition at line 1621 of file fixed_point128.h.

◆ operator--() [2/2]

template<int32_t I>
fixed_point128 fp128::fixed_point128< I >::operator-- ( int32_t )
inlineconstexprnoexcept

Postfix – operation (a–)

Returns
This object.

Definition at line 1630 of file fixed_point128.h.

◆ operator bool()

template<int32_t I>
fp128::fixed_point128< I >::operator bool ( ) const
inlinenodiscardconstexprnoexcept

Convert to bool.

Definition at line 1643 of file fixed_point128.h.

◆ operator!()

template<int32_t I>
bool fp128::fixed_point128< I >::operator! ( ) const
inlinenodiscardconstexprnoexcept

Logical not (!). Opposite of operator bool.

Definition at line 1647 of file fixed_point128.h.

◆ operator~()

template<int32_t I>
fixed_point128 fp128::fixed_point128< I >::operator~ ( ) const
inlinenodiscardconstexprnoexcept

Bitwise not (~). Flips all 128 bits, the sign included, which makes it -x - epsilon().

Definition at line 1651 of file fixed_point128.h.

◆ operator+()

template<int32_t I>
fixed_point128 fp128::fixed_point128< I >::operator+ ( ) const
inlinenodiscardconstexprnoexcept

Unary +. Returns a copy of the object.

Definition at line 1661 of file fixed_point128.h.

◆ operator-()

template<int32_t I>
fixed_point128 fp128::fixed_point128< I >::operator- ( ) const
inlinenodiscardconstexprnoexcept

Unary -. Returns the two's complement of the object. The most negative value has no positive counterpart and is returned unchanged, the same silent wrap the builtin integer types perform.

Definition at line 1671 of file fixed_point128.h.

◆ is_int()

template<int32_t I>
bool fp128::fixed_point128< I >::is_int ( ) const
inlinenodiscardconstexprnoexcept

Returns true if the value is an int (fraction is zero)

Returns
True when the fraction is zero.

Definition at line 1687 of file fixed_point128.h.

◆ is_positive()

template<int32_t I>
bool fp128::fixed_point128< I >::is_positive ( ) const
inlinenodiscardconstexprnoexcept

Returns true if the value is positive (including zero)

Returns
True when the value is positive

Definition at line 1700 of file fixed_point128.h.

◆ is_negative()

template<int32_t I>
bool fp128::fixed_point128< I >::is_negative ( ) const
inlinenodiscardconstexprnoexcept

Returns true if the value negative (smaller than zero)

Returns
True when the value is negative

Definition at line 1705 of file fixed_point128.h.

◆ is_zero()

template<int32_t I>
bool fp128::fixed_point128< I >::is_zero ( ) const
inlinenodiscardconstexprnoexcept

Returns true if the value is zero.

Returns
Returns true if the value is zero

Definition at line 1710 of file fixed_point128.h.

◆ get_bit()

template<int32_t I>
int32_t fp128::fixed_point128< I >::get_bit ( uint32_t bit) const
inlinenodiscardconstexprnoexcept

get a specific bit within the 128 fixed point data

Parameters
bitbit to get [0,127]
Returns
0 or 1. Undefined when bit > 127

Definition at line 1716 of file fixed_point128.h.

◆ get_exponent()

template<int32_t I>
int32_t fp128::fixed_point128< I >::get_exponent ( ) const
inlinenodiscardconstexprnoexcept

Returns the exponent of the object - like the base 2 exponent of a floating point A value of 2.1 would return 1, values in the range [0.5,1.0) would return -1. Reads the magnitude, so the sign of the value makes no difference.

Returns
Exponent of the number

Definition at line 1729 of file fixed_point128.h.

◆ pi()

template<int32_t I>
const fixed_point128 & fp128::fixed_point128< I >::pi ( )
inlinestaticnodiscardnoexcept

Returns an instance of fixed_point128 with the value of pi Needs I to be 2 or more.

Returns
pi

Definition at line 1739 of file fixed_point128.h.

◆ pi2()

template<int32_t I>
const fixed_point128 & fp128::fixed_point128< I >::pi2 ( )
inlinestaticnodiscardnoexcept

Returns an instance of fixed_point128 with the value of pi * 2 Needs I to be 3 or more.

Returns
pi * 2

Definition at line 1750 of file fixed_point128.h.

◆ half_pi()

template<int32_t I>
const fixed_point128 & fp128::fixed_point128< I >::half_pi ( )
inlinestaticnodiscardnoexcept

Returns an instance of fixed_point128 with the value of pi / 2.

Returns
pi / 2

Definition at line 1760 of file fixed_point128.h.

◆ golden_ratio()

template<int32_t I>
const fixed_point128 & fp128::fixed_point128< I >::golden_ratio ( )
inlinestaticnodiscardnoexcept

Returns an instance of fixed_point128 with the value of the golden ratio.

Returns
golden ratio constant

Definition at line 1769 of file fixed_point128.h.

◆ e()

template<int32_t I>
const fixed_point128 & fp128::fixed_point128< I >::e ( )
inlinestaticnodiscardnoexcept

Returns an instance of fixed_point128 with the value of e Needs I to be 2 or more.

Returns
e

Definition at line 1779 of file fixed_point128.h.

◆ sqrt_2()

template<int32_t I>
const fixed_point128 & fp128::fixed_point128< I >::sqrt_2 ( )
inlinestaticnodiscardnoexcept

Returns an instance of fixed_point128 with the value of sqrt(2)

Returns
e

Definition at line 1789 of file fixed_point128.h.

◆ one()

template<int32_t I>
constexpr fixed_point128 fp128::fixed_point128< I >::one ( )
inlinestaticnodiscardconstexprnoexcept

Return an instance of fixed_point128 with the value of 1 These three exact constants are returned by value rather than by reference like the transcendental ones above: they are representable in every instantiation without a string conversion, so a constexpr function can build them on the spot and callers get a compile time constant instead of a lazily initialized static.

Returns
1

Definition at line 1802 of file fixed_point128.h.

◆ half()

template<int32_t I>
constexpr fixed_point128 fp128::fixed_point128< I >::half ( )
inlinestaticnodiscardconstexprnoexcept

Return an instance of fixed_point128 with the value of 0.5 Shifting one() right is exact for any I. Note that 0.5 lands in the low QWORD when I is 63, which rules out spelling it as a constant of the upper QWORD alone.

Returns
0.5

Definition at line 1809 of file fixed_point128.h.

◆ epsilon()

template<int32_t I>
constexpr fixed_point128 fp128::fixed_point128< I >::epsilon ( )
inlinestaticnodiscardconstexprnoexcept

Return an instance of fixed_point128 with the smallest positive value possible.

Returns
1

Definition at line 1814 of file fixed_point128.h.

◆ fixed_point128

template<int32_t I>
template<int32_t I2>
friend class fixed_point128
friend

Definition at line 309 of file fixed_point128.h.

◆ fp128_gtest

template<int32_t I>
friend class fp128_gtest
friend

Definition at line 310 of file fixed_point128.h.

◆ operator>> [1/2]

template<int32_t I>
template<int32_t J>
std::istream & operator>> ( std::istream & is,
fixed_point128< J > & value )
friend

◆ operator+ [1/2]

template<int32_t I>
template<typename T>
fixed_point128 operator+ ( fixed_point128< I > lhs,
const T & rhs )
friend

Adds 2 values and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 2383 of file fixed_point128.h.

◆ operator- [1/2]

template<int32_t I>
template<typename T>
fixed_point128 operator- ( fixed_point128< I > lhs,
const T & rhs )
friend

subtracts the right hand side operand to this object to and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The fixed_point128 result

Definition at line 2395 of file fixed_point128.h.

◆ operator* [1/2]

template<int32_t I>
template<typename T>
fixed_point128 operator* ( fixed_point128< I > lhs,
const T & rhs )
friend

Multiplies the right hand side operand with this object to and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The fixed_point128 result

Definition at line 2407 of file fixed_point128.h.

◆ operator/ [1/2]

template<int32_t I>
template<typename T>
fixed_point128 operator/ ( fixed_point128< I > lhs,
const T & rhs )
friend

Divides this object by the right hand side operand and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The fixed_point128 result

Definition at line 2418 of file fixed_point128.h.

◆ operator% [1/2]

template<int32_t I>
template<typename T>
fixed_point128 operator% ( fixed_point128< I > lhs,
const T & rhs )
friend

Calculates modulo.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The fixed_point128 result

Definition at line 2429 of file fixed_point128.h.

◆ operator& [1/2]

template<int32_t I>
template<typename T>
fixed_point128 operator& ( fixed_point128< I > lhs,
const T & rhs )
friend

Performs bitwise AND (&)

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The fixed_point128 result

Definition at line 2446 of file fixed_point128.h.

◆ operator| [1/2]

template<int32_t I>
template<typename T>
fixed_point128 operator| ( fixed_point128< I > lhs,
const T & rhs )
friend

Performs bitwise OR (|)

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The fixed_point128 result

Definition at line 2457 of file fixed_point128.h.

◆ operator^ [1/2]

template<int32_t I>
template<typename T>
fixed_point128 operator^ ( fixed_point128< I > lhs,
const T & rhs )
friend

Performs bitwise XOR (^)

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The fixed_point128 result

Definition at line 2468 of file fixed_point128.h.

◆ operator+ [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator+ ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Adds a scalar and a fixed_point128, in that order.

Parameters
lhsLeft operand
rhsRight operand
Returns
The fixed_point128 result

Definition at line 2492 of file fixed_point128.h.

◆ operator- [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator- ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Subtracts a fixed_point128 from a scalar.

Parameters
lhsLeft operand
rhsRight operand
Returns
The fixed_point128 result

Definition at line 2499 of file fixed_point128.h.

◆ operator* [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator* ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Multiplies a scalar and a fixed_point128, in that order.

Parameters
lhsLeft operand
rhsRight operand
Returns
The fixed_point128 result

Definition at line 2506 of file fixed_point128.h.

◆ operator/ [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator/ ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Divides a scalar by a fixed_point128.

Parameters
lhsLeft operand
rhsRight operand
Returns
The fixed_point128 result

Definition at line 2513 of file fixed_point128.h.

◆ operator% [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator% ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Performs modulo of a scalar by a fixed_point128.

Parameters
lhsLeft operand
rhsRight operand
Returns
The fixed_point128 result

Definition at line 2520 of file fixed_point128.h.

◆ operator& [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator& ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Performs bitwise AND of a scalar and a fixed_point128.

Parameters
lhsLeft operand
rhsRight operand
Returns
The fixed_point128 result

Definition at line 2527 of file fixed_point128.h.

◆ operator| [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator| ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Performs bitwise OR of a scalar and a fixed_point128.

Parameters
lhsLeft operand
rhsRight operand
Returns
The fixed_point128 result

Definition at line 2534 of file fixed_point128.h.

◆ operator^ [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator^ ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Performs bitwise XOR of a scalar and a fixed_point128.

Parameters
lhsLeft operand
rhsRight operand
Returns
The fixed_point128 result

Definition at line 2541 of file fixed_point128.h.

◆ operator<<

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator<< ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Shifts a scalar left by a fixed_point128 shift count.

The left operand is widened to fixed_point128 first, so the result is 128 bit rather than the builtin type of lhs.

The count is rhs converted to int32_t, the same conversion the fixed_point128 on the left overload applies. That conversion is a shift, so the fraction of rhs is truncated toward zero rather than rounded.

Parameters
lhsLeft operand, the value being shifted
rhsRight operand, the shift count
Returns
The fixed_point128 result

Definition at line 2561 of file fixed_point128.h.

◆ operator>> [2/2]

template<int32_t I>
template<typename T>
requires std::is_arithmetic_v<T>
fixed_point128 operator>> ( const T & lhs,
const fixed_point128< I > & rhs )
friend

Shifts a scalar right by a fixed_point128 shift count. The left operand is widened to fixed_point128 first, see operator<< above.

Parameters
lhsLeft operand, the value being shifted
rhsRight operand, the shift count
Returns
The fixed_point128 result

Definition at line 2574 of file fixed_point128.h.

◆ operator== [1/3]

template<int32_t I>
bool operator== ( const fixed_point128< I > & lhs,
const fixed_point128< I > & rhs )
friend

Compare logical/bitwise equal.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True if this and rhs are equal.

Definition at line 2590 of file fixed_point128.h.

◆ operator== [2/3]

template<int32_t I>
template<typename T>
bool operator== ( const fixed_point128< I > & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2595 of file fixed_point128.h.

◆ operator== [3/3]

template<int32_t I>
template<typename T>
bool operator== ( const T & lhs,
const fixed_point128< I > & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2600 of file fixed_point128.h.

◆ operator!= [1/3]

template<int32_t I>
bool operator!= ( const fixed_point128< I > & lhs,
const fixed_point128< I > & rhs )
friend

Return true when objects are not equal. Can be used as logical XOR.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True if not equal.

Definition at line 2610 of file fixed_point128.h.

◆ operator!= [2/3]

template<int32_t I>
template<typename T>
bool operator!= ( const fixed_point128< I > & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2615 of file fixed_point128.h.

◆ operator!= [3/3]

template<int32_t I>
template<typename T>
bool operator!= ( const T & lhs,
const fixed_point128< I > & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2620 of file fixed_point128.h.

◆ operator< [1/3]

template<int32_t I>
bool operator< ( const fixed_point128< I > & lhs,
const fixed_point128< I > & rhs )
friend

Return true if this object is small than the rhs.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True when this object is smaller.

Definition at line 2630 of file fixed_point128.h.

◆ operator< [2/3]

template<int32_t I>
template<typename T>
bool operator< ( const fixed_point128< I > & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2640 of file fixed_point128.h.

◆ operator< [3/3]

template<int32_t I>
template<typename T>
bool operator< ( const T & lhs,
const fixed_point128< I > & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2645 of file fixed_point128.h.

◆ operator<= [1/3]

template<int32_t I>
bool operator<= ( const fixed_point128< I > & lhs,
const fixed_point128< I > & rhs )
friend

Return true this object is small or equal than the rhs.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True when this object is smaller or equal.

Definition at line 2655 of file fixed_point128.h.

◆ operator<= [2/3]

template<int32_t I>
template<typename T>
bool operator<= ( const fixed_point128< I > & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2657 of file fixed_point128.h.

◆ operator<= [3/3]

template<int32_t I>
template<typename T>
bool operator<= ( const T & lhs,
const fixed_point128< I > & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2662 of file fixed_point128.h.

◆ operator> [1/3]

template<int32_t I>
bool operator> ( const fixed_point128< I > & lhs,
const fixed_point128< I > & rhs )
friend

Return true this object is larger than the rhs.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True when this object is larger.

Definition at line 2672 of file fixed_point128.h.

◆ operator> [2/3]

template<int32_t I>
template<typename T>
bool operator> ( const fixed_point128< I > & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2681 of file fixed_point128.h.

◆ operator> [3/3]

template<int32_t I>
template<typename T>
bool operator> ( const T & lhs,
const fixed_point128< I > & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2686 of file fixed_point128.h.

◆ operator>= [1/3]

template<int32_t I>
bool operator>= ( const fixed_point128< I > & lhs,
const fixed_point128< I > & rhs )
friend

Return true this object is larger or equal than the rhs.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True when this objext is larger or equal.

Definition at line 2696 of file fixed_point128.h.

◆ operator>= [2/3]

template<int32_t I>
template<typename T>
bool operator>= ( const fixed_point128< I > & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2698 of file fixed_point128.h.

◆ operator>= [3/3]

template<int32_t I>
template<typename T>
bool operator>= ( const T & lhs,
const fixed_point128< I > & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2703 of file fixed_point128.h.

◆ fabs

template<int32_t I>
fixed_point128 fabs ( const fixed_point128< I > & x)
friend

Returns the absolute value.

Parameters
xInput value
Returns
A copy of x with the sign removed. lowest() has no positive counterpart and comes back unchanged, the same silent wrap the builtin integer types perform.

Definition at line 2720 of file fixed_point128.h.

◆ floor

template<int32_t I>
fixed_point128 floor ( const fixed_point128< I > & x)
friend

Performs the floor() function, similar to libc's floor(), rounds down towards -infinity.

Parameters
xInput value
Returns
A fixed_point128 holding the integer value. Overflow is not reported.

Definition at line 2733 of file fixed_point128.h.

◆ ceil

template<int32_t I>
fixed_point128 ceil ( const fixed_point128< I > & x)
friend

Performs the ceil() function, similar to libc's ceil(), rounds up towards infinity.

Parameters
xInput value
Returns
A fixed_point128 holding the integer value. Overflow is not reported.

Definition at line 2745 of file fixed_point128.h.

◆ trunc

template<int32_t I>
fixed_point128 trunc ( const fixed_point128< I > & x)
friend

Rounds towards zero.

Parameters
xValue to truncate
Returns
Integer value, rounded towards zero.

Definition at line 2758 of file fixed_point128.h.

◆ round

template<int32_t I>
fixed_point128 round ( const fixed_point128< I > & x)
friend

Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.

Parameters
xValue to round
Returns
Integer value, rounded towards the nearest integer.

Definition at line 2769 of file fixed_point128.h.

◆ ilogb

template<int32_t I>
int32_t ilogb ( const fixed_point128< I > & x)
friend

Retrieves an integer that represents the base-2 exponent of the specified value. floor(log2(|x|)), as the C function: the sign of x makes no difference.

Parameters
xThe specified value.
Returns
The exponent. FP_ILOGB0 for zero, which is what the C function returns for it.

Definition at line 2782 of file fixed_point128.h.

◆ copysign

template<int32_t I>
fixed_point128 copysign ( const fixed_point128< I > & x,
const fixed_point128< I > & y )
friend

returns the value of x with the sign of y.

Parameters
xThe value that's returned as the magnitude of the result.
yThe sign of the result.
Returns
The copysign functions return a floating-point value that combines the magnitude of x and the sign of y.

Definition at line 2792 of file fixed_point128.h.

◆ fmod

template<int32_t I>
fixed_point128 fmod ( const fixed_point128< I > & x,
const fixed_point128< I > & y )
friend

Performs the fmod() function, similar to libc's fmod(): x - y * trunc(x / y). Exact, and the result keeps the sign of x. See operator%=().

Parameters
xNumerator
yDenominator
Returns
A fixed_point128 holding the modulo value.
Exceptions
std::logic_errorwhen y is zero. This type has no NaN to return instead.

Definition at line 2805 of file fixed_point128.h.

◆ modf

template<int32_t I>
fixed_point128 modf ( const fixed_point128< I > & x,
fixed_point128< I > * iptr )
friend

Split into integer and fraction parts. Both results carry the sign of the input variable.

Parameters
xInput value
iptrPointer to fixed_point128 holding the integer part of x.
Returns
The fraction part of x. Undefined when iptr is nullptr.

Definition at line 2813 of file fixed_point128.h.

◆ fdim

template<int32_t I>
fixed_point128 fdim ( const fixed_point128< I > & x,
const fixed_point128< I > & y )
friend

Determines the positive difference between the first and second values.

Parameters
xFirst value
ySecond value
Returns
If x > y returns x - y. Otherwise zero.

Definition at line 2829 of file fixed_point128.h.

◆ fmin

template<int32_t I>
fixed_point128 fmin ( const fixed_point128< I > & x,
const fixed_point128< I > & y )
friend

Returns the minimum between x and y.

Parameters
xFirst value
ySecond value
Returns
If x < y returns x. Otherwise y.

Definition at line 2839 of file fixed_point128.h.

◆ fmax

template<int32_t I>
fixed_point128 fmax ( const fixed_point128< I > & x,
const fixed_point128< I > & y )
friend

Returns the maximum between x and y.

Parameters
xFirst value
ySecond value
Returns
If x > y returns x. Otherwise y.

Definition at line 2846 of file fixed_point128.h.

◆ hypot

template<int32_t I>
fixed_point128 hypot ( const fixed_point128< I > & x,
const fixed_point128< I > & y )
friend

Calculates the hypotenuse. i.e. sqrt(x^2 + y^2)

Correctly rounded, and free of intermediate overflow: hypot(x, y) * 2^F is the square root of the sum of the squared raw values, which is formed as an exact 256 bit integer. Spelled as sqrt(sqr(x) + sqr(y)) instead, it overflowed as soon as |x| or |y| reached 2^(I/2), long before the result itself leaves the range.

Parameters
xFirst value
ySecond value
Returns
sqrt(x^2 + y^2), rounded to nearest. A result of 2^I or more wraps, like any overflow.

Definition at line 2859 of file fixed_point128.h.

◆ sqr

template<int32_t I>
fixed_point128 sqr ( fixed_point128< I > x)
friend

Calculates the square of a value. i.e. x^2.

Faster than x * x and bit identical to it. Prefer this function wherever a value is multiplied by itself, see fixed_point128::square().

Parameters
xValue to square
Returns
x^2, which is never negative.

Definition at line 2887 of file fixed_point128.h.

◆ lzcnt128

template<int32_t I>
uint64_t lzcnt128 ( const fixed_point128< I > & x)
friend

Calculates the left zero count of value x, ignoring the sign. Counted on the magnitude, so a negative value gives the same answer as its opposite rather than the zero its two's complement bits would produce.

Parameters
xinput value.
Returns
lzc (uint32_t) of th result.

Definition at line 2895 of file fixed_point128.h.

◆ sqrt

template<int32_t I>
fixed_point128 sqrt ( const fixed_point128< I > & x,
uint32_t iterations = 3 )
friend

Calculates the square root, correctly rounded.

sqrt(x) * 2^F is the square root of the raw value scaled up by another F bits, which makes it the integer square root of a 256 bit number: RoundedSqrt256() computes it rounded to nearest, so the result is the representable value closest to the exact root at every I and every magnitude. (A tie between two representable values cannot occur.)

Parameters
xValue to calculate the root of
iterationsIgnored. It used to set the step count of an iterative refinement, which lost up to half of the integer bits for a large argument; it is kept so that existing calls still compile.
Returns
Square root of (x), zero when x <= 0.

Definition at line 2915 of file fixed_point128.h.

◆ fact_reciprocal

template<int32_t I>
void fact_reciprocal ( int x,
fixed_point128< I > & res )
friend

Factorial reciprocal (inverse). Calculates 1 / x! Maximum value of x that may produce non zero values is 34. This value depends on the amount of fraction bits. The table is parsed from decimal strings, so it carries the accuracy of the string constructor: within 0.5 + 23 / 2^(I-1) ulp of the exact 1 / x!, measured 17.5 ulp at I = 1 and 0.5 from I = 16 up.

Parameters
xInput value
resResult of the function

Definition at line 2945 of file fixed_point128.h.

◆ reciprocal

template<int32_t I>
fixed_point128 reciprocal ( const fixed_point128< I > & x)
friend

Calculates the reciprocal of a value. y = 1 / x.

Correctly rounded: the quotient comes from the same long division operator/=() uses, rounded to nearest with a tie going away from zero. The division is the faster of the two since div_128bit, and the Newton refinement of a double estimate this replaced lost accuracy as 1 / x grew - tens of thousands of ulp at fixed_point128<16>. It is called directly rather than through operator/=(), which divides by multiplying with this function when FP128_USE_RECIPROCAL_FOR_DIVISION is set.

Parameters
xInput value
Returns
1 / x. Zero when x is zero or when 1 / x does not fit the range [-2^I, 2^I), that is for |x| < 2^-I and for x == 2^-I; x == -2^-I gives lowest().

Definition at line 3006 of file fixed_point128.h.

◆ _sincos_cordic

template<int32_t I>
void _sincos_cordic ( fixed_point128< I > x,
fixed_point128< I > & sin_x,
fixed_point128< I > & cos_x,
bool apply_scale_factor )
friend

Calculate Sine and Cosine using CORDIC using a limited range of [-pi/2, pi/2] 64 iterations, one per entry of the arctangent table, which give an absolute accuracy of about 2^-63: measured within 1.1e-19 of mpmath for |x| < 1. The table used to skip arctan(2^-62), which held the error near 1.8e-19. Not used by tan(), which divides sin() by cos().

Parameters
x
sin_xResult of the Sine of x.
cos_xResult of the Cosine of x.
apply_scale_factorApply a scale factor when 60 or more iterations are performed. This is required to get accurate results when the number of iterations is high, but it can be disabled for performance reasons when the number of iterations is low.

Definition at line 3041 of file fixed_point128.h.

◆ sin1

template<int32_t I>
fixed_point128 sin1 ( fixed_point128< I > x)
friend

Calculate the sine function over a limited range [-0.5pi, 0.5pi] Using the Maclaurin series expansion, the formula is: x^3 x^5 x^7 sin(x) = x - — + — - — + ... 3! 5! 7! The series runs in a wider fraction than this instantiation has, see TrigSeries().

Parameters
xvalue in Radians in the range [-0.5pi, 0.5pi]
Returns
Sine of x. See sin() for the accuracy.

Definition at line 3185 of file fixed_point128.h.

◆ cos1

template<int32_t I>
fixed_point128 cos1 ( const fixed_point128< I > & x)
friend

Calculate the cosine function over a limited range [-0.5pi, 0.5pi] Using the Maclaurin series expansion, the formula is: x^2 x^4 x^6 cos(x) = 1 - — + — - — + ... 2! 4! 6! Evaluated directly rather than as sin1(pi/2 - x), which carried the rounding error of pi/2 into every result; cos1(0) is now exactly one. See TrigSeries().

Parameters
xvalue in Radians in the range [-0.5pi, 0.5pi]
Returns
Cosine of x. See cos() for the accuracy.

Definition at line 3206 of file fixed_point128.h.

◆ sin

template<int32_t I>
fixed_point128 sin ( fixed_point128< I > x)
friend

Calculate the Sine function Ultimately uses sin1() or cos1() with a reduced range of [-pi/4, pi/4].

Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 2.5 ulp for |x| < 4 (1.4 ulp from I = 16 up). Beyond that the reduction x - n * pi/2 adds n times the rounding error of pi/2 in this instantiation, at most |x| / 3 ulp more: 320 ulp for |x| near 1000 at I = 32.

Parameters
xvalue in Radians
Returns
Sine of x

Definition at line 3224 of file fixed_point128.h.

◆ asin

template<int32_t I>
fixed_point128 asin ( fixed_point128< I > x)
friend

Calculate the inverse sine function Newton's method on sin() for |x| up to one half, see AsinNewton(). Beyond that the identity asin(x) = pi/2 - 2 asin(sqrt((1 - x) / 2)) keeps the iteration away from pi/2, where the derivative it divides by vanishes and the refinement stalled at double precision: asin(1) was 4e16 ulp off at fixed_point128<16>. The square root is correctly rounded, so the identity costs a few ulp at most.

Accuracy, measured against mpmath at I = 4, 16, 32, 62 and 63: within 3.5 ulp.

Parameters
xvalue in radians in the range [-1,1]
Returns
Inverse sine of x, zero outside [-1,1]

Definition at line 3257 of file fixed_point128.h.

◆ cos

template<int32_t I>
fixed_point128 cos ( fixed_point128< I > x)
friend

Calculate the cosine function Ultimately uses sin1() or cos1() with a reduced range of [-pi/4, pi/4]. cos(0) is exactly one.

Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 1.8 ulp for |x| < 4 (1.3 ulp from I = 16 up), plus at most |x| / 3 ulp of argument reduction beyond, see sin().

Parameters
xvalue in Radians
Returns
Cosine of x

Definition at line 3283 of file fixed_point128.h.

◆ acos

template<int32_t I>
fixed_point128 acos ( fixed_point128< I > x)
friend

Calculate the inverse cosine function pi/2 - asin(x) for |x| up to one half. Beyond that acos(x) = 2 asin(sqrt((1 - x) / 2)) for a positive x and pi - 2 asin(sqrt((1 + x) / 2)) for a negative one, which keep Newton's method away from the ends of the domain: acos(-1) was 8e16 ulp off at fixed_point128<16>.

Accuracy, measured against mpmath at I = 4, 16, 32, 62 and 63: within 4 ulp.

Parameters
xvalue in radians in the range [-1,1]
Returns
Inverse cosine of x, zero outside [-1,1]

Definition at line 3314 of file fixed_point128.h.

◆ tan

template<int32_t I>
fixed_point128 tan ( fixed_point128< I > x)
friend

Calculate the tangent function tan(x) = sin(x)/cos(x)

The error of the cosine is divided by the cosine itself, so it grows towards the poles. Measured against mpmath for |x| < 4 at I = 4, 16, 32, 62 and 63: within 45 * max(1, |tan(x)|) ulp.

Parameters
xvalue
Returns
Tangent of x. Zero at the poles (odd multiples of pi/2), where the true value is unbounded and the cosine comes out exactly zero.

Definition at line 3339 of file fixed_point128.h.

◆ atan

template<int32_t I>
fixed_point128 atan ( fixed_point128< I > x)
friend

Calculate the inverse tangent function Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 2.2 ulp.

Parameters
xvalue
Returns
Arctangent of x

Definition at line 3365 of file fixed_point128.h.

◆ atan2

template<int32_t I>
fixed_point128 atan2 ( fixed_point128< I > y,
fixed_point128< I > x )
friend

Calculate the inverse tangent function of the ratio y / x Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 3.3 ulp.

Parameters
yvalue
xvalue
Returns
Arctangent of y / x in the range [-pi, pi]

Definition at line 3414 of file fixed_point128.h.

◆ sinh

template<int32_t I>
fixed_point128 sinh ( const fixed_point128< I > & x)
friend

Calculate the hyperbolic sine function Use the exponent function which produces more accurate results than the power series. e^x - e^(-x) sinh(x) = ---------— 2 Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 10 * max(1, |sinh(x)|) ulp at I = 4 and 3 * max(1, |sinh(x)|) ulp from I = 16 up. e^x itself has to fit the range, so a result between 2^(I-1) and 2^I overflows.

Parameters
xvalue
Returns
Sine of x

Definition at line 3460 of file fixed_point128.h.

◆ asinh

template<int32_t I>
fixed_point128 asinh ( const fixed_point128< I > & x)
friend

Calculates the inverse hyperbolic sine For positive x: asinh(x) = log(x + sqrt(x^2 + 1)) For negative x, the function returns the result with the sign inverted. The root is exact before it is rounded (RootOfSquareAndOne()) and the sum is kept from overflowing (LogOfSum()), so every argument works, lowest() included. Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 over the whole range: within 2.8 ulp.

Parameters
xvalue
Returns
Inverse hyperbolic sine of x

Definition at line 3503 of file fixed_point128.h.

◆ cosh

template<int32_t I>
fixed_point128 cosh ( const fixed_point128< I > & x)
friend

Calculate the hyperbolic cosine function e^x + e^(-x) cosh(x) = ---------— 2 Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 10 * max(1, cosh(x)) ulp at I = 4 and 3 * max(1, cosh(x)) ulp from I = 16 up. e^|x| itself has to fit the range, see sinh().

Parameters
xvalue
Returns
Hyperbolic cosine of x

Definition at line 3521 of file fixed_point128.h.

◆ acosh

template<int32_t I>
fixed_point128 acosh ( const fixed_point128< I > & x)
friend

Calculates the inverse hyperbolic cosine For x >= 1: acosh(x) = log(x + sqrt(x^2 - 1)) For x < 1, the function return zero Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 over the whole range: within 2.9 ulp. It was out by up to 5e8 ulp just above one, where x^2 - 1 cancels.

Parameters
xvalue in the range [1, inf]
Returns
Inverse hyperbolic cosine of x

Definition at line 3559 of file fixed_point128.h.

◆ tanh

template<int32_t I>
fixed_point128 tanh ( const fixed_point128< I > & x)
friend

Calculates the hyperbolic tangent e^x - e^(-x) 1 - e^(-2|x|) tanh(x) = ---------— = ------------—, with the sign of x e^x + e^(-x) 1 + e^(-2|x|) The second form is the one evaluated, for every argument. Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 4.2 ulp.

Parameters
xvalue
Returns
hyperbolic tangent of x

Definition at line 3579 of file fixed_point128.h.

◆ atanh

template<int32_t I>
fixed_point128 atanh ( const fixed_point128< I > & x)
friend

Calculates the inverse hyperbolic tangent 1 + x atanh(x) = 0.5 * log( --—) = 0.5 * (log(1 + x) - log(1 - x)) 1 - x The second form is the one evaluated. Both of its logarithms take an argument in (0, 2), inside the range of every instantiation, while the quotient of the first form leaves the range as x approaches one - beyond 0.88 at fixed_point128<4>, within 2^-15 of one at fixed_point128<16> - and the wrapped value made log() throw out of this noexcept function, terminating the process. Accuracy, measured against mpmath at I = 4, 16, 32, 62 and 63: within 1.4 ulp. Below I = 4 the same holds where log(1 - x) fits the range, which log() needs for its log2 (see log()): for |x| up to 0.75 at I = 1 and 0.9375 at I = 2.

Parameters
xvalue in the range (-1, 1)
Returns
Inverse hyperbolic tangent of x, zero outside (-1, 1). A result beyond the range of the instantiation, which only a narrow one can meet, wraps like any overflow.

Definition at line 3612 of file fixed_point128.h.

◆ exp

template<int32_t I>
fixed_point128 exp ( const fixed_point128< I > & x)
friend

Calculates the exponent of x: e Using the Maclaurin series expansion, the formula is: 1 2 3 x x x exp(x) = 1 + — + — + — + ... 1! 2! 3!

                                  x

The Maclaurin series will quickly overflow as x's power increases rapidly.

                x   ix   fx

Using the equality e = e * e Where ix is the integer part of x and fx is the fraction part. ix is computed via multiplications which won't overflow if the result value can be held. fx is computed via Maclaurin series expansion, but since fx < 1, it won't overflow. A negative x works with 1/e and the alternating series of e^-fx, so it never forms e^|x|.

Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 6.2 * max(1, e^x) ulp. The argument itself is only known to half an ulp, which alone moves a large result by e^x / 2 ulp.

Parameters
xA number specifying a power.
Returns
Exponent of x

Definition at line 3645 of file fixed_point128.h.

◆ expm1

template<int32_t I>
fixed_point128 expm1 ( const fixed_point128< I > & x)
friend

Calculates the exponent of x and reduces 1 from the result: (e^x) - 1 Evaluated as exp(x) - 1, which in a fixed point type loses nothing to cancellation: the absolute error is that of exp(), within 9.3 * max(1, |e^x - 1|) ulp measured at I = 4 and 6.2 * max(1, e^x) ulp from I = 16 up.

Parameters
xA number specifying a power.
Returns
Exponent of x

Definition at line 3699 of file fixed_point128.h.

◆ exp2

template<int32_t I>
fixed_point128 exp2 ( const fixed_point128< I > & x)
friend

Computes 2 to the power of x exp(x * ln(2)), the constant applied with 128 fraction bits. Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 5.5 * max(1, 2^x) ulp.

Parameters
xExponent value
Returns
2^x

Definition at line 3711 of file fixed_point128.h.

◆ pow

template<int32_t I>
fixed_point128 pow ( const fixed_point128< I > & x,
const fixed_point128< I > & y )
friend

Computes x to the power of y exp(y * log(x)). Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 7.3 * max(1, x^y) ulp at I = 4 and 4.4 * max(1, x^y) ulp from I = 16 up. Exact powers are not exact: pow(2, 3) is 8 - 15 ulp at I = 16. y * log(x) has to fit the range.

Parameters
xBase value, must be positive
yExponent value
Returns
x^y, one when x is one, zero when x is negative, and for x = 0 one when y = 0 and zero otherwise

Definition at line 3732 of file fixed_point128.h.

◆ log2

template<int32_t I>
fixed_point128 log2 ( fixed_point128< I > x)
friend

Calculates the Log base 2 of x: y = log2(x) Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 2 ulp at I = 1, 1.7 ulp at I = 2 and 1.1 ulp from I = 4 up. The compile time evaluation takes a different, bitwise path that truncates instead: it can be one ulp below the runtime result. A result outside the range wraps, which a narrow instantiation meets for small arguments: log2(x) needs x >= 2^(-2^I).

Parameters
xThe number to perform log2 on.
Returns
log2(x)

Definition at line 3767 of file fixed_point128.h.

◆ log

template<int32_t I>
fixed_point128 log ( fixed_point128< I > x)
friend

Calculates the natural Log (base e) of x: log(x) log2(x) * ln(2), the constant applied with 128 fraction bits by MulByFraction128() - a constant held to this instantiation's F bits, multiplied by a log2 of up to 63, would cost tens of ulp. Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 1.9 ulp. log2(x) has to fit the range as well, so a narrow instantiation cannot take the log of an argument below 2^(-2^I) even when its log would fit: at I = 1 that is x below 0.25.

Parameters
xThe number to perform log on.
Returns
log(x)
Exceptions
std::domain_errorwhen x is zero or negative.

Definition at line 3812 of file fixed_point128.h.

◆ log1p

template<int32_t I>
fixed_point128 log1p ( fixed_point128< I > x)
friend

Calculates the natural Log (base e) of 1 + x: log(1 + x) Not noexcept: it forwards to log(), which rejects a non positive argument. Marking it noexcept would turn that domain_error into a call to std::terminate. 1 + x is exact in a fixed point type, so the error is that of log(): within 1.2 ulp measured, with log()'s limit on small arguments at I < 7.

Parameters
xThe number to perform log on.
Returns
log1p(x)
Exceptions
std::domain_errorwhen x is -1 or below.

Definition at line 3832 of file fixed_point128.h.

◆ log10

template<int32_t I>
fixed_point128 log10 ( fixed_point128< I > x)
friend

Calculates Log base 10 of x: log10(x) log2(x) * log10(2), the constant applied with 128 fraction bits, see log(). Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 1 ulp, with log()'s limit on small arguments at I < 7.

Parameters
xThe number to perform log on.
Returns
log10(x)

Definition at line 3840 of file fixed_point128.h.

◆ logb

template<int32_t I>
fixed_point128 logb ( fixed_point128< I > x)
friend

Calculates Log base 2 of x as an integer ignoring the sign of x. floor(log2(fabs(x))), as the C function: logb(-8) is 3. A narrow instantiation cannot hold the exponent of its smallest values - logb(epsilon()) is -126 for fixed_point128<1> - and the result then wraps like any other overflow.

Parameters
xThe number to perform log on.
Returns
logb(x)
Exceptions
std::domain_errorwhen x is zero. The C function returns minus infinity for it, which this type cannot represent.

Definition at line 3862 of file fixed_point128.h.

Member Data Documentation

◆ F

template<int32_t I>
int32_t fp128::fixed_point128< I >::F = 127 - I
staticconstexpr

Fraction bit count (127 - I). The remaining bit is the sign.

Definition at line 322 of file fixed_point128.h.

◆ max_int_value

template<int32_t I>
uint64_t fp128::fixed_point128< I >::max_int_value = FP128_MAX_VALUE_64(I)
staticconstexpr

Largest representable positive integer, 2^I - 1.

Definition at line 581 of file fixed_point128.h.


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