Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_math_operations.d

raw
1 @safe @nogc pure nothrow unittest
2 {
3 import std.math.operations;
4
5 import std.math.traits : isNaN;
6
7 real a = NaN(1_000_000);
8 assert(isNaN(a));
9 assert(getNaNPayload(a) == 1_000_000);
10 }
11
12 @safe @nogc pure nothrow unittest
13 {
14 import std.math.operations;
15
16 import std.math.traits : isNaN;
17
18 real a = NaN(1_000_000);
19 assert(isNaN(a));
20 assert(getNaNPayload(a) == 1_000_000);
21 }
22
23 @safe @nogc pure nothrow unittest
24 {
25 import std.math.operations;
26
27 assert(nextUp(1.0 - 1.0e-6).feqrel(0.999999) > 16);
28 assert(nextUp(1.0 - real.epsilon).feqrel(1.0) > 16);
29 }
30
31 @safe pure nothrow @nogc unittest
32 {
33 import std.math.operations;
34
35 assert( nextDown(1.0 + real.epsilon) == 1.0);
36 }
37
38 @safe pure nothrow @nogc unittest
39 {
40 import std.math.operations;
41
42 import std.math.traits : isNaN;
43
44 float a = 1;
45 assert(is(typeof(nextafter(a, a)) == float));
46 assert(nextafter(a, a.infinity) > a);
47 assert(isNaN(nextafter(a, a.nan)));
48 assert(isNaN(nextafter(a.nan, a)));
49
50 double b = 2;
51 assert(is(typeof(nextafter(b, b)) == double));
52 assert(nextafter(b, b.infinity) > b);
53 assert(isNaN(nextafter(b, b.nan)));
54 assert(isNaN(nextafter(b.nan, b)));
55
56 real c = 3;
57 assert(is(typeof(nextafter(c, c)) == real));
58 assert(nextafter(c, c.infinity) > c);
59 assert(isNaN(nextafter(c, c.nan)));
60 assert(isNaN(nextafter(c.nan, c)));
61 }
62
63 @safe pure nothrow @nogc unittest
64 {
65 import std.math.operations;
66
67 import std.math.traits : isNaN;
68
69 assert(fdim(2.0, 0.0) == 2.0);
70 assert(fdim(-2.0, 0.0) == 0.0);
71 assert(fdim(real.infinity, 2.0) == real.infinity);
72 assert(isNaN(fdim(real.nan, 2.0)));
73 assert(isNaN(fdim(2.0, real.nan)));
74 assert(isNaN(fdim(real.nan, real.nan)));
75 }
76
77 @safe pure nothrow @nogc unittest
78 {
79 import std.math.operations;
80
81 import std.meta : AliasSeq;
82 static foreach (F; AliasSeq!(float, double, real))
83 {
84 assert(fmax(F(0.0), F(2.0)) == 2.0);
85 assert(fmax(F(-2.0), 0.0) == F(0.0));
86 assert(fmax(F.infinity, F(2.0)) == F.infinity);
87 assert(fmax(F.nan, F(2.0)) == F(2.0));
88 assert(fmax(F(2.0), F.nan) == F(2.0));
89 }
90 }
91
92 @safe pure nothrow @nogc unittest
93 {
94 import std.math.operations;
95
96 import std.meta : AliasSeq;
97 static foreach (F; AliasSeq!(float, double, real))
98 {
99 assert(fmin(F(0.0), F(2.0)) == 0.0);
100 assert(fmin(F(-2.0), F(0.0)) == -2.0);
101 assert(fmin(F.infinity, F(2.0)) == 2.0);
102 assert(fmin(F.nan, F(2.0)) == 2.0);
103 assert(fmin(F(2.0), F.nan) == 2.0);
104 }
105 }
106
107 @safe pure nothrow @nogc unittest
108 {
109 import std.math.operations;
110
111 assert(fma(0.0, 2.0, 2.0) == 2.0);
112 assert(fma(2.0, 2.0, 2.0) == 6.0);
113 assert(fma(real.infinity, 2.0, 2.0) == real.infinity);
114 assert(fma(real.nan, 2.0, 2.0) is real.nan);
115 assert(fma(2.0, 2.0, real.nan) is real.nan);
116 }
117
118 @safe pure unittest
119 {
120 import std.math.operations;
121
122 assert(feqrel(2.0, 2.0) == 53);
123 assert(feqrel(2.0f, 2.0f) == 24);
124 assert(feqrel(2.0, double.nan) == 0);
125
126 // Test that numbers are within n digits of each
127 // other by testing if feqrel > n * log2(10)
128
129 // five digits
130 assert(feqrel(2.0, 2.00001) > 16);
131 // ten digits
132 assert(feqrel(2.0, 2.00000000001) > 33);
133 }
134
135 @safe pure nothrow @nogc unittest
136 {
137 import std.math.operations;
138
139 assert(isClose(1.0,0.999_999_999));
140 assert(isClose(0.001, 0.000_999_999_999));
141 assert(isClose(1_000_000_000.0,999_999_999.0));
142
143 assert(isClose(17.123_456_789, 17.123_456_78));
144 assert(!isClose(17.123_456_789, 17.123_45));
145
146 // use explicit 3rd parameter for less (or more) accuracy
147 assert(isClose(17.123_456_789, 17.123_45, 1e-6));
148 assert(!isClose(17.123_456_789, 17.123_45, 1e-7));
149
150 // use 4th parameter when comparing close to zero
151 assert(!isClose(1e-100, 0.0));
152 assert(isClose(1e-100, 0.0, 0.0, 1e-90));
153 assert(!isClose(1e-10, -1e-10));
154 assert(isClose(1e-10, -1e-10, 0.0, 1e-9));
155 assert(!isClose(1e-300, 1e-298));
156 assert(isClose(1e-300, 1e-298, 0.0, 1e-200));
157
158 // different default limits for different floating point types
159 assert(isClose(1.0f, 0.999_99f));
160 assert(!isClose(1.0, 0.999_99));
161 static if (real.sizeof > double.sizeof)
162 assert(!isClose(1.0L, 0.999_999_999L));
163 }
164
165 @safe pure nothrow unittest
166 {
167 import std.math.operations;
168
169 assert(isClose([1.0, 2.0, 3.0], [0.999_999_999, 2.000_000_001, 3.0]));
170 assert(!isClose([1.0, 2.0], [0.999_999_999, 2.000_000_001, 3.0]));
171 assert(!isClose([1.0, 2.0, 3.0], [0.999_999_999, 2.000_000_001]));
172
173 assert(isClose([2.0, 1.999_999_999, 2.000_000_001], 2.0));
174 assert(isClose(2.0, [2.0, 1.999_999_999, 2.000_000_001]));
175 }
176
177 @safe unittest
178 {
179 import std.math.operations;
180
181 assert(cmp(-double.infinity, -double.max) < 0);
182 assert(cmp(-double.max, -100.0) < 0);
183 assert(cmp(-100.0, -0.5) < 0);
184 assert(cmp(-0.5, 0.0) < 0);
185 assert(cmp(0.0, 0.5) < 0);
186 assert(cmp(0.5, 100.0) < 0);
187 assert(cmp(100.0, double.max) < 0);
188 assert(cmp(double.max, double.infinity) < 0);
189
190 assert(cmp(1.0, 1.0) == 0);
191 }
192
193 @safe unittest
194 {
195 import std.math.operations;
196
197 assert(cmp(-0.0, +0.0) < 0);
198 assert(cmp(+0.0, -0.0) > 0);
199 }
200
201 @safe unittest
202 {
203 import std.math.operations;
204
205 assert(cmp(-double.nan, -double.infinity) < 0);
206 assert(cmp(double.infinity, double.nan) < 0);
207 assert(cmp(-double.nan, double.nan) < 0);
208 }
209
210 @safe unittest
211 {
212 import std.math.operations;
213
214 assert(cmp(NaN(10), NaN(20)) < 0);
215 assert(cmp(-NaN(20), -NaN(10)) < 0);
216 }
217