1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*!
Conversions between text-encoded and binary-encoded decimal numbers.
*/
use crate::{
binary::{
decode_combination_finite,
decode_significand_trailing_declets,
encode_combination_finite,
encode_combination_infinity,
encode_combination_nan,
encode_significand_trailing_digits,
is_finite,
is_infinite,
is_nan,
is_quiet_nan,
is_sign_negative,
BinaryBuf,
BinaryExponent,
},
num::Integer,
text::{
ParsedDecimal,
ParsedDecimalPoint,
ParsedFinite,
ParsedInfinity,
ParsedNan,
ParsedNanHeader,
ParsedSignificand,
TextBuf,
},
OverflowError,
};
use core::{
fmt,
str,
};
mod from_binary_float;
mod from_int;
mod from_str;
pub(crate) use self::{
from_binary_float::*,
from_int::*,
from_str::*,
};
/**
Convert a decimal parsed from text into its binary form.
*/
pub(crate) fn decimal_from_parsed<D: BinaryBuf, T: TextBuf>(
parsed: ParsedDecimal<T>,
) -> Result<D, OverflowError> {
match parsed {
// ±1.234e±5
ParsedDecimal::Finite(ParsedFinite {
finite_buf,
finite_significand:
ParsedSignificand {
significand_is_negative,
significand_range,
decimal_point,
},
finite_exponent,
}) => {
let buf = finite_buf.get_ascii();
// First, get a starting point for the exponent.
let unbiased_exponent = match finite_exponent {
// If the number has an explicit exponent, like `1.23e4`, then parse it.
//
// The value will already have been validated, so this is more of a conversion
// than a regular parse.
Some(exponent) => D::try_exponent_from_ascii(
exponent.exponent_is_negative,
buf[exponent.exponent_range].iter().copied(),
)?,
// If the number doesn't have an explicit exponent, like `1.23`, then use 0.
None => D::default_exponent(),
};
match decimal_point {
// ±1.234e5
Some(ParsedDecimalPoint {
decimal_point_range,
}) => {
let integer_range = significand_range.start..decimal_point_range.start;
let fractional_range = decimal_point_range.end..significand_range.end;
let integer_digits = &buf[integer_range];
let fractional_digits = &buf[fractional_range];
// Account for the fractional part of the number
// This is where the exponent range that an end-user sees may
// be different than what's actually encoded. For example, the
// exponent range of a decimal64 is -383 to 384, as in
// 10e-383 and 10e384. However, for each fractional digit the
// exponent range is decreased by 1. This doesn't change the
// actual range of what's encoded, it just lets you specify
// the same values in different ways. 1.0e-382 and 1.0e385
// are both equivalent to the values mentioned before.
let unbiased_integer_exponent =
unbiased_exponent.lower(fractional_digits.len());
// Get a decimal buffer with enough space to fit all the digits
// and the exponent
let mut buf = D::try_with_at_least_precision(
integer_digits.len() + fractional_digits.len(),
Some(&unbiased_integer_exponent),
)?;
let msd = encode_significand_trailing_digits(
&mut buf,
[integer_digits, fractional_digits],
);
encode_combination_finite(
&mut buf,
significand_is_negative,
unbiased_integer_exponent,
msd,
);
Ok(buf)
}
// ±123e4
None => {
let integer_range = significand_range;
let integer_digits = &buf[integer_range];
// Get a decimal buffer with enough space to fit all the digits
// and the exponent
let mut buf = D::try_with_at_least_precision(
integer_digits.len(),
Some(&unbiased_exponent),
)?;
let msd = encode_significand_trailing_digits(&mut buf, [integer_digits]);
encode_combination_finite(
&mut buf,
significand_is_negative,
unbiased_exponent,
msd,
);
Ok(buf)
}
}
}
// ±inf
ParsedDecimal::Infinity(ParsedInfinity {
is_infinity_negative,
}) => {
// Infinity doesn't encode any special information, so we can ask for a buffer
// with the minimum size supported
let mut buf = D::try_with_at_least_storage_width_bytes(4)
.expect("infinity will always fit in the minimal sized buffer");
encode_combination_infinity(&mut buf, is_infinity_negative);
Ok(buf)
}
// ±nan(123)
ParsedDecimal::Nan(ParsedNan {
nan_buf,
nan_header:
ParsedNanHeader {
is_nan_signaling,
is_nan_negative,
},
nan_payload,
}) => {
// If the NaN was parsed with a payload then encode it.
//
// This process is the same as finite integers.
if let Some(ParsedSignificand {
significand_range, ..
}) = nan_payload
{
let payload_buf = nan_buf.get_ascii();
let mut buf = D::try_with_at_least_precision(significand_range.len() + 1, None)?;
encode_significand_trailing_digits(&mut buf, [&payload_buf[significand_range]]);
encode_combination_nan(&mut buf, is_nan_negative, is_nan_signaling);
Ok(buf)
}
// If the NaN doesn't have a payload then just ask for the minimum size buffer,
// just like we do for infinities.
else {
let mut buf = D::try_with_at_least_storage_width_bytes(4)
.expect("a NaN with no payload will always fit in the minimal sized buffer");
encode_combination_nan(&mut buf, is_nan_negative, is_nan_signaling);
Ok(buf)
}
}
}
}
/**
Convert a decimal in its binary form into text.
*/
pub(crate) fn decimal_to_fmt<D: BinaryBuf>(
decimal: &D,
mut out: impl fmt::Write,
) -> Result<(), fmt::Error> {
// Write the sign
if is_sign_negative(decimal) {
out.write_char('-')?;
}
// ±1.234e±5
if is_finite(decimal) {
let mut written = 0;
let (exponent, msd) = decode_combination_finite(decimal);
let msd = msd.get_ascii();
// The formatter works in declets, which are 3 digits at a time,
// in order from most to least significant.
//
// The precision of a decimal is always some number of declets plus
// the lonely most-significant-digit. We create a dummy declet for it here.
// Calculations that depend on the precision of the number need to have these
// extra two zeroes accounted for.
let mut declets = decode_significand_trailing_declets(decimal);
match exponent.to_i32() {
// ±123
Some(0) => {
write_all_as_integer(
skip_leading_zeroes(msd, &mut declets),
declets,
&mut written,
&mut out,
)?;
}
// ±123.456
Some(exponent) if exponent.is_negative() => {
// Skip leading zeroes
let skipped = skip_leading_zeroes(msd, &mut declets);
// Work out how many digits are remaining after skipping leading zeroes.
//
// The extra two precision accounts for the dummy declet created for the
// most-significant-digit.
let non_zero_digits =
adjusted_precision_digits_with_msd_declet(decimal) - skipped.skipped;
// Work out where the exponent falls in the decimal
// It might either be somewhere in the middle, as in `123.456`, or before it, as in `0.00123456`.
match non_zero_digits
.try_into()
.map(|non_zero_digits: i32| {
// The `exponent` is negative, so adding it to the non-zero number of digits will work out how
// many digits we need to write before the decimal point.
//
// This value itself may end up negative. If that happens it means there aren't enough
// digits to put the decimal point between. In that case, we'll write `0.` and then
// some number of leading zeroes first.
non_zero_digits + exponent
})
.ok()
{
// ±123.456
Some(integer_digits) if integer_digits > 0 => {
let total_integer_digits = integer_digits as usize;
// Write digits up to the decimal point
let mut written_decimal_point = false;
if let Some((declet, idx)) = skipped.partial_declet {
written_decimal_point = write_decimal_digits(
&declet[idx..],
total_integer_digits,
&mut written,
&mut out,
)?;
}
while !written_decimal_point {
written_decimal_point = write_decimal_digits(
&declets
.next()
.expect("ran out of digits before the decimal point"),
total_integer_digits,
&mut written,
&mut out,
)?;
}
// Write any remaining fractional digits
for declet in declets {
write_declet(declet, &mut written, &mut out)?;
}
}
// ±0.0123
Some(leading_zeroes) => {
debug_assert!(leading_zeroes == 0 || leading_zeroes.is_negative());
// This buffer determines how many leading zeroes we'll try write
// before falling back to exponential notation
const DECIMAL_ZEROES: &str = "0.00000";
let leading_zeroes = leading_zeroes.unsigned_abs() as usize;
// If the decimal point is before the non-zero digits, and there
// aren't too many leading zeroes then write them directly.
if leading_zeroes + "0.".len() <= DECIMAL_ZEROES.len() {
// Write the leading zeroes along with the decimal point
write_content(
&DECIMAL_ZEROES[..leading_zeroes + "0.".len()],
leading_zeroes,
&mut written,
&mut out,
)?;
// Write the declets as an integer following the leading fractional zeroes
write_all_as_integer(skipped, declets, &mut written, &mut out)?;
}
// If there are too many leading zeroes then write the number in scientific notation
else {
write_all_as_scientific(
skipped,
declets,
exponent,
&mut written,
&mut out,
)?;
}
}
// If the exponent is too large for an `i32` then write the number in scientific notation
None => {
write_all_as_scientific(
skipped,
declets,
exponent,
&mut written,
&mut out,
)?;
}
}
}
// ±1.234e±5
_ => {
write_all_as_scientific(
skip_leading_zeroes(msd, &mut declets),
declets,
exponent,
&mut written,
&mut out,
)?;
}
}
Ok(())
}
// ±inf
else if is_infinite(decimal) {
out.write_str("inf")
}
// ±nan(123)
else {
debug_assert!(is_nan(decimal));
if is_quiet_nan(decimal) {
out.write_str("nan")?;
} else {
out.write_str("snan")?;
}
// NaNs may include an integer payload.
//
// If the payload is non-zero then it'll also be written to the output.
let mut payload = decode_significand_trailing_declets(decimal)
.flatten()
.peekable();
// Skip over leading zeroes in the payload
while let Some(b'0') = payload.peek() {
let _ = payload.next();
}
// If there are any non-zero digits, then write them between braces
if payload.peek().is_some() {
out.write_char('(')?;
for digit in payload {
out.write_char(digit as char)?;
}
out.write_char(')')?;
}
Ok(())
}
}
fn adjusted_precision_digits_with_msd_declet(decimal: &impl BinaryBuf) -> usize {
decimal.precision_digits() + 2
}
fn skip_leading_zeroes(msd_ascii: u8, declets: impl Iterator<Item = [u8; 3]>) -> LeadingZeroes {
let mut skipped = 0;
// Check the most-significant-digit
if msd_ascii == b'0' {
skipped += 3;
} else {
return LeadingZeroes {
skipped: 2,
partial_declet: Some(([b'0', b'0', msd_ascii], 2)),
};
}
for declet in declets {
// If the declet contains just zeroes then skip them entirely
if declet == [b'0', b'0', b'0'] {
skipped += 3;
continue;
}
// Find the first non-zero slice of the declet
else {
let mut i = 0;
for digit in declet {
if digit != b'0' {
break;
}
i += 1;
}
skipped += i;
return LeadingZeroes {
skipped,
partial_declet: Some((declet, i)),
};
}
}
LeadingZeroes {
skipped,
partial_declet: None,
}
}
fn write_content(
content: &str,
digits: usize,
written: &mut usize,
mut out: impl fmt::Write,
) -> Result<(), fmt::Error> {
*written += digits;
out.write_str(content)
}
fn write_digits(
digits: &[u8],
written: &mut usize,
out: impl fmt::Write,
) -> Result<(), fmt::Error> {
write_content(
str::from_utf8(digits).map_err(|_| fmt::Error)?,
digits.len(),
written,
out,
)
}
fn write_declet(
declet: [u8; 3],
written: &mut usize,
out: impl fmt::Write,
) -> Result<(), fmt::Error> {
write_content(
str::from_utf8(&declet).map_err(|_| fmt::Error)?,
declet.len(),
written,
out,
)
}
fn write_decimal_digits(
digits: &[u8],
total_integer_digits: usize,
written: &mut usize,
mut out: impl fmt::Write,
) -> Result<bool, fmt::Error> {
// If the decimal point doesn't intersect these digits then write it and continue
if *written + digits.len() <= total_integer_digits {
write_digits(digits, written, &mut out)?;
Ok(false)
}
// If the decimal point falls at the start of these digits then print it first
else if *written == total_integer_digits {
out.write_char('.')?;
write_digits(digits, written, &mut out)?;
Ok(true)
}
// If the decimal point falls within these digits then print it in the middle and break
else {
let decimal_point = total_integer_digits - *written;
write_digits(&digits[..decimal_point], written, &mut out)?;
out.write_char('.')?;
write_digits(&digits[decimal_point..], written, &mut out)?;
Ok(true)
}
}
fn write_all_as_integer(
leading_zeroes: LeadingZeroes,
declets: impl Iterator<Item = [u8; 3]>,
written: &mut usize,
mut out: impl fmt::Write,
) -> Result<(), fmt::Error> {
if let LeadingZeroes {
partial_declet: Some((declet, idx)),
..
} = leading_zeroes
{
write_digits(&declet[idx..], written, &mut out)?;
}
// Write the remaining digits
for declet in declets {
write_declet(declet, written, &mut out)?;
}
// If no digits were written then write a zero
if *written == 0 {
out.write_char('0')?;
}
Ok(())
}
fn write_all_as_scientific(
leading_zeroes: LeadingZeroes,
mut declets: impl Iterator<Item = [u8; 3]>,
exponent: impl BinaryExponent,
written: &mut usize,
mut out: impl fmt::Write,
) -> Result<(), fmt::Error> {
// Write the first declet along with a decimal point
if let LeadingZeroes {
partial_declet: Some((declet, idx)),
..
} = leading_zeroes
{
write_decimal_digits(&declet[idx..], 1, written, &mut out)?;
} else if let Some(declet) = declets.next() {
write_content(
str::from_utf8(&[declet[0], b'.', declet[1], declet[2]]).map_err(|_| fmt::Error)?,
1,
written,
&mut out,
)?;
}
// Write the remaining digits
for declet in declets {
write_declet(declet, written, &mut out)?;
}
// If no digits were written, then write a zero
if *written == 0 {
out.write_str("0e")?;
} else {
out.write_char('e')?;
}
// Adjust the integer exponent to the form `1.23e4`.
//
// This means raising it to account for the number of fractional digits written.
let exponent = exponent.raise(written.saturating_sub(1));
// Write the exponent into the buffer
exponent.to_fmt(&mut out)?;
Ok(())
}
struct LeadingZeroes {
skipped: usize,
partial_declet: Option<([u8; 3], usize)>,
}