Skip to main content

Q5

Defines a "floating point number" with 5 bits reserved for the fractional part of the decimal number. Q5 implements some simple arithmetic operations, most yielding a Q5 number and some yielding a Q10 number as a result. Other operations also work with Q15 numbers.

See: Q10, Q15

Public Functions

intceil() const
Convert the Q5 value to an integer by removing the 5 bits used for the fraction.
intfloor() const
Convert the Q5 value to an integer by removing the 5 bits used for the fraction.
operator int() const
Gets the Q5 as an integer without conversion.
Q5operator%(const Q5 q5) const
Modulus operator.
Q5 &operator%=(const Q5 q5)
Modulus operator.
Q5operator*(const int i) const
Multiplication operator.
Q5operator*(const Q15 & q15) const
Multiplication operator.
Q10operator*(const Q5 & q5) const
Multiplication operator.
Q5operator+(const Q5 & q5) const
Addition operator.
Q5 &operator+=(const Q5 & q5)
Addition operator.
Q5operator-() const
Negation operator.
Q5operator-(const Q5 & q5) const
Subtraction operator.
Q5 &operator-=(const Q5 & q5)
Subtraction operator.
Q5operator/(const int i) const
Division operator.
Q5operator/(const Q5 q5) const
Division operator.
Q5()
Initializes a new instance of the Q5 class.
Q5(const Q10 q10)
Constructor from Q10.
Q5(int i)
Constructor from integer.
intround() const
Round the Q5 value to the nearest integer value.
template <typename T >
T
to() const
Convert the Q5 value to an integer by removing the 5 bits used for the fraction, or to a floating point value by dividing by 32, depending on the type specified as T.

Public Functions Documentation

ceil

int ceil()const

Convert the Q5 value to an integer by removing the 5 bits used for the fraction.

The number is rounded up to the nearest integer.

Returns:

The first integer value higher than (or equal to) the Q5 value.

See also:

floor

int floor()const

Convert the Q5 value to an integer by removing the 5 bits used for the fraction.

The number is rounded down to the nearest integer.

Returns:

The first integer value higher than (or equal to) the Q5 value.

See also:

operator int

Gets the Q5 as an integer without conversion.

Returns:

The unconverted Q5 value.

operator%

Q5 operator%(const Q5q5)

Modulus operator.

Parameters:
q5The Q5 to modulus this by.
Returns:

The result of the operation.

operator%=

Q5 & operator%=(const Q5q5)

Modulus operator.

Parameters:
q5The Q5 to modulus this by.
Returns:

The result of the operation.

operator*

Q5 operator*(const inti)

Multiplication operator.

Parameters:
iThe integer to multiply this with.
Returns:

The result of the operation.

operator*

Q5 operator*(const Q15 &q15)

Multiplication operator.

Often used in relation with sine and cosine calculation which are pre-calculated as Q15. As the result is needed as a Q5, this operator multiplies with the given Q15 and converts the result to a Q5.

Parameters:
q15The Q15 to multiply this with.
Returns:

The result of the operation.

See also:

operator*

Q10 operator*(const Q5 &q5)

Multiplication operator.

The result is a Q10, not a Q5, for increased precision.

Parameters:
q5The Q5 to multiply this with.
Returns:

The result of the operation.

See also:

operator+

Q5 operator+(const Q5 &q5)

Addition operator.

Parameters:
q5The Q5 to add to this.
Returns:

The result of the operation.

operator+=

Q5 & operator+=(const Q5 &q5)

Addition operator.

Parameters:
q5The Q5 to add to this.
Returns:

The result of the operation.

operator-

Q5 operator-()const

Negation operator.

Returns:

The negative value of this.

operator-

Q5 operator-(const Q5 &q5)

Subtraction operator.

Parameters:
q5The Q5 to subtract from this.
Returns:

The result of the operation.

operator-=

Q5 & operator-=(const Q5 &q5)

Subtraction operator.

Parameters:
q5The Q5 to subtract from this.
Returns:

The result of the operation.

operator/

Q5 operator/(const inti)

Division operator.

Parameters:
iThe integer to divide this by.
Returns:

The result of the operation.

operator/

Q5 operator/(const Q5q5)

Division operator.

Internally this Q5 is converted to Q10 before the division to increased precision.

Parameters:
q5The Q5 to divide this by.
Returns:

The result of the operation.

See also:

Q5

Q5()

Initializes a new instance of the Q5 class.

Q5

Q5(const Q10q10)

Constructor from Q10.

The Q10 is shifted down to convert it to Q5, thus the value is rounded down in the conversion.

Parameters:
q10The Q10 value to convert to a Q5 value.
See also:

Q5

explicit Q5(inti)

Constructor from integer.

No conversion is done - the integer is assumed to already be in Q5 format.

Parameters:
iInteger pre-formattet in Q5 format.

round

int round()const

Round the Q5 value to the nearest integer value.

Returns:

The integer closest to the Q5 value.

to

T to()const

Convert the Q5 value to an integer by removing the 5 bits used for the fraction, or to a floating point value by dividing by 32, depending on the type specified as T.

Template Parameters:
TEither int or float.
Returns:

Q5 value as a type T.

Note

Using "to<int16_t>()" result in loss of precision. Use "(int16_t)to<int>()" instead.