CNL  2.0.2 (development)
Compositional Numeric Library
named.h
Go to the documentation of this file.
1 
2 // Copyright John McFarlane 2015 - 2016.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file ../LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
9 
10 #if !defined(CNL_IMPL_SCALED_INTEGER_NAMED_H)
11 #define CNL_IMPL_SCALED_INTEGER_NAMED_H
12 
13 #include "../num_traits/rep_of.h"
14 #include "../num_traits/tag_of.h"
15 #include "../scaled/power.h"
16 
17 #include <algorithm>
18 
20 namespace cnl {
21 
23  // cnl::make_scaled_integer<Value>
24 
31 
32  template<typename Value>
33  [[nodiscard]] constexpr auto make_scaled_integer(Value const& value)
34  {
35  return _impl::from_value<scaled_integer<>, Value>(value);
36  }
37 
38  template<fixed_point Numerator, fixed_point Denominator>
39  struct fraction;
40 
41  template<class Dividend, class Divisor>
42  [[nodiscard]] constexpr auto make_scaled_integer(fraction<Dividend, Divisor> const& f)
43  {
44  using natural_result = _impl::op_result<_impl::divide_op, Dividend, Divisor>;
45 
46  constexpr int integer_digits = _impl::integer_digits<Dividend> + _impl::fractional_digits<Divisor>;
47  constexpr int fractional_digits = _impl::fractional_digits<Dividend> + _impl::integer_digits<Divisor>;
48 
49  constexpr auto necessary_digits = integer_digits + fractional_digits;
50  constexpr auto natural_digits = digits_v<natural_result>;
51  constexpr auto result_digits = std::max(necessary_digits, natural_digits);
52 
53  constexpr int rep_exponent = -fractional_digits;
54  using scale = power<rep_exponent>;
55 
56  using rep_type = set_digits_t<natural_result, result_digits>;
57  using rep = decltype(_impl::not_scaled_integer(std::declval<rep_type>()));
58 
59  return _impl::from_rep<scaled_integer<rep, scale>>(
60  convert<scale, rep, power<>>{}(f));
61  }
62 
74 
75  template<class Dividend, class Divisor>
76  [[nodiscard]] constexpr auto quotient(Dividend const& dividend, Divisor const& divisor)
77  {
78  return make_scaled_integer(make_fraction(dividend, divisor));
79  }
80 }
81 
82 #endif // CNL_IMPL_SCALED_INTEGER_NAMED_H
cnl::make_scaled_integer
constexpr auto make_scaled_integer(Value const &value)
makes a scaled_integer object from a given value
Definition: named.h:33
cnl
compositional numeric library
Definition: abort.h:15
cnl::quotient
constexpr auto quotient(Dividend const &dividend, Divisor const &divisor)
calculates the quotient of two scaled_integer values
Definition: named.h:76
std::max
T max(T... args)