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
231
232
233
234
235
236
237
238
239
240
241
242
243
|
#pragma GCC diagnostic ignored "-Wformat"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
namespace almostEqualDetail {
union fasi {
int i;
float f;
};
union dasi {
long long i;
double f;
};
} // namespace almostEqualDetail
inline int intDiff(float a, float b) {
using namespace almostEqualDetail;
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
fasi fa;
fa.f = a;
// Make aInt lexicographically ordered as a twos-complement int
if (fa.i < 0)
fa.i = 0x80000000 - fa.i;
// Make bInt lexicographically ordered as a twos-complement int
fasi fb;
fb.f = b;
if (fb.i < 0)
fb.i = 0x80000000 - fb.i;
return std::abs(fa.i - fb.i);
}
inline long long intDiff(double a, double b) {
using namespace almostEqualDetail;
dasi fa;
fa.f = a;
// Make aInt lexicographically ordered as a twos-complement int
if (fa.i < 0)
fa.i = 0x8000000000000000LL - fa.i;
// Make bInt lexicographically ordered as a twos-complement int
dasi fb;
fb.f = b;
if (fb.i < 0)
fb.i = 0x8000000000000000LL - fb.i;
return std::abs(fa.i - fb.i);
}
template <typename T>
inline bool almostEqual(T a, T b, int maxUlps) {
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
// assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
return intDiff(a, b) <= maxUlps;
}
namespace {
template <typename T>
inline T eta(T x, T y, T z) {
T t(z / std::sqrt(x * x + y * y));
return std::log(t + std::sqrt(t * t + T(1)));
}
template <typename T>
inline T eta2(T x, T y, T z) {
T t = (z * z) / (x * x + y * y);
return copysign(std::log(std::sqrt(t) + std::sqrt(t + T(1))), z);
}
inline float eta3(float x, float y, float z) {
float t(z / std::sqrt(x * x + y * y));
return ::asinhf(t);
}
inline double eta3(double x, double y, double z) {
double t(z / std::sqrt(x * x + y * y));
return ::asinh(t);
}
void look(float x) {
int e;
float r = ::frexpf(x, &e);
std::cout << x << " exp " << e << " res " << r << std::endl;
union {
float val;
int bin;
} f;
f.val = x;
printf("%e %a %x\n", f.val, f.val, f.bin);
// printf("%e %x\n", f.val, f.bin);
int log_2 = ((f.bin >> 23) & 255) - 127; //exponent
f.bin &= 0x7FFFFF; //mantissa (aka significand)
std::cout << "exp " << log_2 << " mant in binary " << std::hex << f.bin << " mant as float " << std::dec
<< (f.bin | 0x800000) * ::pow(2., -23) << std::endl
<< std::endl;
}
void look(double x) {
// int e;
// float r = ::frexpf(x,&e);
// std::cout << x << " exp " << e << " res " << r << std::endl;
union {
double val;
long long bin;
} f;
f.val = x;
printf("%e %a %x\n", f.val, f.val, f.bin);
// printf("%e %x\n", f.val, f.bin);
}
} // namespace
void peta() {
std::cout << "T t(z/std::sqrt(x*x+y*y)); return std::log(t+std::sqrt(t*t+T(1)));" << std::endl;
{
float xn = 122.436f, yn = 10.7118f, zn = -1115.f;
float etan = eta(xn, yn, zn);
std::cout << etan << std::endl;
look(etan);
std::cout << -etan << std::endl;
look(-etan);
float xp = 122.436f, yp = 10.7118f, zp = 1115.f;
float etap = eta(xp, yp, zp);
std::cout << etap << std::endl;
look(etap);
std::cout << intDiff(etap, -etan) << std::endl << std::endl;
}
{
double xn = 122.436, yn = 10.7118, zn = -1115.;
double etan = eta(xn, yn, zn);
std::cout << etan << std::endl;
look(etan);
std::cout << -etan << std::endl;
look(-etan);
double xp = 122.436, yp = 10.7118, zp = 1115.;
double etap = eta(xp, yp, zp);
std::cout << etap << std::endl;
look(etap);
std::cout << intDiff(etap, -etan) << std::endl << std::endl;
}
}
void peta2() {
std::cout << "T t = (z*z)/(x*x+y*y); return copysign(std::log(std::sqrt(t)+std::sqrt(t+T(1))), z);" << std::endl;
{
float xn = 122.436f, yn = 10.7118f, zn = -1115.f;
float etan = eta2(xn, yn, zn);
std::cout << etan << std::endl;
look(etan);
std::cout << -etan << std::endl;
look(-etan);
float xp = 122.436f, yp = 10.7118f, zp = 1115.f;
float etap = eta2(xp, yp, zp);
std::cout << etap << std::endl;
look(etap);
std::cout << intDiff(etap, -etan) << std::endl << std::endl;
}
{
double xn = 122.436, yn = 10.7118, zn = -1115.;
double etan = eta2(xn, yn, zn);
std::cout << etan << std::endl;
look(etan);
std::cout << -etan << std::endl;
look(-etan);
double xp = 122.436, yp = 10.7118, zp = 1115.;
double etap = eta2(xp, yp, zp);
std::cout << etap << std::endl;
look(etap);
std::cout << intDiff(etap, -etan) << std::endl << std::endl;
}
}
void peta3() {
std::cout << "t(z/std::sqrt(x*x+y*y)); return ::asinh(t);" << std::endl;
{
float xn = 122.436f, yn = 10.7118f, zn = -1115.f;
float etan = eta3(xn, yn, zn);
std::cout << etan << std::endl;
look(etan);
std::cout << -etan << std::endl;
look(-etan);
float xp = 122.436f, yp = 10.7118f, zp = 1115.f;
float etap = eta3(xp, yp, zp);
std::cout << etap << std::endl;
look(etap);
std::cout << intDiff(etap, -etan) << std::endl << std::endl;
}
{
double xn = 122.436, yn = 10.7118, zn = -1115.;
double etan = eta3(xn, yn, zn);
std::cout << etan << std::endl;
look(etan);
std::cout << -etan << std::endl;
look(-etan);
double xp = 122.436, yp = 10.7118, zp = 1115.;
double etap = eta3(xp, yp, zp);
std::cout << etap << std::endl;
look(etap);
std::cout << intDiff(etap, -etan) << std::endl << std::endl;
}
}
int main() {
peta();
peta2();
peta3();
return 0;
}
|