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
Types
- group ratio-types
Typedefs
-
using component_type = Int
representation for (numerator, denominator)
-
using component_type = Int
Constructors
- group ratio-ctor
Static Methods
- group ratio-static-methods
Functions
-
static inline constexpr ratio add(const ratio &x, const ratio &y)
add ratios
x
andy
- Post:
result ratio is normalized
-
static inline constexpr ratio subtract(const ratio &x, const ratio &y)
subtract ratio
y
from ratiox
- Post:
result ratio is normalized
-
static inline constexpr ratio multiply(const ratio &x, const ratio &y)
multiply ratios
x
andy
- Post:
result ratio is normalized
-
static inline constexpr ratio add(const ratio &x, const ratio &y)
Access Methods
- group ratio-access
Functions
-
inline constexpr Int num() const noexcept
fetch ratio’s numerator
-
inline constexpr Int den() const noexcept
fetch ratio’s denominator
-
inline constexpr bool is_zero() const noexcept
true if and only if ratio is equal to zero
-
inline constexpr bool is_unity() const noexcept
true if and only if ratio is equal to one
-
inline constexpr bool is_integer() const noexcept
true if and only if ratio represents an integer
(denominator is +/- 1)
-
inline constexpr Int num() const noexcept
General Methods
- group ratio-methods
Functions
-
inline constexpr Int floor() const
r.floor() is the largest integer x : x <= r
-
inline constexpr Int ceil() const
r.ceil() is the smallest integer x : r < x.
-
inline constexpr ratio normalize() const
reduce to lowest terms
- Pre:
Int
type must be totally ordered
-
inline constexpr ratio maybe_reduce() const
reduce to lowest terms, if Int representation admits
Otherwise fallback to identity function
-
inline constexpr ratio frac() const
return fractional part of this ratio
- Pre:
Int
type must be totally ordered
-
inline constexpr Int floor() const
Conversion
- group ratio-conversion
Functions
-
template<typename Repr>
inline constexpr Repr to() const noexcept convert to non-ratio representation
For example: to int or double
-
template<typename Repr>