CNL  2.0.2 (development)
Compositional Numeric Library
extras.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 
11 
12 #if !defined(CNL_IMPL_SCALED_INTEGER_EXTRAS_H)
13 #define CNL_IMPL_SCALED_INTEGER_EXTRAS_H
14 
15 #include "../cmath/abs.h"
16 #include "../config.h"
17 #include "../num_traits/width.h"
18 #include "definition.h"
19 #include "to_chars.h"
20 
21 #include <cmath>
22 #if defined(CNL_IOSTREAMS_ENABLED)
23 #include <istream>
24 #include <ostream>
25 #endif
26 
28 namespace cnl {
29 
31  // cnl::abs
32 
41 
42  template<typename Rep, class Scale>
43  [[nodiscard]] constexpr auto abs(scaled_integer<Rep, Scale> const& x) noexcept -> decltype(-x)
44  {
45  return (x >= scaled_integer<Rep, Scale>{}) ? static_cast<decltype(-x)>(x) : -x;
46  }
47 
49  // cnl::floor
50 
51  template<class Rep, int Exponent, int Radix>
52  requires(Exponent < 0)
53  [[nodiscard]] constexpr auto floor(scaled_integer<Rep, power<Exponent, Radix>> const& x)
54  {
55  static_assert(
56  Radix == 2, "cnl::floor(scaled_integer<Rep, power<Exponent, Radix>>) not "
57  "implemented for Exponent<0 && Radix!=2");
58 
60  return _impl::from_rep<scaled_integer<Rep, power<0, Radix>>>(
61  _impl::to_rep(x) >> constant<-Exponent>());
63  }
64 
65  template<class Rep, int Exponent, int Radix>
66  requires(Exponent >= 0)
67  [[nodiscard]] constexpr auto floor(scaled_integer<Rep, power<Exponent, Radix>> const& x)
68  {
69  return x;
70  }
71 
73  // scaled_integer trig functions
74  //
75  // Placeholder implementations fall back on <cmath> functions which is slow
76  // due to conversion to and from floating-point types; also inconvenient as
77  // many <cmath> functions are not [[nodiscard]] constexpr.
78 
79  namespace _impl {
80  template<int NumBits>
81  struct float_of_size;
82 
83  template<int NumBits>
84  requires(NumBits <= sizeof(float) * CHAR_BIT) struct float_of_size<NumBits> {
85  using type = float;
86  };
87 
88  template<int NumBits>
89  requires(sizeof(float) * CHAR_BIT < NumBits && NumBits <= sizeof(double) * CHAR_BIT) struct float_of_size<NumBits> {
90  using type = double;
91  };
92 
93  template<int NumBits>
94  requires(sizeof(double) * CHAR_BIT < NumBits && NumBits <= sizeof(long double) * CHAR_BIT) struct float_of_size<NumBits> {
95  using type = long double;
96  };
97 
98  template<class T>
99  using float_of_same_size = typename float_of_size<_impl::width<T>>::type;
100 
101  template<
102  typename Rep, int Exponent, int Radix,
103  _impl::float_of_same_size<Rep> (*F)(_impl::float_of_same_size<Rep>)>
104  [[nodiscard]] constexpr auto
105  crib(scaled_integer<Rep, power<Exponent, Radix>> const& x) noexcept
106  {
107  using fp = _impl::float_of_same_size<Rep>;
108  return static_cast<scaled_integer<Rep, power<Exponent, Radix>>>(
109  F(static_cast<fp>(x)));
110  }
111  }
112 
113  template<typename Rep, int Exponent, int Radix>
114  [[nodiscard]] constexpr auto sin(scaled_integer<Rep, power<Exponent, Radix>> const& x) noexcept
115  {
116  return _impl::crib<Rep, Exponent, Radix, std::sin>(x);
117  }
118 
119  template<typename Rep, int Exponent, int Radix>
120  [[nodiscard]] constexpr auto cos(scaled_integer<Rep, power<Exponent, Radix>> const& x) noexcept
121  {
122  return _impl::crib<Rep, Exponent, Radix, std::cos>(x);
123  }
124 
125  template<typename Rep, int Exponent, int Radix>
126  [[nodiscard]] constexpr auto exp(scaled_integer<Rep, power<Exponent, Radix>> const& x) noexcept
127  {
128  return _impl::crib<Rep, Exponent, Radix, std::exp>(x);
129  }
130 
131  template<typename Rep, int Exponent, int Radix>
132  [[nodiscard]] constexpr auto pow(scaled_integer<Rep, power<Exponent, Radix>> const& x) noexcept
133  {
134  return _impl::crib<Rep, Exponent, Radix, std::pow>(x);
135  }
136 
138  // cnl::scaled_integer streaming - (placeholder implementation)
139 
140 #if defined(CNL_IOSTREAMS_ENABLED)
141  template<typename Rep, int Exponent, int Radix>
142  auto& operator<<(std::ostream& out, scaled_integer<Rep, power<Exponent, Radix>> const& fp)
143  {
144  return out << to_chars_static(fp).chars.data();
145  }
146 
147  template<typename Rep, int Exponent, int Radix>
148  auto& operator>>(std::istream& in, scaled_integer<Rep, power<Exponent, Radix>>& fp)
149  {
150  long double ld{};
151  in >> ld;
152  fp = ld;
153  return in;
154  }
155 #endif
156 }
157 
158 #endif // CNL_IMPL_SCALED_INTEGER_EXTRAS_H
std::floor
T floor(T... args)
std::cos
T cos(T... args)
std::ostream
STL class.
cnl
compositional numeric library
Definition: abort.h:15
std::sin
T sin(T... args)
std::exp
T exp(T... args)
cnl::abs
constexpr auto abs(scaled_integer< Rep, Scale > const &x) noexcept -> decltype(-x)
absolute value
Definition: extras.h:43
cnl::scaled_integer
_impl::wrapper< Rep, Scale > scaled_integer
literal real number approximation that uses fixed-point arithmetic
Definition: definition.h:52
std::istream
STL class.
std::pow
T pow(T... args)