#include <bn_fixed.h>
template<int Precision>
fixed_t class
Poor man's float.
Template parameters | |
---|---|
Precision | Number of bits used for the fractional part. |
It fakes numbers with decimal points with integers.
Public static functions
-
static auto from_data(int data) -> fixed_
t constexpr - Returns a fixed_
t with the given internal data. - static auto precision() -> int constexpr
- Returns the number of bits used for the fractional part.
- static auto scale() -> int constexpr
- Returns the internal data of fixed_t<Precision>(1);.
- static auto half_scale() -> int constexpr
- Returns the internal data of fixed_
t<Precision / 2>(1);.
Constructors, destructors, conversion operators
- fixed_t() defaulted constexpr
- Default constructor.
- fixed_t(int value) constexpr
- Constructor.
- fixed_t(unsigned value) constexpr
- Constructor.
- fixed_t(float value) constexpr
- Constructor.
- fixed_t(double value) constexpr
- Constructor.
-
template<int OtherPrecision>fixed_t(fixed_
t<OtherPrecision> other) constexpr - Constructor.
- operator int() const explicit constexpr
- Returns the integer part using a division.
- operator float() const explicit constexpr
- Returns the nearest single precision floating point value.
- operator double() const explicit constexpr
- Returns the nearest double precision floating point value.
Public functions
- auto data() const -> int constexpr
- Returns the internal data.
- auto integer() const -> int constexpr
- Returns the integer part using a division.
- auto right_shift_integer() const -> int constexpr
- Returns the integer part using a right shift.
- auto unsigned_integer() const -> unsigned constexpr
- Returns the integer part, assuming that it is >= 0.
- auto round_integer() const -> int constexpr
- Returns the nearest integer value.
- auto floor_integer() const -> int constexpr
- Returns the largest integer value not greater than this value.
- auto ceil_integer() const -> int constexpr
- Returns the smallest integer value not less than this value.
- auto fraction() const -> int constexpr
- Returns the fractional part.
- auto to_float() const -> float constexpr
- Returns the nearest single precision floating point value.
- auto to_double() const -> double constexpr
- Returns the nearest double precision floating point value.
-
auto multiplication(int value) const -> fixed_
t constexpr - Returns the multiplication of this value by the given integer value.
-
auto multiplication(fixed_
t other) const -> fixed_ t constexpr - Returns the multiplication of this value by the given fixed point value, using half precision to try to avoid overflow.
-
auto safe_multiplication(int value) const -> fixed_
t constexpr - Returns the multiplication of this value by the given integer value.
-
auto safe_multiplication(fixed_
t other) const -> fixed_ t constexpr - Returns the multiplication of this value by the given fixed point value, casting them to int64_t to try to avoid overflow.
-
auto unsafe_multiplication(int value) const -> fixed_
t constexpr - Returns the multiplication of this value by the given integer value.
-
auto unsafe_multiplication(fixed_
t other) const -> fixed_ t constexpr - Returns the multiplication of this value by the given fixed point value without trying to avoid overflow.
-
auto division(int value) const -> fixed_
t constexpr - Returns the division of this value by the given integer value.
-
auto division(fixed_
t other) const -> fixed_ t constexpr - Returns the division of this value by the given fixed point value, using half precision to try to avoid overflow.
-
auto safe_division(int value) const -> fixed_
t constexpr - Returns the division of this value by the given integer value.
-
auto safe_division(fixed_
t other) const -> fixed_ t constexpr - Returns the division of this value by the given fixed point value, casting them to int64_t to try to avoid overflow.
-
auto unsafe_division(int value) const -> fixed_
t constexpr - Returns the division of this value by the given integer value.
-
auto unsafe_division(fixed_
t other) const -> fixed_ t constexpr - Returns the division of this value by the given fixed point value without trying to avoid overflow.
-
auto modulo(int value) const -> fixed_
t constexpr - Returns the remainder of the division of this value by the given integer value.
-
auto operator-() const -> fixed_
t constexpr - Returns a fixed_
t that is formed by changing the sign of this one. -
auto operator+=(fixed_
t other) -> fixed_ t& constexpr - Adds the given fixed_
t to this one. -
auto operator-=(fixed_
t other) -> fixed_ t& constexpr - Subtracts the given fixed_
t to this one. -
auto operator*=(int value) -> fixed_
t& constexpr - Multiplies this fixed_
t by the given factor. -
auto operator*=(fixed_
t other) -> fixed_ t& constexpr - Multiplies this fixed_
t by the given factor. -
auto operator/=(int value) -> fixed_
t& constexpr - Divides this fixed_
t by the given divisor. -
auto operator/=(fixed_
t other) -> fixed_ t& constexpr - Divides this fixed_
t by the given divisor. -
auto operator%=(int other) -> fixed_
t& constexpr - Assigns the remainder of the division of this fixed_
t by the given divisor.
Friends
-
auto operator+(fixed_
t a, fixed_ t b) -> fixed_ t constexpr - Returns the sum of a and b.
-
template<int OtherPrecision>auto operator+(fixed_
t a, fixed_ t<OtherPrecision> b) -> fixed_ t<bn::max(Precision, OtherPrecision)> constexpr - Returns the sum of a and b.
-
auto operator-(fixed_
t a, fixed_ t b) -> fixed_ t constexpr - Returns the subtraction of a and b.
-
template<int OtherPrecision>auto operator-(fixed_
t a, fixed_ t<OtherPrecision> b) -> fixed_ t<bn::max(Precision, OtherPrecision)> constexpr - Returns the subtraction of a and b.
-
auto operator*(fixed_
t a, int b) -> fixed_ t constexpr - Returns a multiplied by b.
-
auto operator*(fixed_
t a, fixed_ t b) -> fixed_ t constexpr - Returns a multiplied by b.
-
template<int OtherPrecision>auto operator*(fixed_
t a, fixed_ t<OtherPrecision> b) -> fixed_ t<bn::max(Precision, OtherPrecision)> constexpr - Returns a multiplied by b.
-
auto operator/(fixed_
t a, int b) -> fixed_ t constexpr - Returns a divided by b.
-
auto operator/(fixed_
t a, fixed_ t b) -> fixed_ t constexpr - Returns a divided by b.
-
template<int OtherPrecision>auto operator/(fixed_
t a, fixed_ t<OtherPrecision> b) -> fixed_ t<bn::max(Precision, OtherPrecision)> constexpr - Returns a divided by b.
-
auto operator%(fixed_
t a, int b) -> fixed_ t constexpr - Returns the remainder of the division of a by b.
-
auto operator<=>(fixed_
t a, fixed_ t b) -> auto defaulted constexpr - Default three-way comparison operator.
Function documentation
template<int Precision>
bn:: fixed_t<Precision>:: fixed_t(int value) constexpr
Constructor.
Parameters | |
---|---|
value | Integer value. |
template<int Precision>
bn:: fixed_t<Precision>:: fixed_t(unsigned value) constexpr
Constructor.
Parameters | |
---|---|
value | Unsigned integer value. |
template<int Precision>
bn:: fixed_t<Precision>:: fixed_t(float value) constexpr
Constructor.
Parameters | |
---|---|
value | Single precision floating point value. |
template<int Precision>
bn:: fixed_t<Precision>:: fixed_t(double value) constexpr
Constructor.
Parameters | |
---|---|
value | Double precision floating point value. |
template<int Precision>
fixed_ t& bn:: fixed_t<Precision>:: operator+=(fixed_ t other) constexpr
Adds the given fixed_
Parameters | |
---|---|
other | fixed_ |
Returns | Reference to this. |
template<int Precision>
fixed_ t& bn:: fixed_t<Precision>:: operator-=(fixed_ t other) constexpr
Subtracts the given fixed_
Parameters | |
---|---|
other | fixed_ |
Returns | Reference to this. |
template<int Precision>
fixed_ t& bn:: fixed_t<Precision>:: operator*=(int value) constexpr
Multiplies this fixed_
Parameters | |
---|---|
value | Integer multiplication factor. |
Returns | Reference to this. |
template<int Precision>
fixed_ t& bn:: fixed_t<Precision>:: operator*=(fixed_ t other) constexpr
Multiplies this fixed_
Parameters | |
---|---|
other | Fixed point multiplication factor. |
Returns | Reference to this. |
template<int Precision>
fixed_ t& bn:: fixed_t<Precision>:: operator/=(int value) constexpr
Divides this fixed_
Parameters | |
---|---|
value | Valid integer divisor (!= 0). |
Returns | Reference to this. |
template<int Precision>
fixed_ t& bn:: fixed_t<Precision>:: operator/=(fixed_ t other) constexpr
Divides this fixed_
Parameters | |
---|---|
other | Valid fixed point divisor (!= 0). |
Returns | Reference to this. |
template<int Precision>
fixed_ t& bn:: fixed_t<Precision>:: operator%=(int other) constexpr
Assigns the remainder of the division of this fixed_
Parameters | |
---|---|
other | Valid integer divisor (!= 0). |
Returns | Reference to this. |