CNL  2.0.2 (development)
Compositional Numeric Library
elastic_scaled_integer.h
Go to the documentation of this file.
1 
2 // Copyright John McFarlane 2015 - 2017.
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_ELASTIC_SCALED_INTEGER_H)
11 #define CNL_ELASTIC_SCALED_INTEGER_H
12 
13 #include "_impl/charconv/descale.h"
14 #include "_impl/numbers/adopt_signedness.h"
15 #include "_impl/scaled/is_scaled_tag.h"
16 #include "_impl/scaled/power.h"
17 #include "elastic_integer.h"
18 #include "scaled_integer.h"
19 
20 #include <algorithm>
21 #include <limits>
22 #include <type_traits>
23 
25 namespace cnl {
28  // cnl-specific definitions
29 
38 
39  template<int Digits, scaled_tag Scale = power<>, class Narrowest = signed>
42 
45  // cnl::make_elastic_scaled_integer
46 
63 
64  template<typename Narrowest = int, CNL_IMPL_CONSTANT_VALUE_TYPE Value = 0>
65  [[nodiscard]] constexpr auto make_elastic_scaled_integer(constant<Value>)
67  std::max(digits_v<constant<Value>> - trailing_bits(Value), 1),
68  power<trailing_bits(Value)>, Narrowest>
69  {
70  return Value;
71  }
72 
75  // cnl::make_elastic_scaled_integer
76 
92  template<typename Narrowest = void, typename Integral = int>
93  [[nodiscard]] constexpr auto make_elastic_scaled_integer(Integral const& value)
96  typename std::conditional<
98  _impl::adopt_signedness_t<int, Integral>, Narrowest>::type>
99  {
100  return {value};
101  }
102 
103  template<typename Narrowest = void, typename Rep = int, scaled_tag Scale = power<>>
104  [[nodiscard]] constexpr auto make_elastic_scaled_integer(scaled_integer<Rep, Scale> const& value)
107  typename std::conditional<
109  _impl::adopt_signedness_t<int, Rep>, Narrowest>::type>
110  {
111  return {value};
112  }
113 
114  namespace _impl {
115  template<integer auto ParsedSignificand, integer auto ParsedExponent, int ParsedRadix, int UdlRadix>
116  [[nodiscard]] constexpr auto make_from_udl()
117  {
118  constexpr auto descaled{
119  descale<decltype(ParsedSignificand), UdlRadix, true>(
120  ParsedSignificand,
121  power<ParsedExponent, ParsedRadix>{})};
122  constexpr auto rep{make_elastic_integer(constant<descaled.significand>{})};
123  return from_rep<scaled_integer<
124  decltype(rep),
125  power<descaled.exponent, descaled.radix>>>(rep);
126  }
127  }
128 
130  namespace literals {
133  // cnl::literals::operator "" _cnl
134 
148 
149  template<char... Chars>
150  [[nodiscard]] constexpr auto operator"" _cnl()
151  {
152  constexpr auto parsed{_impl::parse_real<intmax_t, Chars...>()};
153  return _impl::make_from_udl<parsed.significand, parsed.exponent, parsed.radix, parsed.radix>();
154  }
155 
166 
167  template<char... Chars>
168  [[nodiscard]] constexpr auto operator"" _cnl2()
169  {
170  constexpr auto parsed{_impl::parse_real<intmax_t, Chars...>()};
171  return _impl::make_from_udl<parsed.significand, parsed.exponent, parsed.radix, 2>();
172  }
173  }
174 }
175 
176 #endif // CNL_ELASTIC_SCALED_INTEGER_H
std::is_same
cnl::digits_v
constexpr int digits_v
provide number of binary digits of the given type
Definition: digits.h:31
scaled_integer.h
all definitions related to the cnl::scaled_integer type
cnl::elastic_scaled_integer
scaled_integer< elastic_integer< Digits, Narrowest >, Scale > elastic_scaled_integer
literal real number approximation that uses fixed-point arithmetic and auto-widens to avoid overflow
Definition: elastic_scaled_integer.h:41
cnl
compositional numeric library
Definition: abort.h:15
cnl::scaled_integer
_impl::wrapper< Rep, Scale > scaled_integer
literal real number approximation that uses fixed-point arithmetic
Definition: definition.h:52
cnl::constant
type whose value is also its non-type template parameter
Definition: constant.h:44
cnl::make_elastic_scaled_integer
constexpr auto make_elastic_scaled_integer(constant< Value >) -> elastic_scaled_integer< std::max(digits_v< constant< Value >> - trailing_bits(Value), 1), power< trailing_bits(Value)>, Narrowest >
generate an elastic_scaled_integer object of given value
Definition: elastic_scaled_integer.h:65
std::conditional
std::max
T max(T... args)
elastic_integer.h
essential definitions related to the cnl::elastic_integer type
std::numeric_limits
cnl::power
tag representing the scaling of an integer by a fixed factor
Definition: declaration.h:13