-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_specializations.hpp
177 lines (164 loc) · 4.38 KB
/
std_specializations.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef TOKOX_FRACTIONS_SPECIALIZATIONS
#define TOKOX_FRACTIONS_SPECIALIZATIONS
#include <functional>
#include <cstddef>
#include <limits>
#include <cmath>
#include <ostream>
#include <istream>
#include <stdexcept>
#include "fractions.hpp"
template <typename T>
void swap (tokox::Fraction<T>& one, tokox::Fraction<T>& two)
{
one.swap(two);
}
template <typename T>
struct std::hash<tokox::Fraction<T>>
{
std::size_t operator() (const tokox::Fraction<T>& f) const
{
return f.hash();
}
};
template <typename T>
std::ostream& operator<< (std::ostream& o, const tokox::Fraction<T>& f)
{
return o << f.numerator() << '/' << f.denominator();
}
namespace tokox
{
template<typename T = void>
class FractionInputError : public std::exception
{
public:
FractionInputError(const std::string& where):
message("wrong input for tokox::Fraction"
+ (typeid(T) == typeid(void) ? "" : "<" + get_typename<T>() + ">")
+ " in " + where)
{}
const char* what() const noexcept override
{
return message.c_str();
}
private:
std::string message;
};
}
template <typename T>
std::istream& operator>> (std::istream& i, tokox::Fraction<T>& f)
{
T v;
i >> v;
if (i.fail())
{
throw tokox::FractionInputError<T>("std::istream::operator>>");
}
f.numerator(v);
char c;
i >> c;
if (c != '/' || i.fail())
{
throw tokox::FractionInputError<T>("std::istream::operator>>");
}
i >> v;
if (i.fail())
{
throw tokox::FractionInputError<T>("std::istream::operator>>");
}
f.denominator(v);
return i;
}
template <typename T>
class std::numeric_limits<tokox::Fraction<T>>
{
public:
static constexpr bool is_specialized = true;
static constexpr bool is_signed = true;
static constexpr bool is_integer = false;
static constexpr bool is_exact = true;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
static constexpr std::float_denorm_style has_denorm = std::denorm_absent;
static constexpr bool has_denorm_loss = false;
static constexpr std::float_round_style round_style = std::round_indeterminate;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = std::numeric_limits<T>::is_bounded;
static constexpr bool is_modulo = false;
static constexpr int digits = std::numeric_limits<T>::digits * 2;
static constexpr int digits10 = std::numeric_limits<T>::digits10 * 2;
static constexpr int max_digits10 = std::numeric_limits<T>::max_digits10 * 2;
static constexpr int radix = std::numeric_limits<T>::radix;
static constexpr int min_exponent = is_bounded ? std::log2(std::numeric_limits<T>::max()) / std::log2(std::numeric_limits<T>::radix) + 1 : 0;
static constexpr int min_exponent10 = is_bounded ? std::log10(std::numeric_limits<T>::max()) + 1 : 0;
static constexpr int max_exponent = is_bounded ? std::log2(std::numeric_limits<T>::max()) / std::log2(std::numeric_limits<T>::radix) + 1 : 0;
static constexpr int max_exponent10 = is_bounded ? std::log10(std::numeric_limits<T>::max()) + 1 : 0;
static constexpr bool traps = true;
static constexpr bool tineness_before = false;
static constexpr tokox::Fraction<T> min () noexcept
{
if constexpr (is_bounded)
{
return tokox::Fraction<T>(1, std::numeric_limits<T>::max());
}
else
{
return tokox::Fraction<T>();
}
}
static constexpr tokox::Fraction<T> lowest () noexcept
{
if constexpr (is_bounded)
{
return tokox::Fraction<T>(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
}
else
{
return tokox::Fraction<T>();
}
}
static constexpr tokox::Fraction<T> max () noexcept
{
if constexpr (is_bounded)
{
return tokox::Fraction<T>(std::numeric_limits<T>::max(), 1);
}
else
{
return tokox::Fraction<T>();
}
}
static constexpr tokox::Fraction<T> epsilon () noexcept
{
if constexpr (is_bounded)
{
return tokox::Fraction<T>(1, std::numeric_limits<T>::max() - 1);
}
else
{
return tokox::Fraction<T>();
}
}
static constexpr tokox::Fraction<T> round_error () noexcept
{
return tokox::Fraction<T>(0);
}
static constexpr tokox::Fraction<T> infinity () noexcept
{
return tokox::Fraction<T>();
}
static constexpr tokox::Fraction<T> quiet_NaN () noexcept
{
return tokox::Fraction<T>();
}
static constexpr tokox::Fraction<T> signaling_NaN () noexcept
{
return tokox::Fraction<T>();
}
static constexpr tokox::Fraction<T> denorm_min () noexcept
{
return tokox::Fraction<T>();
}
};
#endif