#include <bn_fixed.h>
template<int Precision>
bn::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.

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 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.

Friends

auto operator+(fixed_t a, fixed_t b) -> fixed_t constexpr
Returns the sum of a and b.
auto operator-(fixed_t a, fixed_t b) -> fixed_t constexpr
Returns b subtracted from a.
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.
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.
auto operator<=>(fixed_t a, fixed_t b) -> auto defaulted constexpr
Default three-way comparison operator.

Function documentation

template<int Precision>
static fixed_t bn::fixed_t<Precision>::from_data(int data) constexpr

Returns a fixed_t with the given internal data.

If you want to convert an integer value to fixed_t, you should use its constructor.

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> template<int OtherPrecision>
bn::fixed_t<Precision>::fixed_t(fixed_t<OtherPrecision> other) constexpr

Constructor.

Parameters
other Fixed point value.

template<int Precision>
fixed_t& bn::fixed_t<Precision>::operator+=(fixed_t other) constexpr

Adds the given fixed_t to this one.

Parameters
other fixed_t to add.
Returns Reference to this.

template<int Precision>
fixed_t& bn::fixed_t<Precision>::operator-=(fixed_t other) constexpr

Subtracts the given fixed_t to this one.

Parameters
other fixed_t to subtract.
Returns Reference to this.

template<int Precision>
fixed_t& bn::fixed_t<Precision>::operator*=(int value) constexpr

Multiplies this fixed_t by the given factor.

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_t by the given factor.

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_t by the given divisor.

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_t by the given divisor.

Parameters
other Valid fixed point divisor (!= 0).
Returns Reference to this.