-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
2106 lines (1888 loc) · 72.1 KB
/
lib.rs
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![feature(bigint_helper_methods)]
#![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_array_assume_init)]
#![feature(strict_overflow_ops)]
#![feature(test)]
extern crate test;
use std::convert::From;
use std::fmt;
use std::mem::MaybeUninit;
use std::str::from_utf8_unchecked;
/// Count with plain integers.
pub type Natural = BaseCount<0>;
pub type Quetta = BaseCount<30>;
pub type Ronna = BaseCount<27>;
pub type Yotta = BaseCount<24>;
pub type Zetta = BaseCount<21>;
pub type Exa = BaseCount<18>;
pub type Peta = BaseCount<15>;
pub type Tera = BaseCount<12>;
pub type Giga = BaseCount<9>;
pub type Mega = BaseCount<6>;
pub type Kilo = BaseCount<3>;
pub type Hecto = BaseCount<2>;
pub type Deca = BaseCount<1>;
pub type Deci = BaseCount<-1>;
pub type Centi = BaseCount<-2>;
pub type Milli = BaseCount<-3>;
pub type Micro = BaseCount<-6>;
pub type Nano = BaseCount<-9>;
pub type Pico = BaseCount<-12>;
pub type Femto = BaseCount<-15>;
pub type Atto = BaseCount<-18>;
pub type Zepto = BaseCount<-21>;
pub type Yocto = BaseCount<-24>;
pub type Ronto = BaseCount<-27>;
pub type Quecto = BaseCount<-30>;
/// Count a base number, which is a generic exponent of ten.
/// EXP can be seen as the resolution of the count, i.e., EXP
/// 3 counts per thousand, and -2 counts with two fractions.
///
/// ```
/// let cents = b10::BaseCount::<-2>::from(199);
/// assert_eq!("€ 1.99", format!("€ {cents}"));
/// ```
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
pub struct BaseCount<const EXP: i8> {
c: u64,
}
/// Zero is the default.
impl<const EXP: i8> Default for BaseCount<EXP> {
fn default() -> Self {
return Self::ZERO;
}
}
/// Count Adoption
///
/// ```
/// let degrees = b10::Deci::from(192);
/// assert_eq!("19.2 ℃", format!("{degrees} ℃"));
/// ```
impl<const EXP: i8> From<u64> for BaseCount<EXP> {
/// Apply the count to the base component.
fn from(count: u64) -> Self {
return Self { c: count };
}
}
/// Count Extraction
///
/// ```
/// type Century = b10::BaseCount::<2>;
/// let previous = Century::map_n(1900).unwrap();
/// assert_eq!(19, u64::from(previous));
/// ```
impl<const EXP: i8> From<BaseCount<EXP>> for u64 {
/// Decouple the count from the base component.
fn from(c: BaseCount<EXP>) -> u64 {
return c.c;
}
}
impl<const EXP: i8> BaseCount<EXP> {
/// Numeric value 0 is always in range.
pub const ZERO: Self = Self { c: 0 };
/// The smallest numeric value in range is 10 to the power of EXP.
pub const ONE: Self = Self { c: 1 };
/// The largest numeric value in range is `u64::MAX` times `Self::ONE`.
pub const MAX: Self = Self { c: u64::MAX };
/// Get numeric value n iff an exact match within the base exponent exists,
/// and iff the numeric value is in range `Self::MAX`.
///
/// ```
/// use b10::{Centi, Kilo};
///
/// assert_eq!(Some(Centi::from(200)), Centi::map_n(2));
/// assert_eq!(Some(Kilo::from(5)), Kilo::map_n(5000));
///
/// // range protection
/// assert_eq!(None, Centi::map_n(u64::MAX));
/// // loss-of-precision protection
/// assert_eq!(None, Kilo::map_n(5100));
/// ```
pub fn map_n(n: u64) -> Option<Self> {
Natural::from(n).rebase()
}
/// Get the same numeric value under base exponent R iff an exact match
/// exists, and iff the numeric value is in range `BaseCount<R>::MAX`.
///
/// ```
/// use b10::{Centi, Kilo};
///
/// assert_eq!(Some(Centi::from(700_000)), Kilo::from(7).rebase());
/// assert_eq!(Some(Kilo::from(7)), Centi::from(700_000).rebase());
/// ```
pub fn rebase<const R: i8>(self) -> Option<BaseCount<R>> {
if const { R == EXP } {
return Some(BaseCount::<R> { c: self.c });
}
if const { R < EXP } {
let (ratio, max_in) = const {
if R < EXP && (EXP as isize - R as isize) < 20 {
let ratio = 10u64.strict_pow((EXP - R) as u32);
(ratio, u64::MAX / ratio)
} else {
// only zero can be converted
(0u64, 0u64)
}
};
return if self.c <= max_in {
Some(BaseCount::<R> { c: self.c * ratio })
} else {
// rebase overflows Self::MAX
None
};
}
assert!(const { R > EXP });
if const { R as isize - EXP as isize > 19 } {
// rebase underflows Self::ONE
return None;
}
let downscale = const {
// redundant check needed for compiler
if R > EXP && (R as isize - EXP as isize) < 20 {
10u64.strict_pow((R - EXP) as u32)
} else {
// not possible in this branch
42u64 // arbitrary placeholder
}
};
// modulo and division caught as one by compiler
return if self.c % downscale == 0 {
Some(BaseCount::<R> {
c: self.c / downscale,
})
} else {
None
};
}
/// Get the sum of both counts including an overflow flag. For any pair of
/// arguments, self + summand = sum + overflow, in which overflow is 2⁶⁴
/// times the base when `true`, or 0 when `false`.
#[inline(always)]
pub fn sum(self, summand: Self) -> (Self, bool) {
let (sum, overflow) = self.c.overflowing_add(summand.c);
return (Self { c: sum }, overflow);
}
/// Get the difference between both counts including a negative flag. For
/// any pair of arguments, self − subtrahend = difference × negative, in
/// which negative is −1 when `true`, or 1 when `false`.
///
/// ```
/// let (a, b): (b10::Deca, b10::Deca) = (7.into(), 9.into());
/// assert_eq!(
/// "7E1 − 9E1 = (2E1, true)",
/// format!("{a:?} − {b:?} = {0:?}", a.difference(b)),
/// );
/// // zero is not negative
/// assert_eq!(
/// "7E1 − 7E1 = (0E1, false)",
/// format!("{a:?} − {a:?} = {0:?}", a.difference(a)),
/// );
/// ```
#[inline(always)]
pub fn difference(self, subtrahend: Self) -> (Self, bool) {
if self >= subtrahend {
((self.c - subtrahend.c).into(), false)
} else {
((subtrahend.c - self.c).into(), true)
}
}
/// Get the product of both counts including the 64-bit overflow, if any.
/// A compile-time check guarantees that generic P is equal to EXP + M to
/// ensure a lossless calculation exclusively. For any pair of arguments,
/// self × multiplicant = product + (overflow × 2⁶⁴).
///
/// ```
/// use b10::{Milli, Nano, Pico};
///
/// let mA = Milli::from(100);
/// let ns = Nano::from(4);
///
/// let (pC, overflow):(Pico, Pico) = mA.product(ns);
/// if overflow != Pico::ZERO {
/// panic!("too much for pico");
/// }
/// assert_eq!(
/// "100E-3 × 4E-9 = 400E-12",
/// format!("{mA:E} × {ns:E} = {pC:E}"),
/// );
/// ```
#[inline(always)]
pub fn product<const M: i8, const P: i8>(
self,
multiplicant: BaseCount<M>,
) -> (BaseCount<P>, BaseCount<P>) {
const {
if P != EXP + M {
panic!("generic EXP plus M goes not equal P");
}
}
let (product, overflow) = self.c.widening_mul(multiplicant.c);
return (
BaseCount::<P> { c: product },
BaseCount::<P> { c: overflow },
);
}
/// Get the quotient and the remainder for divisor, with None for division
/// by zero. D must greater or equal to EXP. The constraint prevents numeric
/// overflows by design.
///
/// ```
/// let price = b10::BaseCount::<-2>::from(100420);
/// let fifty = b10::Natural::from(50);
///
/// let (part, rem) = price.quotient(fifty).unwrap();
/// assert_eq!(
/// "1004.20 ÷ 50 is 20 with 4.20 remaining",
/// format!("{price} ÷ {fifty} is {part} with {rem} remaining"),
/// );
/// ```
#[inline(always)]
pub fn quotient<const D: i8>(self, divisor: BaseCount<D>) -> Option<(u64, Self)> {
const {
if D < EXP {
// could cause numeric overflows
panic!("divisor exponent smaller than divident exponent");
}
}
if divisor.c == 0 {
return None;
}
match divisor.rebase::<EXP>() {
None => Some((0, self)), // divisor is greater than self
Some(d) => Some((self.c / d.c, Self { c: self.c % d.c })),
}
}
/// Euclidian division of self with a constant DIVISOR.
///
/// ```
/// let price = b10::BaseCount::<-2>::from(299);
/// let (half, remainder) = price.quotient_const::<2>();
/// assert_eq!(
/// "½ of 2.99 is 1.49 with 0.01 remaining",
/// format!("½ of {price} is {half} with {remainder} remaining"),
/// );
/// ```
#[inline(always)]
pub fn quotient_const<const DIVISOR: u64>(self) -> (Self, Self) {
const {
if DIVISOR == 0 {
panic!("zero divisor denied");
}
}
((self.c / DIVISOR).into(), (self.c % DIVISOR).into())
}
/// Exponentiation of self with a constant POWER. The return is None when a
/// numeric overflow occurred.
///
/// ```
/// let rib = b10::Milli::from(12);
/// let square = rib.power_const::<2, -6>().unwrap();
/// let cube = rib.power_const::<3, -9>().unwrap();
/// assert_eq!(
/// "0.012² = 0.000144",
/// format!("{rib}² = {square}"),
/// );
/// assert_eq!(
/// "0.012³ = 0.000001728",
/// format!("{rib}³ = {cube}"),
/// );
#[inline(always)]
pub fn power_const<const POWER: u32, const P: i8>(self) -> Option<BaseCount<P>> {
// compile-time checks
const {
if POWER == 0 {
panic!("to the power of 0 always is 1")
}
if POWER == 1 {
panic!("to the power of 1 is a no-op")
}
if POWER as i64 * EXP as i64 != P as i64 {
panic!("generic POWER times EXP does not equal P");
}
}
match self.c.checked_pow(POWER) {
None => None,
Some(p) => Some(p.into()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn generic_constants() {
assert!(Kilo::ONE > Kilo::ZERO);
assert!(Kilo::MAX > Kilo::ONE);
assert_eq!(0 as u64, Kilo::ZERO.into());
assert_eq!(1 as u64, Kilo::ONE.into());
assert_eq!(u64::MAX, Kilo::MAX.into());
}
#[test]
fn rebase() {
assert_eq!(Some(Natural::from(1000)), Kilo::from(1).rebase());
assert_eq!(Some(Kilo::from(2000)), Mega::from(2).rebase());
assert_eq!(Some(Milli::from(3000)), Natural::from(3).rebase());
assert_eq!(Some(Micro::from(4000)), Milli::from(4).rebase());
// limit of 64 bits is 18_446_744_073_709_551_615
assert_eq!(
Some(Femto::from(18_000_000_000_000_000_000)),
Kilo::from(18).rebase::<-15>(),
);
assert_eq!(None, Kilo::from(19).rebase::<-15>(),);
// below Self::ONE
assert_eq!(None, Exa::from(200).rebase::<0>());
assert_eq!(None, Exa::from(300).rebase::<-128>());
assert_eq!(None, Natural::from(400).rebase::<-18>());
assert_eq!(None, Natural::from(500).rebase::<-128>());
// deny rounding
assert_eq!(None, Natural::from(123).rebase::<1>());
assert_eq!(None, Natural::from(223).rebase::<2>());
assert_eq!(None, Natural::from(323).rebase::<3>());
assert_eq!(None, Natural::from(423).rebase::<4>());
}
#[test]
fn sum_overflow() {
assert_eq!((Deci::from(6), true), Deci::MAX.sum(Deci::from(7)));
}
#[test]
fn product() {
assert_eq!(
(Natural::from(6), Natural::ZERO),
Natural::from(2).product(Natural::from(3))
);
}
// quotient reversed with product plus remainder
#[test]
fn consistency() {
let a: [u64; 7] = [0, 0, 1, 42, u64::MAX, 200, 5000];
let b: [u64; 7] = [0, 1, 0, u64::MAX, 42, 20, 17];
for i in 0..a.len() {
let da = Deci::from(a[i]);
let db = Deca::from(b[i]);
match da.quotient(db) {
None => assert_eq!(db, Deca::ZERO),
Some((quot, rem)) => {
assert_ne!(db, Deca::ZERO);
println!("{da} ÷ {db} got {quot} with {rem} remaining");
let (prod, over) = Natural::from(quot).product::<1, 1>(db);
println!("{quot} × {db} got {prod} + {over} × 2⁶⁴");
assert_eq!(over, Deca::ZERO);
let (sum, carry) = prod.rebase::<-1>().unwrap().sum(rem);
println!("{prod} × {rem} got {sum} with carry {carry}");
assert_eq!(sum, da);
assert!(!carry);
}
};
}
}
}
/// Textual Representation
impl<const EXP: i8> BaseCount<EXP> {
/// Require `parse` to consume the text in full. As such, the Option always
/// is an exact reading of the numeric value in text.
pub fn parse_all(text: &[u8]) -> Option<Self> {
let (c, read) = Self::parse(text);
return if read < text.len() { None } else { Some(c) };
}
/// Read a numeric value from a JSON fragment until it finds either an ASCII
/// comma (","), a closing brace ("}"), or a closing bracket ("]"). Trailing
/// whitespace is ignored. The return is zero on error encounters.
pub fn parse_json(fragment: &[u8]) -> (Self, usize) {
let (c, mut i) = Self::parse(fragment);
return loop {
if i < fragment.len() {
match fragment[i] {
// whitespace
b' ' | b'\t' | b'\r' | b'\n' => {
i += 1;
continue;
}
// value continuation
b',' | b'}' | b']' => break (c, i),
// error
_ => {}
}
}
break (Self::ZERO, 0);
};
}
/// Get an exact reading of the numeric value at the start of text. The
/// `usize` in return has the number of octets parsed, which can be less
/// than slice length. Use `parse_all` to ensure a full reading instead.
/// Parsing is robust against malicious input. No assumptions are made on
/// the content of text.
///
/// ASCII character "." (0x2E) is recognised as a decimal separator. ASCII
/// character "E" (0x45) and "e" (0x65) are both accepted for E notation.
/// Non-significant digits (a.k.a. leading zeroes) are ignored/permitted.
/// The following cases get rejected with a zero usize.
///
/// * No input: empty text slice
/// * Range exhaustion: any numeric value above Self::MAX
/// * Beyond resolution: significant digits beneath Self::ONE
///
///
/// ```
/// let label = b"1.44 MB";
/// // micro resolution of mega value to count bytes
/// let (read_v, read_n) = b10::Micro::parse(label);
/// // verify parse together with unit expectation
/// assert_eq!(b" MB", &label[read_n..]);
/// assert_eq!(b10::Micro::from(1_440_000), read_v);
///
/// // need E notation for EXP greater than zero
/// assert_eq!((b10::Kilo::from(500), 5), b10::Kilo::parse(b"5.0E5"));
/// // … because digits beyond the resolution are denied, even when zero
/// assert_eq!((b10::Kilo::ZERO, 0), b10::Kilo::parse(b"1000"));
/// assert_eq!((b10::Centi::ZERO, 0), b10::Centi::parse(b"0.500"));
/// ```
pub fn parse(text: &[u8]) -> (Self, usize) {
// read number with optional E notation
let (int, exp, parse_len) = Self::parse_as_int_exp(text);
if parse_len == 0 {
return (Self::ZERO, 0);
}
// rebase int/exp to generic EXP
let count: Option<u64> = match exp - const { EXP as isize } {
0 => Some(int),
1 => int.checked_mul(10),
2 => int.checked_mul(100),
3 => int.checked_mul(1000),
4 => int.checked_mul(10000),
5 => int.checked_mul(100000),
6 => int.checked_mul(1000000),
7 => int.checked_mul(10000000),
8 => int.checked_mul(100000000),
9 => int.checked_mul(1000000000),
10 => int.checked_mul(10000000000),
11 => int.checked_mul(100000000000),
12 => int.checked_mul(1000000000000),
13 => int.checked_mul(10000000000000),
14 => int.checked_mul(100000000000000),
15 => int.checked_mul(1000000000000000),
16 => int.checked_mul(10000000000000000),
17 => int.checked_mul(100000000000000000),
18 => int.checked_mul(1000000000000000000),
19 => int.checked_mul(10000000000000000000),
// beyond resolution is not permitted
..0 => None,
// only non-significant digits permitted
20.. => {
if int == 0 {
Some(0)
} else {
None
}
}
};
match count {
None => (Self::ZERO, 0),
Some(n) => (Self { c: n }, parse_len),
}
}
/// Parse the number as an integer with a base-10 exponent. The usize in
/// return has the number of octets parsed from text with zero for none.
fn parse_as_int_exp(text: &[u8]) -> (u64, isize, usize) {
// parsed decimals
let mut num: u64 = 0;
// index of first decimal (with zero for none)
let mut fraction_offset: usize = 0;
// read index in text
let mut i: usize = 0;
while i < text.len() {
let c = text[i];
match c {
b'0'..=b'9' => {
let digit = c as u64 - b'0' as u64;
if num >= u64::MAX / 10 && (num > u64::MAX / 10 || digit > 5) {
// numeric overflow
return (0, 0, 0);
}
num = num * 10 + digit;
}
// decimal separator
b'.' => {
if fraction_offset != 0 {
// two separators
return (0, 0, 0);
}
fraction_offset = i + 1;
}
_ => break,
}
i += 1;
}
// exponent caused by fraction is negative, if any
let frac_exp: isize = if fraction_offset == 0 {
0 // without fraction
} else {
fraction_offset as isize - i as isize
};
// maybe E notation
if i >= text.len() || (text[i] != b'E' && text[i] != b'e') {
// no E notation
return (num, frac_exp, i);
}
i += 1;
// maybe exponent sign
let mut exp_neg = false;
if i < text.len() && text[i] == b'-' {
exp_neg = true;
i += 1;
} else if i < text.len() && text[i] == b'+' {
// redudant sign permitted
i += 1;
}
// read exponent number
let mut exp_num: usize = 0;
while i < text.len() && text[i] >= b'0' && text[i] <= b'9' {
let digit = text[i] as usize - b'0' as usize;
i += 1;
exp_num = exp_num * 10 + digit;
if exp_num > 0xFFFF {
return (0, 0, 0);
}
}
let exp = if exp_neg {
frac_exp - exp_num as isize
} else {
frac_exp + exp_num as isize
};
(num, exp, i)
}
/// Get the SI prefix with the empty string for undefined.
pub const fn metric_prefix() -> &'static str {
match EXP {
1 => "da",
2 => "h",
3 => "k",
6 => "M",
9 => "G",
12 => "T",
15 => "P",
18 => "E",
21 => "Z",
24 => "Y",
27 => "R",
30 => "Q",
-1 => "d",
-2 => "c",
-3 => "m",
-6 => "µ",
-9 => "n",
-12 => "p",
-15 => "f",
-18 => "a",
-21 => "z",
-24 => "y",
-27 => "r",
-30 => "q",
_ => "",
}
}
fn fmt_metric_prefix(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match const { EXP } {
31.. => write!(f, "{self:E}"),
30 => write!(f, "{} Q", BaseCount::<0>::from(self.c)),
29 => write!(f, "{} Q", BaseCount::<-1>::from(self.c)),
28 => write!(f, "{} Q", BaseCount::<-2>::from(self.c)),
27 => write!(f, "{} R", BaseCount::<0>::from(self.c)),
26 => write!(f, "{} R", BaseCount::<-1>::from(self.c)),
25 => write!(f, "{} R", BaseCount::<-2>::from(self.c)),
24 => write!(f, "{} Y", BaseCount::<0>::from(self.c)),
23 => write!(f, "{} Y", BaseCount::<-1>::from(self.c)),
22 => write!(f, "{} Y", BaseCount::<-2>::from(self.c)),
21 => write!(f, "{} Z", BaseCount::<0>::from(self.c)),
20 => write!(f, "{} Z", BaseCount::<-1>::from(self.c)),
19 => write!(f, "{} Z", BaseCount::<-2>::from(self.c)),
18 => write!(f, "{} E", BaseCount::<0>::from(self.c)),
17 => write!(f, "{} E", BaseCount::<-1>::from(self.c)),
16 => write!(f, "{} E", BaseCount::<-2>::from(self.c)),
15 => write!(f, "{} P", BaseCount::<0>::from(self.c)),
14 => write!(f, "{} P", BaseCount::<-1>::from(self.c)),
13 => write!(f, "{} P", BaseCount::<-2>::from(self.c)),
12 => write!(f, "{} T", BaseCount::<0>::from(self.c)),
11 => write!(f, "{} T", BaseCount::<-1>::from(self.c)),
10 => write!(f, "{} T", BaseCount::<-2>::from(self.c)),
9 => write!(f, "{} G", BaseCount::<0>::from(self.c)),
8 => write!(f, "{} G", BaseCount::<-1>::from(self.c)),
7 => write!(f, "{} G", BaseCount::<-2>::from(self.c)),
6 => write!(f, "{} M", BaseCount::<0>::from(self.c)),
5 => write!(f, "{} M", BaseCount::<-1>::from(self.c)),
4 => write!(f, "{} M", BaseCount::<-2>::from(self.c)),
3 => write!(f, "{} k", BaseCount::<0>::from(self.c)),
2 => write!(f, "{} h", BaseCount::<0>::from(self.c)),
1 => write!(f, "{} da", BaseCount::<0>::from(self.c)),
0 => write!(f, "{}", BaseCount::<0>::from(self.c)),
-1 => write!(f, "{} d", BaseCount::<0>::from(self.c)),
-2 => write!(f, "{} c", BaseCount::<0>::from(self.c)),
-3 => write!(f, "{} m", BaseCount::<0>::from(self.c)),
-4 => write!(f, "{} m", BaseCount::<-1>::from(self.c)),
-5 => write!(f, "{} m", BaseCount::<-2>::from(self.c)),
-6 => write!(f, "{} µ", BaseCount::<0>::from(self.c)),
-7 => write!(f, "{} µ", BaseCount::<-1>::from(self.c)),
-8 => write!(f, "{} µ", BaseCount::<-2>::from(self.c)),
-9 => write!(f, "{} n", BaseCount::<0>::from(self.c)),
-10 => write!(f, "{} n", BaseCount::<-1>::from(self.c)),
-11 => write!(f, "{} n", BaseCount::<-2>::from(self.c)),
-12 => write!(f, "{} p", BaseCount::<0>::from(self.c)),
-13 => write!(f, "{} p", BaseCount::<-1>::from(self.c)),
-14 => write!(f, "{} p", BaseCount::<-2>::from(self.c)),
-15 => write!(f, "{} f", BaseCount::<0>::from(self.c)),
-16 => write!(f, "{} f", BaseCount::<-1>::from(self.c)),
-17 => write!(f, "{} f", BaseCount::<-2>::from(self.c)),
-18 => write!(f, "{} a", BaseCount::<0>::from(self.c)),
-19 => write!(f, "{} a", BaseCount::<-1>::from(self.c)),
-20 => write!(f, "{} a", BaseCount::<-2>::from(self.c)),
-21 => write!(f, "{} z", BaseCount::<0>::from(self.c)),
-22 => write!(f, "{} z", BaseCount::<-1>::from(self.c)),
-23 => write!(f, "{} z", BaseCount::<-2>::from(self.c)),
-24 => write!(f, "{} y", BaseCount::<0>::from(self.c)),
-25 => write!(f, "{} y", BaseCount::<-1>::from(self.c)),
-26 => write!(f, "{} y", BaseCount::<-2>::from(self.c)),
-27 => write!(f, "{} r", BaseCount::<0>::from(self.c)),
-28 => write!(f, "{} r", BaseCount::<-1>::from(self.c)),
-29 => write!(f, "{} r", BaseCount::<-2>::from(self.c)),
-30 => write!(f, "{} q", BaseCount::<0>::from(self.c)),
-31 => write!(f, "{} q", BaseCount::<-1>::from(self.c)),
-32 => write!(f, "{} q", BaseCount::<-2>::from(self.c)),
-33 => write!(f, "{} q", BaseCount::<-3>::from(self.c)),
-34 => write!(f, "{} q", BaseCount::<-4>::from(self.c)),
-35 => write!(f, "{} q", BaseCount::<-5>::from(self.c)),
-36 => write!(f, "{} q", BaseCount::<-6>::from(self.c)),
-37 => write!(f, "{} q", BaseCount::<-7>::from(self.c)),
-38 => write!(f, "{} q", BaseCount::<-8>::from(self.c)),
-39 => write!(f, "{} q", BaseCount::<-9>::from(self.c)),
-40 => write!(f, "{} q", BaseCount::<-10>::from(self.c)),
-41 => write!(f, "{} q", BaseCount::<-11>::from(self.c)),
-42 => write!(f, "{} q", BaseCount::<-12>::from(self.c)),
-43 => write!(f, "{} q", BaseCount::<-13>::from(self.c)),
-44 => write!(f, "{} q", BaseCount::<-14>::from(self.c)),
-45 => write!(f, "{} q", BaseCount::<-15>::from(self.c)),
-46 => write!(f, "{} q", BaseCount::<-16>::from(self.c)),
-47 => write!(f, "{} q", BaseCount::<-17>::from(self.c)),
-48 => write!(f, "{} q", BaseCount::<-18>::from(self.c)),
-49 => write!(f, "{} q", BaseCount::<-19>::from(self.c)),
-50 => write!(f, "{} q", BaseCount::<-20>::from(self.c)),
-51 => write!(f, "{} q", BaseCount::<-21>::from(self.c)),
-52 => write!(f, "{} q", BaseCount::<-22>::from(self.c)),
-53 => write!(f, "{} q", BaseCount::<-23>::from(self.c)),
-54 => write!(f, "{} q", BaseCount::<-24>::from(self.c)),
-55 => write!(f, "{} q", BaseCount::<-25>::from(self.c)),
-56 => write!(f, "{} q", BaseCount::<-26>::from(self.c)),
-57 => write!(f, "{} q", BaseCount::<-27>::from(self.c)),
-58 => write!(f, "{} q", BaseCount::<-28>::from(self.c)),
-59 => write!(f, "{} q", BaseCount::<-29>::from(self.c)),
-60 => write!(f, "{} q", BaseCount::<-30>::from(self.c)),
-61 => write!(f, "{} q", BaseCount::<-31>::from(self.c)),
-62 => write!(f, "{} q", BaseCount::<-32>::from(self.c)),
-63 => write!(f, "{} q", BaseCount::<-33>::from(self.c)),
-64 => write!(f, "{} q", BaseCount::<-34>::from(self.c)),
-65 => write!(f, "{} q", BaseCount::<-35>::from(self.c)),
-66 => write!(f, "{} q", BaseCount::<-36>::from(self.c)),
-67 => write!(f, "{} q", BaseCount::<-37>::from(self.c)),
-68 => write!(f, "{} q", BaseCount::<-38>::from(self.c)),
-69 => write!(f, "{} q", BaseCount::<-39>::from(self.c)),
-70 => write!(f, "{} q", BaseCount::<-40>::from(self.c)),
-71 => write!(f, "{} q", BaseCount::<-41>::from(self.c)),
-72 => write!(f, "{} q", BaseCount::<-42>::from(self.c)),
-73 => write!(f, "{} q", BaseCount::<-43>::from(self.c)),
-74 => write!(f, "{} q", BaseCount::<-44>::from(self.c)),
-75 => write!(f, "{} q", BaseCount::<-45>::from(self.c)),
-76 => write!(f, "{} q", BaseCount::<-46>::from(self.c)),
-77 => write!(f, "{} q", BaseCount::<-47>::from(self.c)),
-78 => write!(f, "{} q", BaseCount::<-48>::from(self.c)),
-79 => write!(f, "{} q", BaseCount::<-49>::from(self.c)),
-80 => write!(f, "{} q", BaseCount::<-50>::from(self.c)),
-81 => write!(f, "{} q", BaseCount::<-51>::from(self.c)),
-82 => write!(f, "{} q", BaseCount::<-52>::from(self.c)),
-83 => write!(f, "{} q", BaseCount::<-53>::from(self.c)),
-84 => write!(f, "{} q", BaseCount::<-54>::from(self.c)),
-85 => write!(f, "{} q", BaseCount::<-55>::from(self.c)),
-86 => write!(f, "{} q", BaseCount::<-56>::from(self.c)),
-87 => write!(f, "{} q", BaseCount::<-57>::from(self.c)),
-88 => write!(f, "{} q", BaseCount::<-58>::from(self.c)),
-89 => write!(f, "{} q", BaseCount::<-59>::from(self.c)),
-90 => write!(f, "{} q", BaseCount::<-60>::from(self.c)),
-91 => write!(f, "{} q", BaseCount::<-61>::from(self.c)),
-92 => write!(f, "{} q", BaseCount::<-62>::from(self.c)),
-93 => write!(f, "{} q", BaseCount::<-63>::from(self.c)),
-94 => write!(f, "{} q", BaseCount::<-64>::from(self.c)),
-95 => write!(f, "{} q", BaseCount::<-65>::from(self.c)),
-96 => write!(f, "{} q", BaseCount::<-66>::from(self.c)),
-97 => write!(f, "{} q", BaseCount::<-67>::from(self.c)),
-98 => write!(f, "{} q", BaseCount::<-68>::from(self.c)),
-99 => write!(f, "{} q", BaseCount::<-69>::from(self.c)),
-100 => write!(f, "{} q", BaseCount::<-70>::from(self.c)),
-101 => write!(f, "{} q", BaseCount::<-71>::from(self.c)),
-102 => write!(f, "{} q", BaseCount::<-72>::from(self.c)),
-103 => write!(f, "{} q", BaseCount::<-73>::from(self.c)),
-104 => write!(f, "{} q", BaseCount::<-74>::from(self.c)),
-105 => write!(f, "{} q", BaseCount::<-75>::from(self.c)),
-106 => write!(f, "{} q", BaseCount::<-76>::from(self.c)),
-107 => write!(f, "{} q", BaseCount::<-77>::from(self.c)),
-108 => write!(f, "{} q", BaseCount::<-78>::from(self.c)),
-109 => write!(f, "{} q", BaseCount::<-79>::from(self.c)),
-110 => write!(f, "{} q", BaseCount::<-80>::from(self.c)),
-111 => write!(f, "{} q", BaseCount::<-81>::from(self.c)),
-112 => write!(f, "{} q", BaseCount::<-82>::from(self.c)),
-113 => write!(f, "{} q", BaseCount::<-83>::from(self.c)),
-114 => write!(f, "{} q", BaseCount::<-84>::from(self.c)),
-115 => write!(f, "{} q", BaseCount::<-85>::from(self.c)),
-116 => write!(f, "{} q", BaseCount::<-86>::from(self.c)),
-117 => write!(f, "{} q", BaseCount::<-87>::from(self.c)),
-118 => write!(f, "{} q", BaseCount::<-88>::from(self.c)),
-119 => write!(f, "{} q", BaseCount::<-89>::from(self.c)),
-120 => write!(f, "{} q", BaseCount::<-90>::from(self.c)),
-121 => write!(f, "{} q", BaseCount::<-91>::from(self.c)),
-122 => write!(f, "{} q", BaseCount::<-92>::from(self.c)),
-123 => write!(f, "{} q", BaseCount::<-93>::from(self.c)),
-124 => write!(f, "{} q", BaseCount::<-94>::from(self.c)),
-125 => write!(f, "{} q", BaseCount::<-95>::from(self.c)),
-126 => write!(f, "{} q", BaseCount::<-96>::from(self.c)),
-127 => write!(f, "{} q", BaseCount::<-97>::from(self.c)),
-128 => write!(f, "{} q", BaseCount::<-98>::from(self.c)),
}
}
const fn upper_exp() -> &'static str {
match const { EXP } {
0 => "E0",
1 => "E1",
2 => "E2",
3 => "E3",
4 => "E4",
5 => "E5",
6 => "E6",
7 => "E7",
8 => "E8",
9 => "E9",
10 => "E10",
11 => "E11",
12 => "E12",
13 => "E13",
14 => "E14",
15 => "E15",
16 => "E16",
17 => "E17",
18 => "E18",
19 => "E19",
20 => "E20",
21 => "E21",
22 => "E22",
23 => "E23",
24 => "E24",
25 => "E25",
26 => "E26",
27 => "E27",
28 => "E28",
29 => "E29",
30 => "E30",
31 => "E31",
32 => "E32",
33 => "E33",
34 => "E34",
35 => "E35",
36 => "E36",
37 => "E37",
38 => "E38",
39 => "E39",
40 => "E40",
41 => "E41",
42 => "E42",
43 => "E43",
44 => "E44",
45 => "E45",
46 => "E46",
47 => "E47",
48 => "E48",
49 => "E49",
50 => "E50",
51 => "E51",
52 => "E52",
53 => "E53",
54 => "E54",
55 => "E55",
56 => "E56",
57 => "E57",
58 => "E58",
59 => "E59",
60 => "E60",
61 => "E61",
62 => "E62",
63 => "E63",
64 => "E64",
65 => "E65",
66 => "E66",
67 => "E67",
68 => "E68",
69 => "E69",
70 => "E70",
71 => "E71",
72 => "E72",
73 => "E73",
74 => "E74",
75 => "E75",
76 => "E76",
77 => "E77",
78 => "E78",
79 => "E79",
80 => "E80",
81 => "E81",
82 => "E82",
83 => "E83",
84 => "E84",
85 => "E85",
86 => "E86",
87 => "E87",
88 => "E88",
89 => "E89",
90 => "E90",
91 => "E91",
92 => "E92",
93 => "E93",
94 => "E94",
95 => "E95",
96 => "E96",
97 => "E97",
98 => "E98",
99 => "E99",
100 => "E100",
101 => "E101",
102 => "E102",
103 => "E103",
104 => "E104",
105 => "E105",
106 => "E106",
107 => "E107",
108 => "E108",
109 => "E109",
110 => "E110",
111 => "E111",
112 => "E112",
113 => "E113",
114 => "E114",
115 => "E115",
116 => "E116",
117 => "E117",
118 => "E118",
119 => "E119",
120 => "E120",
121 => "E121",
122 => "E122",
123 => "E123",
124 => "E124",
125 => "E125",
126 => "E126",
127 => "E127",
-1 => "E-1",
-2 => "E-2",
-3 => "E-3",
-4 => "E-4",
-5 => "E-5",
-6 => "E-6",
-7 => "E-7",
-8 => "E-8",
-9 => "E-9",
-10 => "E-10",
-11 => "E-11",
-12 => "E-12",
-13 => "E-13",
-14 => "E-14",
-15 => "E-15",
-16 => "E-16",
-17 => "E-17",
-18 => "E-18",
-19 => "E-19",