Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit dd7a8b0

Browse files
authored
Merge pull request #2871 from ibuclaw/toprectest
core.math: Re-enable disabled tests and update toPrec comment
2 parents 77df0e9 + 57be9d1 commit dd7a8b0

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/core/math.d

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ unittest
167167
/*************************************
168168
* Round argument to a specific precision.
169169
*
170-
* D language types specify a minimum precision, not a maximum. The
171-
* `toPrec()` function forces rounding of the argument `f` to the
172-
* precision of the specified floating point type `T`.
170+
* D language types specify only a minimum precision, not a maximum. The
171+
* `toPrec()` function forces rounding of the argument `f` to the precision
172+
* of the specified floating point type `T`.
173+
* The rounding mode used is inevitably target-dependent, but will be done in
174+
* a way to maximize accuracy. In most cases, the default is round-to-nearest.
173175
*
174176
* Params:
175177
* T = precision type to round to
@@ -206,9 +208,10 @@ T toPrec(T:real)(real f) { pragma(inline, false); return f; }
206208

207209
@safe unittest
208210
{
209-
static float f = 1.1f;
210-
static double d = 1.1;
211-
static real r = 1.1L;
211+
// Test all instantiations work with all combinations of float.
212+
float f = 1.1f;
213+
double d = 1.1;
214+
real r = 1.1L;
212215
f = toPrec!float(f + f);
213216
f = toPrec!float(d + d);
214217
f = toPrec!float(r + r);
@@ -219,19 +222,32 @@ T toPrec(T:real)(real f) { pragma(inline, false); return f; }
219222
r = toPrec!real(d + d);
220223
r = toPrec!real(r + r);
221224

222-
/+ Uncomment these once compiler support has been added.
225+
// Comparison tests.
226+
bool approxEqual(T)(T lhs, T rhs)
227+
{
228+
return fabs((lhs - rhs) / rhs) <= 1e-2 || fabs(lhs - rhs) <= 1e-5;
229+
}
230+
223231
enum real PIR = 0xc.90fdaa22168c235p-2;
224232
enum double PID = 0x1.921fb54442d18p+1;
225233
enum float PIF = 0x1.921fb6p+1;
226-
227-
assert(toPrec!float(PIR) == PIF);
228-
assert(toPrec!double(PIR) == PID);
229-
assert(toPrec!real(PIR) == PIR);
230-
assert(toPrec!float(PID) == PIF);
231-
assert(toPrec!double(PID) == PID);
232-
assert(toPrec!real(PID) == PID);
233-
assert(toPrec!float(PIF) == PIF);
234-
assert(toPrec!double(PIF) == PIF);
235-
assert(toPrec!real(PIF) == PIF);
236-
+/
234+
static assert(approxEqual(toPrec!float(PIR), PIF));
235+
static assert(approxEqual(toPrec!double(PIR), PID));
236+
static assert(approxEqual(toPrec!real(PIR), PIR));
237+
static assert(approxEqual(toPrec!float(PID), PIF));
238+
static assert(approxEqual(toPrec!double(PID), PID));
239+
static assert(approxEqual(toPrec!real(PID), PID));
240+
static assert(approxEqual(toPrec!float(PIF), PIF));
241+
static assert(approxEqual(toPrec!double(PIF), PIF));
242+
static assert(approxEqual(toPrec!real(PIF), PIF));
243+
244+
assert(approxEqual(toPrec!float(PIR), PIF));
245+
assert(approxEqual(toPrec!double(PIR), PID));
246+
assert(approxEqual(toPrec!real(PIR), PIR));
247+
assert(approxEqual(toPrec!float(PID), PIF));
248+
assert(approxEqual(toPrec!double(PID), PID));
249+
assert(approxEqual(toPrec!real(PID), PID));
250+
assert(approxEqual(toPrec!float(PIF), PIF));
251+
assert(approxEqual(toPrec!double(PIF), PIF));
252+
assert(approxEqual(toPrec!real(PIF), PIF));
237253
}

0 commit comments

Comments
 (0)