1 #if !defined(sol_math_ratio_hpp_included)
2 #define sol_math_ratio_hpp_included
3 /// \addtogroup sol_math
8 * \file sol/math/ratio.hpp
9 * \brief Compute rational values at compile time
11 * \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm#ratio
13 * \todo ratio comparison
17 #include <sol/cstdint.hpp>
18 #include <sol/math/abs.hpp>
19 #include <sol/math/gcd.hpp>
20 #include <sol/math/sign.hpp>
21 #include <sol/static_assert.hpp>
24 namespace sol
{ namespace math
{
27 /// Rational value representation (calculated during compile-time)
28 template <intmax_t N
, intmax_t D
=1>
33 static_assert(D
!= 0, "D != 0");
38 static intmax_t const num
= N
* sign
<D
>::value
/ gcd
<N
, D
>::value
;
41 static intmax_t const den
= abs
<D
>::value
/ gcd
<N
, D
>::value
;
45 /// rational arithmetic: addition
46 template <typename R1
, typename R2
>
51 R1::num
* R2::den
+ R2::num
* R1::den
57 /// rational arithmetic: subtraction
58 template <typename R1
, typename R2
>
63 R1::num
* R2::den
- R2::num
* R1::den
69 /// rational arithmetic: multiplication
70 template <typename R1
, typename R2
>
81 /// rational arithmetic: division
82 template <typename R1
, typename R2
>
93 /// Convenience SI units
95 typedef ratio
<1, 1000000000000000000> atto
;
96 typedef ratio
<1, 1000000000000000> femto
;
97 typedef ratio
<1, 1000000000000> pico
;
98 typedef ratio
<1, 1000000000> nano
;
99 typedef ratio
<1, 1000000> micro
;
100 typedef ratio
<1, 1000> milli
;
101 typedef ratio
<1, 100> centi
;
102 typedef ratio
<1, 10> deci
;
103 typedef ratio
< 10, 1> deca
;
104 typedef ratio
< 100, 1> hecto
;
105 typedef ratio
< 1000, 1> kilo
;
106 typedef ratio
< 1000000, 1> mega
;
107 typedef ratio
< 1000000000, 1> giga
;
108 typedef ratio
< 1000000000000, 1> tera
;
109 typedef ratio
< 1000000000000000, 1> peta
;
110 typedef ratio
<1000000000000000000, 1> exa
;
114 }} // namespace sol::math
118 #endif // sol_math_ratio_hpp_included