Skip to content

Commit 23cdf7d

Browse files
authored
improve human readable floating point formatting (#331)
* improve human readable floating point formatting * fixup
1 parent 815b1e6 commit 23cdf7d

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

source/mir/bignum/decimal.d

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ struct Decimal(size_t maxSize64)
240240
size_t coefficientLength;
241241
static if (size_t.sizeof == 8)
242242
{
243-
if (__ctfe && size_t.sizeof == 8)
243+
if (__ctfe)
244244
{
245245
uint[coefficient.data.length * 2] data;
246246
foreach (i; 0 .. coefficient.length)
@@ -280,9 +280,25 @@ struct Decimal(size_t maxSize64)
280280
{
281281
// try print decimal form without exponent
282282
// up to 6 digits exluding leading 0. or final .0
283+
sizediff_t s = this.exponent + coefficientLength;
284+
if (s <= 0)
285+
{
286+
//0.001....
287+
//0.0001
288+
//0.00001
289+
//0.000001
290+
if (s >= -2 || this.exponent >= -6)
291+
{
292+
w.put(zeros[!coefficient.sign .. -s + 2 + 1]);
293+
w.put(buffer[$ - coefficientLength .. $]);
294+
return;
295+
}
296+
}
297+
else
283298
if (this.exponent >= 0)
284299
{
285-
if (this.exponent + coefficientLength <= 6)
300+
///dddddd.0
301+
if (s <= 6)
286302
{
287303
buffer[$ - coefficientLength - 1] = '-';
288304
w.put(buffer[$ - coefficientLength - coefficient.sign .. $]);
@@ -291,25 +307,15 @@ struct Decimal(size_t maxSize64)
291307
}
292308
}
293309
else
294-
if (this.exponent < 0)
295310
{
296-
if (this.exponent >= -6 && coefficientLength <= 6)
311+
///dddddd.d....
312+
if (s <= 6 || coefficientLength <= 6)
297313
{
298-
sizediff_t zerosLength = -this.exponent - coefficientLength;
299-
if (zerosLength >= 0)
300-
{
301-
w.put(zeros[!coefficient.sign .. zerosLength + 2 + 1]);
302-
w.put(buffer[$ - coefficientLength .. $]);
303-
return;
304-
}
305-
else
306-
{
307-
buffer[$ - coefficientLength - 1] = '-';
308-
w.put(buffer[$ - coefficientLength - coefficient.sign .. $ - coefficientLength - zerosLength]);
309-
buffer[$ - coefficientLength - zerosLength - 1] = '.';
310-
w.put(buffer[$ - coefficientLength - zerosLength - 1 .. $]);
311-
return;
312-
}
314+
buffer[$ - coefficientLength - 1] = '-';
315+
w.put(buffer[$ - coefficientLength - coefficient.sign .. $ - coefficientLength + s]);
316+
buffer[$ - coefficientLength + s - 1] = '.';
317+
w.put(buffer[$ - coefficientLength + s - 1 .. $]);
318+
return;
313319
}
314320
}
315321
}
@@ -340,7 +346,8 @@ struct Decimal(size_t maxSize64)
340346
else
341347
enum N = 11;
342348

343-
auto expLength = printSignedToTail(exponent, buffer[$ - N .. $], '\0');
349+
// prints e+/-exponent
350+
auto expLength = printSignedToTail(exponent, buffer[$ - N .. $], '+');
344351
buffer[$ - ++expLength] = 'e';
345352
w.put(buffer[$ - expLength .. $]);
346353
}

source/mir/format.d

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,22 @@ unittest
718718
assert(stringBuf() << 0.000003 << getData == "0.000003");
719719
assert(stringBuf() << -3e-7 << getData == "-3e-7");
720720
assert(stringBuf() << 123456.0 << getData == "123456.0");
721+
assert(stringBuf() << 123456.1 << getData == "123456.1");
721722
assert(stringBuf() << 12.3456 << getData == "12.3456");
722723
assert(stringBuf() << -0.123456 << getData == "-0.123456");
723-
assert(stringBuf() << 0.1234567 << getData == "1.234567e-1");
724-
assert(stringBuf() << -1234567.0 << getData == "-1.234567e6");
725-
assert(stringBuf() << 1234567890123.0 << getData == "1.234567890123e12");
724+
assert(stringBuf() << 0.1234567 << getData == "0.1234567");
725+
assert(stringBuf() << 0.01234567 << getData == "0.01234567");
726+
assert(stringBuf() << 0.001234567 << getData == "0.001234567");
727+
assert(stringBuf() << 1.234567e-4 << getData == "1.234567e-4");
728+
assert(stringBuf() << -1234567.0 << getData == "-1.234567e+6");
729+
assert(stringBuf() << 123456.7890123 << getData == "123456.7890123");
730+
assert(stringBuf() << 1234567.890123 << getData == "1.234567890123e+6");
731+
assert(stringBuf() << 1234567890123.0 << getData == "1.234567890123e+12");
732+
assert(stringBuf() << 1234567890123.0 << getData == "1.234567890123e+12");
733+
assert(stringBuf() << 0.30000000000000004 << getData == "0.30000000000000004");
734+
assert(stringBuf() << 0.030000000000000002 << getData == "0.030000000000000002");
735+
assert(stringBuf() << 0.0030000000000000005 << getData == "0.0030000000000000005");
736+
assert(stringBuf() << 3.0000000000000003e-4 << getData == "3.0000000000000003e-4");
726737
assert(stringBuf() << +double.nan << getData == "nan");
727738
assert(stringBuf() << -double.nan << getData == "nan");
728739
assert(stringBuf() << +double.infinity << getData == "+inf");

0 commit comments

Comments
 (0)