Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_math_exponential.d

raw
1 @safe pure nothrow @nogc unittest
2 {
3 import std.math.exponential;
4
5 import std.math.operations : feqrel;
6
7 assert(pow(2.0, 5) == 32.0);
8 assert(pow(1.5, 9).feqrel(38.4433) > 16);
9 assert(pow(real.nan, 2) is real.nan);
10 assert(pow(real.infinity, 2) == real.infinity);
11 }
12
13 @safe pure nothrow @nogc unittest
14 {
15 import std.math.exponential;
16
17 assert(pow(2, 3) == 8);
18 assert(pow(3, 2) == 9);
19
20 assert(pow(2, 10) == 1_024);
21 assert(pow(2, 20) == 1_048_576);
22 assert(pow(2, 30) == 1_073_741_824);
23
24 assert(pow(0, 0) == 1);
25
26 assert(pow(1, -5) == 1);
27 assert(pow(1, -6) == 1);
28 assert(pow(-1, -5) == -1);
29 assert(pow(-1, -6) == 1);
30
31 assert(pow(-2, 5) == -32);
32 assert(pow(-2, -5) == 0);
33 assert(pow(cast(double) -2, -5) == -0.03125);
34 }
35
36 @safe pure nothrow @nogc unittest
37 {
38 import std.math.exponential;
39
40 assert(pow(2, 5.0) == 32.0);
41 assert(pow(7, 3.0) == 343.0);
42 assert(pow(2, real.nan) is real.nan);
43 assert(pow(2, real.infinity) == real.infinity);
44 }
45
46 @safe pure nothrow @nogc unittest
47 {
48 import std.math.exponential;
49
50 import std.math.operations : isClose;
51
52 assert(isClose(pow(2.0, 3.0), 8.0));
53 assert(isClose(pow(1.5, 10.0), 57.6650390625));
54
55 // square root of 9
56 assert(isClose(pow(9.0, 0.5), 3.0));
57 // 10th root of 1024
58 assert(isClose(pow(1024.0, 0.1), 2.0));
59
60 assert(isClose(pow(-4.0, 3.0), -64.0));
61
62 // reciprocal of 4 ^^ 2
63 assert(isClose(pow(4.0, -2.0), 0.0625));
64 // reciprocal of (-2) ^^ 3
65 assert(isClose(pow(-2.0, -3.0), -0.125));
66
67 assert(isClose(pow(-2.5, 3.0), -15.625));
68 // reciprocal of 2.5 ^^ 3
69 assert(isClose(pow(2.5, -3.0), 0.064));
70 // reciprocal of (-2.5) ^^ 3
71 assert(isClose(pow(-2.5, -3.0), -0.064));
72
73 // reciprocal of square root of 4
74 assert(isClose(pow(4.0, -0.5), 0.5));
75
76 // per definition
77 assert(isClose(pow(0.0, 0.0), 1.0));
78 }
79
80 @safe pure nothrow @nogc unittest
81 {
82 import std.math.exponential;
83
84 import std.math.operations : isClose;
85
86 // the result is a complex number
87 // which cannot be represented as floating point number
88 import std.math.traits : isNaN;
89 assert(isNaN(pow(-2.5, -1.5)));
90
91 // use the ^^-operator of std.complex instead
92 import std.complex : complex;
93 auto c1 = complex(-2.5, 0.0);
94 auto c2 = complex(-1.5, 0.0);
95 auto result = c1 ^^ c2;
96 // exact result apparently depends on `real` precision => increased tolerance
97 assert(isClose(result.re, -4.64705438e-17, 2e-4));
98 assert(isClose(result.im, 2.52982e-1, 2e-4));
99 }
100
101 @safe pure nothrow @nogc unittest
102 {
103 import std.math.exponential;
104
105 assert(powmod(1U, 10U, 3U) == 1);
106 assert(powmod(3U, 2U, 6U) == 3);
107 assert(powmod(5U, 5U, 15U) == 5);
108 assert(powmod(2U, 3U, 5U) == 3);
109 assert(powmod(2U, 4U, 5U) == 1);
110 assert(powmod(2U, 5U, 5U) == 2);
111 }
112
113 @safe unittest
114 {
115 import std.math.exponential;
116
117 import std.math.operations : feqrel;
118 import std.math.constants : E;
119
120 assert(exp(0.0) == 1.0);
121 assert(exp(3.0).feqrel(E * E * E) > 16);
122 }
123
124 @safe unittest
125 {
126 import std.math.exponential;
127
128 import std.math.traits : isIdentical;
129 import std.math.operations : feqrel;
130
131 assert(isIdentical(expm1(0.0), 0.0));
132 assert(expm1(1.0).feqrel(1.71828) > 16);
133 assert(expm1(2.0).feqrel(6.3890) > 16);
134 }
135
136 @safe unittest
137 {
138 import std.math.exponential;
139
140 import std.math.traits : isIdentical;
141 import std.math.operations : feqrel;
142
143 assert(isIdentical(exp2(0.0), 1.0));
144 assert(exp2(2.0).feqrel(4.0) > 16);
145 assert(exp2(8.0).feqrel(256.0) > 16);
146 }
147
148 @safe unittest
149 {
150 import std.math.exponential;
151
152 import std.math.operations : isClose;
153
154 int exp;
155 real mantissa = frexp(123.456L, exp);
156
157 assert(isClose(mantissa * pow(2.0L, cast(real) exp), 123.456L));
158
159 assert(frexp(-real.nan, exp) && exp == int.min);
160 assert(frexp(real.nan, exp) && exp == int.min);
161 assert(frexp(-real.infinity, exp) == -real.infinity && exp == int.min);
162 assert(frexp(real.infinity, exp) == real.infinity && exp == int.max);
163 assert(frexp(-0.0, exp) == -0.0 && exp == 0);
164 assert(frexp(0.0, exp) == 0.0 && exp == 0);
165 }
166
167 @safe pure unittest
168 {
169 import std.math.exponential;
170
171 assert(ilogb(1) == 0);
172 assert(ilogb(3) == 1);
173 assert(ilogb(3.0) == 1);
174 assert(ilogb(100_000_000) == 26);
175
176 assert(ilogb(0) == FP_ILOGB0);
177 assert(ilogb(0.0) == FP_ILOGB0);
178 assert(ilogb(double.nan) == FP_ILOGBNAN);
179 assert(ilogb(double.infinity) == int.max);
180 }
181
182 @safe pure unittest
183 {
184 import std.math.exponential;
185
186 assert(ilogb(0) == FP_ILOGB0);
187 assert(ilogb(0.0) == FP_ILOGB0);
188 assert(ilogb(double.nan) == FP_ILOGBNAN);
189 }
190
191 @nogc @safe pure nothrow unittest
192 {
193 import std.math.exponential;
194
195 import std.meta : AliasSeq;
196 static foreach (T; AliasSeq!(float, double, real))
197 {{
198 T r;
199
200 r = ldexp(3.0L, 3);
201 assert(r == 24);
202
203 r = ldexp(cast(T) 3.0, cast(int) 3);
204 assert(r == 24);
205
206 T n = 3.0;
207 int exp = 3;
208 r = ldexp(n, exp);
209 assert(r == 24);
210 }}
211 }
212
213 @safe pure nothrow @nogc unittest
214 {
215 import std.math.exponential;
216
217 import std.math.operations : feqrel;
218 import std.math.constants : E;
219
220 assert(feqrel(log(E), 1) >= real.mant_dig - 1);
221 }
222
223 @safe pure nothrow @nogc unittest
224 {
225 import std.math.exponential;
226
227 import std.math.algebraic : fabs;
228
229 assert(fabs(log10(1000.0L) - 3) < .000001);
230 }
231
232 @safe pure unittest
233 {
234 import std.math.exponential;
235
236 import std.math.traits : isIdentical, isNaN;
237 import std.math.operations : feqrel;
238
239 assert(isIdentical(log1p(0.0), 0.0));
240 assert(log1p(1.0).feqrel(0.69314) > 16);
241
242 assert(log1p(-1.0) == -real.infinity);
243 assert(isNaN(log1p(-2.0)));
244 assert(log1p(real.nan) is real.nan);
245 assert(log1p(-real.nan) is -real.nan);
246 assert(log1p(real.infinity) == real.infinity);
247 }
248
249 @safe unittest
250 {
251 import std.math.exponential;
252
253 import std.math.operations : isClose;
254
255 assert(isClose(log2(1024.0L), 10));
256 }
257
258 @safe @nogc nothrow unittest
259 {
260 import std.math.exponential;
261
262 assert(logb(1.0) == 0);
263 assert(logb(100.0) == 6);
264
265 assert(logb(0.0) == -real.infinity);
266 assert(logb(real.infinity) == real.infinity);
267 assert(logb(-real.infinity) == real.infinity);
268 }
269
270 @safe pure nothrow @nogc unittest
271 {
272 import std.math.exponential;
273
274 assert(scalbn(0x1.2345678abcdefp0L, 999) == 0x1.2345678abcdefp999L);
275 assert(scalbn(-real.infinity, 5) == -real.infinity);
276 assert(scalbn(2.0,10) == 2048.0);
277 assert(scalbn(2048.0f,-10) == 2.0f);
278 }
279