Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_numeric.d

raw
1 @safe unittest
2 {
3 import std.numeric;
4
5 import std.math.trigonometry : sin, cos;
6
7 // Define a 16-bit floating point values
8 CustomFloat!16 x; // Using the number of bits
9 CustomFloat!(10, 5) y; // Using the precision and exponent width
10 CustomFloat!(10, 5,CustomFloatFlags.ieee) z; // Using the precision, exponent width and format flags
11 CustomFloat!(10, 5,CustomFloatFlags.ieee, 15) w; // Using the precision, exponent width, format flags and exponent offset bias
12
13 // Use the 16-bit floats mostly like normal numbers
14 w = x*y - 1;
15
16 // Functions calls require conversion
17 z = sin(+x) + cos(+y); // Use unary plus to concisely convert to a real
18 z = sin(x.get!float) + cos(y.get!float); // Or use get!T
19 z = sin(cast(float) x) + cos(cast(float) y); // Or use cast(T) to explicitly convert
20
21 // Define a 8-bit custom float for storing probabilities
22 alias Probability = CustomFloat!(4, 4, CustomFloatFlags.ieee^CustomFloatFlags.probability^CustomFloatFlags.signed );
23 auto p = Probability(0.5);
24 }
25
26 @safe unittest
27 {
28 import std.numeric;
29
30 import std.math.operations : isClose;
31
32 // Average numbers in an array
33 double avg(in double[] a)
34 {
35 if (a.length == 0) return 0;
36 FPTemporary!double result = 0;
37 foreach (e; a) result += e;
38 return result / a.length;
39 }
40
41 auto a = [1.0, 2.0, 3.0];
42 assert(isClose(avg(a), 2));
43 }
44
45 @safe unittest
46 {
47 import std.numeric;
48
49 import std.math.operations : isClose;
50 import std.math.trigonometry : cos;
51
52 float f(float x)
53 {
54 return cos(x) - x*x*x;
55 }
56 auto x = secantMethod!(f)(0f, 1f);
57 assert(isClose(x, 0.865474));
58 }
59
60 @safe unittest
61 {
62 import std.numeric;
63
64 import std.math.operations : isClose;
65
66 auto ret = findLocalMin((double x) => (x-4)^^2, -1e7, 1e7);
67 assert(ret.x.isClose(4.0));
68 assert(ret.y.isClose(0.0, 0.0, 1e-10));
69 }
70
71 @safe unittest
72 {
73 import std.numeric;
74
75 double[] a = [];
76 assert(!normalize(a));
77 a = [ 1.0, 3.0 ];
78 assert(normalize(a));
79 assert(a == [ 0.25, 0.75 ]);
80 assert(normalize!(typeof(a))(a, 50)); // a = [12.5, 37.5]
81 a = [ 0.0, 0.0 ];
82 assert(!normalize(a));
83 assert(a == [ 0.5, 0.5 ]);
84 }
85
86 @safe unittest
87 {
88 import std.numeric;
89
90 import std.math.traits : isNaN;
91
92 assert(sumOfLog2s(new double[0]) == 0);
93 assert(sumOfLog2s([0.0L]) == -real.infinity);
94 assert(sumOfLog2s([-0.0L]) == -real.infinity);
95 assert(sumOfLog2s([2.0L]) == 1);
96 assert(sumOfLog2s([-2.0L]).isNaN());
97 assert(sumOfLog2s([real.nan]).isNaN());
98 assert(sumOfLog2s([-real.nan]).isNaN());
99 assert(sumOfLog2s([real.infinity]) == real.infinity);
100 assert(sumOfLog2s([-real.infinity]).isNaN());
101 assert(sumOfLog2s([ 0.25, 0.25, 0.25, 0.125 ]) == -9);
102 }
103
104 @safe unittest
105 {
106 import std.numeric;
107
108 import std.math.operations : isClose;
109
110 double[] p = [ 0.0, 0, 0, 1 ];
111 assert(kullbackLeiblerDivergence(p, p) == 0);
112 double[] p1 = [ 0.25, 0.25, 0.25, 0.25 ];
113 assert(kullbackLeiblerDivergence(p1, p1) == 0);
114 assert(kullbackLeiblerDivergence(p, p1) == 2);
115 assert(kullbackLeiblerDivergence(p1, p) == double.infinity);
116 double[] p2 = [ 0.2, 0.2, 0.2, 0.4 ];
117 assert(isClose(kullbackLeiblerDivergence(p1, p2), 0.0719281, 1e-5));
118 assert(isClose(kullbackLeiblerDivergence(p2, p1), 0.0780719, 1e-5));
119 }
120
121 @safe unittest
122 {
123 import std.numeric;
124
125 import std.math.operations : isClose;
126
127 double[] p = [ 0.0, 0, 0, 1 ];
128 assert(jensenShannonDivergence(p, p) == 0);
129 double[] p1 = [ 0.25, 0.25, 0.25, 0.25 ];
130 assert(jensenShannonDivergence(p1, p1) == 0);
131 assert(isClose(jensenShannonDivergence(p1, p), 0.548795, 1e-5));
132 double[] p2 = [ 0.2, 0.2, 0.2, 0.4 ];
133 assert(isClose(jensenShannonDivergence(p1, p2), 0.0186218, 1e-5));
134 assert(isClose(jensenShannonDivergence(p2, p1), 0.0186218, 1e-5));
135 assert(isClose(jensenShannonDivergence(p2, p1, 0.005), 0.00602366, 1e-5));
136 }
137
138 @system unittest
139 {
140 import std.numeric;
141
142 import std.math.operations : isClose;
143 import std.math.algebraic : sqrt;
144
145 string[] s = ["Hello", "brave", "new", "world"];
146 string[] t = ["Hello", "new", "world"];
147 assert(gapWeightedSimilarity(s, s, 1) == 15);
148 assert(gapWeightedSimilarity(t, t, 1) == 7);
149 assert(gapWeightedSimilarity(s, t, 1) == 7);
150 assert(isClose(gapWeightedSimilarityNormalized(s, t, 1),
151 7.0 / sqrt(15.0 * 7), 0.01));
152 }
153
154 @system unittest
155 {
156 import std.numeric;
157
158 string[] s = ["Hello", "brave", "new", "world"];
159 string[] t = ["Hello", "new", "world"];
160 auto simIter = gapWeightedSimilarityIncremental(s, t, 1.0);
161 assert(simIter.front == 3); // three 1-length matches
162 simIter.popFront();
163 assert(simIter.front == 3); // three 2-length matches
164 simIter.popFront();
165 assert(simIter.front == 1); // one 3-length match
166 simIter.popFront();
167 assert(simIter.empty); // no more match
168 }
169
170 @safe unittest
171 {
172 import std.numeric;
173
174 assert(gcd(2 * 5 * 7 * 7, 5 * 7 * 11) == 5 * 7);
175 const int a = 5 * 13 * 23 * 23, b = 13 * 59;
176 assert(gcd(a, b) == 13);
177 }
178
179 @safe unittest
180 {
181 import std.numeric;
182
183 assert(lcm(1, 2) == 2);
184 assert(lcm(3, 4) == 12);
185 assert(lcm(5, 6) == 30);
186 }
187
188 @safe pure @nogc unittest
189 {
190 import std.numeric;
191
192 ubyte[21] fac;
193 size_t idx = decimalToFactorial(2982, fac);
194
195 assert(fac[0] == 4);
196 assert(fac[1] == 0);
197 assert(fac[2] == 4);
198 assert(fac[3] == 1);
199 assert(fac[4] == 0);
200 assert(fac[5] == 0);
201 assert(fac[6] == 0);
202 }
203