tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
ratio.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_RATIO_RATIO_HPP
5#define TETL_RATIO_RATIO_HPP
6
7#include <etl/_cstdint/intmax_t.hpp>
8#include <etl/_math/abs.hpp>
9#include <etl/_math/sign.hpp>
10#include <etl/_numeric/gcd.hpp>
11
12namespace etl {
13
14/// \ingroup ratio
15/// @{
16
17/// \brief The typename template provides compile-time rational
18/// arithmetic support. Each instantiation of this template exactly represents
19/// any finite rational number as long as its numerator Num and denominator
20/// Denom are representable as compile-time constants of type intmax_t.
21template <intmax_t Num, intmax_t Denom = 1>
22struct ratio {
23 static constexpr intmax_t num = detail::sign(Num) * detail::sign(Denom) * abs(Num) / gcd(Num, Denom);
24 static constexpr intmax_t den = abs(Denom) / gcd(Num, Denom);
25
26 using type = ratio<num, den>;
27};
28
29using atto = ratio<1, 1'000'000'000'000'000'000>;
30using femto = ratio<1, 1'000'000'000'000'000>;
31using pico = ratio<1, 1'000'000'000'000>;
32using nano = ratio<1, 1'000'000'000>;
33using micro = ratio<1, 1'000'000>;
34using milli = ratio<1, 1'000>;
35using centi = ratio<1, 100>;
36using deci = ratio<1, 10>;
37using deca = ratio<10, 1>;
38using hecto = ratio<100, 1>;
39using kilo = ratio<1'000, 1>;
40using mega = ratio<1'000'000, 1>;
41using giga = ratio<1'000'000'000, 1>;
42using tera = ratio<1'000'000'000'000, 1>;
43using peta = ratio<1'000'000'000'000'000, 1>;
44using exa = ratio<1'000'000'000'000'000'000, 1>;
45
46/// @}
47
48} // namespace etl
49
50#endif // TETL_RATIO_RATIO_HPP
constexpr auto gcd(M m, N n) noexcept -> etl::common_type_t< M, N >
Computes the greatest common divisor of the integers m and n.
Definition gcd.hpp:17
Definition adjacent_find.hpp:9
constexpr auto abs(long n) noexcept -> long
Definition abs.hpp:35
The typename template provides compile-time rational arithmetic support. Each instantiation of this t...
Definition ratio.hpp:22
static constexpr intmax_t den
Definition ratio.hpp:24
static constexpr intmax_t num
Definition ratio.hpp:23