@@ -19,7 +19,7 @@ impl Mantissa {
1919 /// Convert `self` to an array of decimal digits with divide and conquer algorithm.
2020 /// `l` is the number of decimal digits in the input ceiled to a power of 10.
2121 /// `p` is the current depth of `tenpowers` for the given `input`.
22- /// `most_significant` true if `self` contains the most significant part.
22+ /// `most_significant` true if `self` contains the most significant part (ignores leading zeroes if ture) .
2323 pub ( crate ) fn conv_to_dec (
2424 & mut self ,
2525 l : usize ,
@@ -71,16 +71,20 @@ impl Mantissa {
7171 let mut q = Mantissa :: from_word_buf ( q) ;
7272 let mut r = Mantissa :: from_word_buf ( r) ;
7373
74+ let l2 = l / 2 ;
75+
7476 if q. is_zero ( ) {
75- let part1 = Self :: conv_to_dec ( & mut r, l / 2 , tenpowers, p - 1 , most_significant) ?;
77+ let part1 = Self :: conv_to_dec ( & mut r, l2 , tenpowers, p - 1 , most_significant) ?;
7678
7779 Ok ( part1)
7880 } else {
79- let mut part1 = Self :: conv_to_dec ( & mut r, l / 2 , tenpowers, p - 1 , false ) ?;
80- let mut part2 =
81- Self :: conv_to_dec ( & mut q, l / 2 , tenpowers, p - 1 , most_significant) ?;
81+ let mut part1 = Self :: conv_to_dec ( & mut r, l2, tenpowers, p - 1 , false ) ?;
82+ let mut part2 = Self :: conv_to_dec ( & mut q, l2, tenpowers, p - 1 , most_significant) ?;
8283
83- part2. try_reserve_exact ( part1. len ( ) ) ?;
84+ part2. try_reserve_exact ( l2) ?;
85+ if part1. len ( ) < l2 {
86+ part2. resize ( part2. len ( ) + l2 - part1. len ( ) , 0 ) ;
87+ }
8488 part2. append ( & mut part1) ;
8589
8690 Ok ( part2)
@@ -134,7 +138,7 @@ impl Mantissa {
134138 debug_assert ! ( input[ 0 ] != 0 ) ;
135139
136140 let mut chunks = Vec :: new ( ) ;
137- chunks. try_reserve_exact ( ( input. len ( ) + WORD_TENPOWER_LEN - 1 ) / WORD_TENPOWER_LEN ) ?;
141+ chunks. try_reserve_exact ( input. len ( ) . div_ceil ( WORD_TENPOWER_LEN ) ) ?;
138142
139143 let mut word: Word = 0 ;
140144 let mut i = 0 ;
@@ -321,7 +325,7 @@ mod tests {
321325 assert_eq ! ( ret, expected) ;
322326 } ;
323327
324- /* let tst = [0x9999999999999999, 0x9999999999999999, 0x9999999999999999 ];
328+ /* let tst = [0, 15336237721486753792, 13000820969757097588, 12856862479722330362, 1520271299530698982, 744111004532756610, 13398299208874459061, 17516404478540231373, 3742450158296248731, 2601219928155938017, 8664737567992370843, 17634481686646146565, 12526671041338573227, 4683828805702906964, 8213244418837960360, 17723613685701911250, 13242562882371522762, 6031074778335529103 ];
325329 let mut input = WordBuf::new(tst.len()).unwrap();
326330 input.copy_from_slice(&tst);
327331 test_input(input);
0 commit comments