Ratio

#include <xo/ratio/ratio.hpp>

A ratio represents an exact fraction of two integers.

template<typename Int>
class ratio

represent a ratio of two Int values.

Instance Variables

group Ratio-instance-variables

Variables

Int num_ = 0

numerator

Int den_ = 1

denominator

Types

group Ratio-types

Typedefs

using component_type = Int

representation for (numerator, denominator)

Constructors

group Ratio-ctor

Functions

constexpr ratio() = default

construct ratio 0/1

inline constexpr ratio(Int n, Int d = 1)

construct ratio with numerator n and denominator d.

Ratio need not be normalized

Static Methods

group Ratio-static-methods

Functions

static inline ratio reduce(Int n, Int d)

ratio in lowest commono terms

static inline ratio add(const ratio &x, const ratio &y)

add ratios x and y

Post:

result ratio is normalized

static inline ratio subtract(const ratio &x, const ratio &y)

subtract ratio y from ratio x

Post:

result ratio is normalized

static inline ratio multiply(const ratio &x, const ratio &y)

multiply ratios x and y

Post:

result ratio is normalized

static inline ratio divide(const ratio &x, const ratio &y)

divide ratio y into ratio x

Post:

result ratio is normalized

static inline auto compare(ratio x, ratio y)

3-way compare two ratios

Access Methods

group Ratio-access

Functions

inline Int num() const noexcept

fetch ratio’s numerator

inline Int den() const noexcept

fetch ratio’s denominator

inline bool is_zero() const noexcept

true if and only if ratio is equal to zero

inline bool is_unity() const noexcept

true if and only if ratio is equal to one

inline bool is_integer() const noexcept

true if and only if ratio represents an integer

(denominator is +/- 1)

General Methods

group Ratio-methods

Functions

inline ratio negate() const

r.negate() is the exact ratio representing -r

inline ratio reciprocal() const

r.reciprocal() is the eact ratio representing 1/r

inline Int floor() const

r.floor() is the largest integer x : x <= r

inline Int ceil() const

r.ceil() is the smallest integer x : r < x.

inline ratio normalize() const

reduce to lowest terms

Pre:

Int type must be totally ordered

inline ratio maybe_reduce() const

reduce to lowest terms, if Int representation admits

Otherwise fallback to identity function

inline ratio frac() const

return fractional part of this ratio

Pre:

Int type must be totally ordered

inline ratio power(int p) const

compute integer exponent p of this ratio

Note

time complexity is O(log p)

inline ratio operator-() const

negate operator

Conversion

group Ratio-conversion

Functions

template<typename Repr>
inline Repr convert_to() const noexcept

convert to non-ratio representation

For example: to int or double

template<std::size_t N>
inline flatstring<N> to_str() const noexcept

convert to short human-friendly flatstring representation

Example:

ratio(7,1).to_str<5>();   // "7"
ratio(1,7).to_str<5>();   // "(1/7)"
ratio(-1,7).to_str<10>(); // "(-1/7)"
ratio(-1,7).to_str<5>();  // "(-1/"

template<typename Ratio2>
inline constexpr operator Ratio2() const noexcept

convert to representation using different integer types