@@ -167,9 +167,11 @@ unittest
167
167
/* ************************************
168
168
* Round argument to a specific precision.
169
169
*
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.
173
175
*
174
176
* Params:
175
177
* T = precision type to round to
@@ -206,9 +208,10 @@ T toPrec(T:real)(real f) { pragma(inline, false); return f; }
206
208
207
209
@safe unittest
208
210
{
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 ;
212
215
f = toPrec! float (f + f);
213
216
f = toPrec! float (d + d);
214
217
f = toPrec! float (r + r);
@@ -219,19 +222,32 @@ T toPrec(T:real)(real f) { pragma(inline, false); return f; }
219
222
r = toPrec! real (d + d);
220
223
r = toPrec! real (r + r);
221
224
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
+
223
231
enum real PIR = 0xc .90fdaa22168c235p- 2 ;
224
232
enum double PID = 0x1 .921fb54442d18p+ 1 ;
225
233
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 ));
237
253
}
0 commit comments