Numerical constants#

#include <heyoka/number.hpp>

The number class#

class number#

This class is used to represent numerical constants in heyoka’s expression system.

It consists of a union of several floating-point types.

using value_type = std::variant<float, double, long double, mppp::real128, mppp::real>#

Note

mppp::real128 and mppp::real are supported only if heyoka was built with support for the mp++ library (see the installation instructions).

The union of floating-point types.

number() noexcept#

The default constructor initialises the internal union with the 0. literal.

number(const number&)#
number(number&&) noexcept#
number &operator=(const number&)#
number &operator=(number&&) noexcept#
~number()#

Numbers are copy/move constructible, copy/move assignable and destructible.

Moved-from numbers are guaranteed to be in the default-constructed state.

Exception:

any exception thrown by the copy constructor/copy assignment operator of mppp::real.

explicit number(float x) noexcept#
explicit number(double x) noexcept#
explicit number(long double x) noexcept#
explicit number(mppp::real128 x) noexcept#
explicit number(mppp::real x)#

Note

The mppp::real128 and mppp::real overloads are available only if heyoka was built with support for the mp++ library (see the installation instructions).

Numbers can be initialised from values of the supported floating-point types.

Parameters:

x – the construction argument.

Exception:

any exception raised by the copy constructor of mppp::real.

[[nodiscard]] const value_type &value() const noexcept#

Value getter.

Returns:

a reference to the internal union of floating-point types.

Functions#

void swap(number &a, number &b) noexcept#

Swap primitive.

This function will efficiently swap a and b.

Parameters:
  • a – the first number.

  • b – the second number.

std::ostream &operator<<(std::ostream &os, const number &n)#

Stream operator.

Parameters:
  • os – the output stream.

  • n – the input number.

Returns:

a reference to os.

Exception:

any exception thrown by streaming the value of n.

Operators#

number operator+(number n)#
number operator-(const number &n)#

The number class supports the identity and negation operators.

Parameters:

n – the input argument.

Returns:

n or its negation.

Exception:

any exception raised by the constructors of number.

number operator+(const number &x, const number &y)#
number operator-(const number &x, const number &y)#
number operator*(const number &x, const number &y)#
number operator/(const number &x, const number &y)#

The number class supports elementary binary arithmetics.

If the active floating-point types of x and y differ, the active type of the result will be the wider among the operands’ types.

Parameters:
  • x – the first operand.

  • y – the second operand.

Returns:

the result of the binary operation.

Exception:

any exception raised by the constructors of number or by the implementation of the underlying arithmetic operation.

Throws:

std::invalid_argument – if the active types of x and y differ and they don’t support mixed-mode airthmetics.

bool operator==(const number &x, const number &y) noexcept#
bool operator!=(const number &x, const number &y) noexcept#

Equality comparison operators.

Two numbers are considered equal if:

  • their active types are equal, and

  • their values are equal.

Two NaN values are considered equivalent by these comparison operators.

Parameters:
  • x – the first operand.

  • y – the second operand.

Returns:

the result of the comparison.

bool operator<(const number &x, const number &y) noexcept#

Less-than comparison operator.

x is less than y if:

  • the active type of x is narrower than the active type of y, or

  • the active types of x and y are the same, and the value of x is less than the value of y.

NaN values are considered greater than non-NaN values by this operator.

Parameters:
  • x – the first operand.

  • y – the second operand.

Returns:

the result of the comparison.

Standard library specialisations#

template<>
struct std::hash<heyoka::number>#

Specialisation of std::hash for heyoka::number.

The hash value of NaNs depends only on the active floating-point type. That is, all NaNs of a floating-point type hash to the same value.

std::size_t operator()(const heyoka::number &n) const noexcept#
Parameters:

n – the input heyoka::number.

Returns:

a hash value for n.