Merge pull request #8073 from berni44/rounding_f

std.format: Use rounding tool to round %f on floats.
This commit is contained in:
Razvan Nitu 2021-05-20 14:31:59 +08:00 committed by GitHub
commit 16d01b0d14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1493,7 +1493,7 @@ private void printFloatF(bool g, Writer, T, Char)(auto ref Writer w, T val,
if (is(T == float) || is(T == double)
|| (is(T == real) && (T.mant_dig == double.mant_dig || T.mant_dig == 64)))
{
import std.format.internal.write : writeAligned, PrecisionType;
import std.format.internal.write : writeAligned, PrecisionType, RoundingClass, round;
static if (!g)
{
@ -1510,9 +1510,7 @@ if (is(T == float) || is(T == double)
char[T.max_exp + T.mant_dig + 1] dec_buf;
// for rounding we need to know if the rest of the number is exactly 0, between 0 and 0.5, 0.5 or above 0.5
enum roundType { ZERO, LOWER, FIVE, UPPER }
roundType next;
RoundingClass rc;
// Depending on exp, we will use one of three algorithms:
//
@ -1630,7 +1628,7 @@ if (is(T == float) || is(T == double)
dec_buf[--left] = cast(byte) ('0' + mod);
}
next = roundType.ZERO;
rc = RoundingClass.ZERO;
}
else if (exp < small_bound)
{
@ -1713,9 +1711,9 @@ if (is(T == float) || is(T == double)
static if (g) start = 2;
if (lsu >= count - 1 && mybig[count - 1] == 0)
next = roundType.ZERO;
rc = RoundingClass.ZERO;
else if (lsu == count - 1 && mybig[lsu] == 1L << 59)
next = roundType.FIVE;
rc = RoundingClass.FIVE;
else
{
ulong over = 0;
@ -1725,7 +1723,7 @@ if (is(T == float) || is(T == double)
over = mybig[i] >> 60;
mybig[i] &= (1L << 60) - 1;
}
next = over >= 5 ? roundType.UPPER : roundType.LOWER;
rc = over >= 5 ? RoundingClass.UPPER : RoundingClass.LOWER;
}
}
else
@ -1805,7 +1803,7 @@ if (is(T == float) || is(T == double)
static if (g) start = save_start;
if (frac_part == 0)
next = roundType.ZERO;
rc = RoundingClass.ZERO;
else
{
frac_part *= 10;
@ -1813,73 +1811,15 @@ if (is(T == float) || is(T == double)
frac_part &= ((1L << (T.mant_dig - 1 - exp)) - 1);
if (nextDigit == 5 && frac_part == 0)
next = roundType.FIVE;
rc = RoundingClass.FIVE;
else if (nextDigit >= 5)
next = roundType.UPPER;
rc = RoundingClass.UPPER;
else
next = roundType.LOWER;
rc = RoundingClass.LOWER;
}
}
// rounding
bool roundUp = false;
if (rm == RoundingMode.up)
roundUp = next != roundType.ZERO && sgn != "-";
else if (rm == RoundingMode.down)
roundUp = next != roundType.ZERO && sgn == "-";
else if (rm == RoundingMode.toZero)
roundUp = false;
else
{
assert(rm == RoundingMode.toNearestTiesToEven || rm == RoundingMode.toNearestTiesAwayFromZero,
"RoundingMode is not toNearest");
roundUp = next == roundType.UPPER;
if (next == roundType.FIVE)
{
// IEEE754 allows for two different ways of implementing roundToNearest:
// Round to nearest, ties away from zero
if (rm == RoundingMode.toNearestTiesAwayFromZero)
roundUp = true;
else
{
// Round to nearest, ties to even
auto last = dec_buf[right - 1];
if (last == '.') last = dec_buf[right - 2];
roundUp = last % 2 != 0;
}
}
}
if (roundUp)
{
foreach_reverse (i;left .. right)
{
if (dec_buf[i] == '.') continue;
if (dec_buf[i] == '9')
dec_buf[i] = '0';
else
{
dec_buf[i]++;
static if (g)
{
// in case of 0.0...009...9 => 0.0...010...0 we have to remove
// the right most digit to get the precision right
if (dec_buf[i] == '1')
{
foreach (j;left .. i)
if (dec_buf[j] != '0' && dec_buf[j] != '.') goto printFloat_done;
right--;
}
}
goto printFloat_done;
}
}
dec_buf[--left] = '1';
printFloat_done:
}
if (round(dec_buf, left, right, rc, sgn == "-")) left--;
while (right > start + 1 && dec_buf[right - 1] == '0') right--;
@ -2018,11 +1958,20 @@ printFloat_done:
@safe unittest
{
import std.math.hardware; // cannot be selective, because FloatingPointControl might not be defined
// std.math's FloatingPointControl isn't available on all target platforms
static if (is(FloatingPointControl))
{
FloatingPointControl fpctrl;
auto f = FormatSpec!dchar("");
f.spec = 'f';
f.precision = 0;
// ties away from zero
fpctrl.rounding = FloatingPointControl.roundToNearest;
/*
assert(printFloat(11.5f, f, RoundingMode.toNearestTiesAwayFromZero) == "12");
assert(printFloat(12.5f, f, RoundingMode.toNearestTiesAwayFromZero) == "13");
assert(printFloat(11.7f, f, RoundingMode.toNearestTiesAwayFromZero) == "12");
@ -2033,8 +1982,8 @@ printFloat_done:
assert(printFloat(-11.7f, f, RoundingMode.toNearestTiesAwayFromZero) == "-12");
assert(printFloat(-11.3f, f, RoundingMode.toNearestTiesAwayFromZero) == "-11");
assert(printFloat(-11.0f, f, RoundingMode.toNearestTiesAwayFromZero) == "-11");
*/
// ties to even
assert(printFloat(11.5f, f) == "12");
assert(printFloat(12.5f, f) == "12");
assert(printFloat(11.7f, f) == "12");
@ -2046,6 +1995,8 @@ printFloat_done:
assert(printFloat(-11.3f, f) == "-11");
assert(printFloat(-11.0f, f) == "-11");
fpctrl.rounding = FloatingPointControl.roundToZero;
assert(printFloat(11.5f, f, RoundingMode.toZero) == "11");
assert(printFloat(12.5f, f, RoundingMode.toZero) == "12");
assert(printFloat(11.7f, f, RoundingMode.toZero) == "11");
@ -2057,6 +2008,8 @@ printFloat_done:
assert(printFloat(-11.3f, f, RoundingMode.toZero) == "-11");
assert(printFloat(-11.0f, f, RoundingMode.toZero) == "-11");
fpctrl.rounding = FloatingPointControl.roundUp;
assert(printFloat(11.5f, f, RoundingMode.up) == "12");
assert(printFloat(12.5f, f, RoundingMode.up) == "13");
assert(printFloat(11.7f, f, RoundingMode.up) == "12");
@ -2068,6 +2021,8 @@ printFloat_done:
assert(printFloat(-11.3f, f, RoundingMode.up) == "-11");
assert(printFloat(-11.0f, f, RoundingMode.up) == "-11");
fpctrl.rounding = FloatingPointControl.roundDown;
assert(printFloat(11.5f, f, RoundingMode.down) == "11");
assert(printFloat(12.5f, f, RoundingMode.down) == "12");
assert(printFloat(11.7f, f, RoundingMode.down) == "11");
@ -2078,6 +2033,7 @@ printFloat_done:
assert(printFloat(-11.7f, f, RoundingMode.down) == "-12");
assert(printFloat(-11.3f, f, RoundingMode.down) == "-12");
assert(printFloat(-11.0f, f, RoundingMode.down) == "-11");
}
}
@safe unittest
@ -2563,11 +2519,21 @@ if (is(T == float) || is(T == double)
@safe unittest
{
import std.math.hardware; // cannot be selective, because FloatingPointControl might not be defined
// std.math's FloatingPointControl isn't available on all target platforms
static if (is(FloatingPointControl))
{
FloatingPointControl fpctrl;
char[256] buf;
auto f = FormatSpec!dchar("");
f.spec = 'g';
f.precision = 2;
fpctrl.rounding = FloatingPointControl.roundToNearest;
/*
assert(printFloat(11.5f, f, RoundingMode.toNearestTiesAwayFromZero) == "12");
assert(printFloat(12.5f, f, RoundingMode.toNearestTiesAwayFromZero) == "13");
assert(printFloat(11.7f, f, RoundingMode.toNearestTiesAwayFromZero) == "12");
@ -2578,6 +2544,7 @@ if (is(T == float) || is(T == double)
assert(printFloat(-11.7f, f, RoundingMode.toNearestTiesAwayFromZero) == "-12");
assert(printFloat(-11.3f, f, RoundingMode.toNearestTiesAwayFromZero) == "-11");
assert(printFloat(-11.0f, f, RoundingMode.toNearestTiesAwayFromZero) == "-11");
*/
// ties to even
assert(printFloat(11.5f, f) == "12");
@ -2591,6 +2558,8 @@ if (is(T == float) || is(T == double)
assert(printFloat(-11.3f, f) == "-11");
assert(printFloat(-11.0f, f) == "-11");
fpctrl.rounding = FloatingPointControl.roundToZero;
assert(printFloat(11.5f, f, RoundingMode.toZero) == "11");
assert(printFloat(12.5f, f, RoundingMode.toZero) == "12");
assert(printFloat(11.7f, f, RoundingMode.toZero) == "11");
@ -2602,6 +2571,8 @@ if (is(T == float) || is(T == double)
assert(printFloat(-11.3f, f, RoundingMode.toZero) == "-11");
assert(printFloat(-11.0f, f, RoundingMode.toZero) == "-11");
fpctrl.rounding = FloatingPointControl.roundUp;
assert(printFloat(11.5f, f, RoundingMode.up) == "12");
assert(printFloat(12.5f, f, RoundingMode.up) == "13");
assert(printFloat(11.7f, f, RoundingMode.up) == "12");
@ -2613,6 +2584,8 @@ if (is(T == float) || is(T == double)
assert(printFloat(-11.3f, f, RoundingMode.up) == "-11");
assert(printFloat(-11.0f, f, RoundingMode.up) == "-11");
fpctrl.rounding = FloatingPointControl.roundDown;
assert(printFloat(11.5f, f, RoundingMode.down) == "11");
assert(printFloat(12.5f, f, RoundingMode.down) == "12");
assert(printFloat(11.7f, f, RoundingMode.down) == "11");
@ -2623,6 +2596,7 @@ if (is(T == float) || is(T == double)
assert(printFloat(-11.7f, f, RoundingMode.down) == "-12");
assert(printFloat(-11.3f, f, RoundingMode.down) == "-12");
assert(printFloat(-11.0f, f, RoundingMode.down) == "-11");
}
}
@safe unittest