-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpe_float
More file actions
230 lines (192 loc) · 5.58 KB
/
pe_float
File metadata and controls
230 lines (192 loc) · 5.58 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef PE_FLOAT128_
#define PE_FLOAT128_
#include "pe_base"
#include "pe_int"
namespace pe {
template <typename T>
SL int IsNAN(T v) {
return 0;
}
} // namespace pe
#if PE_HAS_FLOAT128
namespace pe {
namespace internal {
SL std::string ToStringFloat128(float128 f, const char* format_string,
int dig = 20) {
char buff[256];
const int buff_size = sizeof(buff);
int n = quadmath_snprintf(buff, buff_size, format_string, dig, f);
if (n < buff_size) {
return buff;
}
n = quadmath_snprintf(NULL, 0, format_string, dig, f);
if (n <= -1) {
return "";
}
char* str = static_cast<char*>(malloc(n + 1));
std::string result;
if (str) {
quadmath_snprintf(str, n + 1, format_string, dig, f);
result = str;
}
free(str);
return result;
}
} // namespace internal
SL std::string ToString(float128 f, int dig = 20) {
return internal::ToStringFloat128(f, "%#.*Qe", dig);
}
SL std::string ToStringF(float128 f, int dig = 20) {
return internal::ToStringFloat128(f, "%#.*Qf", dig);
}
SL std::ostream& operator<<(std::ostream& o, float128 f) {
return o << ToString(f, 20);
}
SL int IsNAN(float128 v) { return isnanq(v); }
SL float128 Abs(float128 f) { return fabsq(f); }
SL float128 FAbs(float128 f) { return fabsq(f); }
SL float128 Ceil(float128 f) { return ceilq(f); }
SL float128 Floor(float128 f) { return floorq(f); }
SL float128 Trunc(float128 f) { return truncq(f); }
SL float128 Power(float128 f, int p) {
return powq(f, static_cast<float128>(p));
}
SL float128 Sqrt(float128 f) { return sqrtq(f); }
SL float128 Cos(float128 f) { return cosq(f); }
SL float128 Sin(float128 f) { return sinq(f); }
SL float128 Exp(float128 f) { return expq(f); }
SL float128 Log(float128 f) { return logq(f); }
SL float128 Log10(float128 f) { return log10q(f); }
} // namespace pe
#endif
namespace pe {
namespace internal {
template <typename T>
SL std::string ToStringFloat(T f, const char* format_string, int dig = 20) {
char buff[256];
const int buff_size = sizeof(buff);
int n = snprintf(buff, buff_size, format_string, dig, f);
if (n < buff_size) {
return buff;
}
n = snprintf(NULL, 0, format_string, dig, f);
if (n <= -1) {
return "";
}
char* str = static_cast<char*>(malloc(n + 1));
std::string result;
if (str) {
snprintf(str, n + 1, format_string, dig, f);
result = str;
}
free(str);
return result;
}
} // namespace internal
SL std::string ToString(float f, int dig = 20) {
return internal::ToStringFloat<float>(f, "%#.*e", dig);
}
SL std::string ToString(double f, int dig = 20) {
return internal::ToStringFloat<double>(f, "%#.*e", dig);
}
SL std::string ToString(long double f, int dig = 20) {
return internal::ToStringFloat<long double>(f, "%#.*Le", dig);
}
SL std::string ToStringF(float f, int dig = 20) {
return internal::ToStringFloat<float>(f, "%#.*f", dig);
}
SL std::string ToStringF(double f, int dig = 20) {
return internal::ToStringFloat<double>(f, "%#.*f", dig);
}
SL std::string ToStringF(long double f, int dig = 20) {
return internal::ToStringFloat<long double>(f, "%#.*Lf", dig);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(int)
IsNAN(T v) {
return std::isnan(v);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Abs(T f) {
return std::fabs(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) FAbs(T f) {
return std::fabs(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Floor(T f) {
return std::floor(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Ceil(T f) {
return std::ceil(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Trunc(T f) {
return std::trunc(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T)
Power(T f, int p) {
return std::pow(f, static_cast<T>(p));
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Sqrt(T f) {
return std::sqrt(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Cos(T f) {
return std::cos(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Sin(T f) {
return std::sin(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Exp(T f) {
return std::exp(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Log(T f) {
return std::log(f);
}
template <typename T>
SL REQUIRES((is_one_of_v<T, float, double, long double>)) RETURN(T) Log10(T f) {
return std::log10(f);
}
} // namespace pe
#if PE_HAS_CPP20
namespace pe {
template <class T>
concept PeFloatUtil = requires(T v) {
requires PeComparable<T>;
Abs(v);
FAbs(v);
Floor(v);
Ceil(v);
Trunc(v);
Power(v, 0);
Sqrt(v);
ToString(v);
to_string(v);
std::cout << v;
};
template <class T>
concept PeRichFloatUtil = requires(T v) {
requires PeFloatUtil<T>;
Cos(v);
Sin(v);
Exp(v);
Log(v);
Log10(v);
};
static_assert(PeRichFloatUtil<float>);
static_assert(PeRichFloatUtil<double>);
static_assert(PeRichFloatUtil<long double>);
#if PE_HAS_FLOAT128
static_assert(PeRichFloatUtil<float128>);
#endif
} // namespace pe
#endif
#endif