decimalEandM computes and returns the exponent E and mantissa M for d. The mantissa is a base-100 representation of the value. The exponent E determines where to put the decimal point. Each centimal digit of the mantissa is stored in a byte. If the value of the centimal digit is X (hence X>=0 and
(d *apd.Decimal, tmp []byte)
| 89 | // point, then the exponent E is the power of one hundred by which one must |
| 90 | // multiply the mantissa to recover the original value. |
| 91 | func decimalEandM(d *apd.Decimal, tmp []byte) (int, []byte) { |
| 92 | addedZero := false |
| 93 | if cap(tmp) > 0 { |
| 94 | tmp = tmp[:1] |
| 95 | tmp[0] = '0' |
| 96 | addedZero = true |
| 97 | } |
| 98 | tmp = d.Coeff.Append(tmp, 10) |
| 99 | if !addedZero { |
| 100 | tmp = append(tmp, '0') |
| 101 | copy(tmp[1:], tmp[:len(tmp)-1]) |
| 102 | tmp[0] = '0' |
| 103 | } |
| 104 | |
| 105 | // The exponent will be the combination of the decimal's exponent, and the |
| 106 | // number of digits in the big.Int. |
| 107 | e10 := int(d.Exponent) + len(tmp[1:]) |
| 108 | |
| 109 | // Strip off trailing zeros in big.Int's string representation. |
| 110 | for tmp[len(tmp)-1] == '0' { |
| 111 | tmp = tmp[:len(tmp)-1] |
| 112 | } |
| 113 | |
| 114 | // Convert the power-10 exponent to a power of 100 exponent. |
| 115 | var e100 int |
| 116 | if e10 >= 0 { |
| 117 | e100 = (e10 + 1) / 2 |
| 118 | } else { |
| 119 | e100 = e10 / 2 |
| 120 | } |
| 121 | // Strip the leading 0 if the conversion to e100 did not add a multiple of |
| 122 | // 10. |
| 123 | if e100*2 == e10 { |
| 124 | tmp = tmp[1:] |
| 125 | } |
| 126 | |
| 127 | // Ensure that the number of digits is even. |
| 128 | if len(tmp)%2 != 0 { |
| 129 | tmp = append(tmp, '0') |
| 130 | } |
| 131 | |
| 132 | // Convert the base-10 'b' slice to a base-100 'm' slice. We do this |
| 133 | // conversion in place to avoid an allocation. |
| 134 | m := tmp[:len(tmp)/2] |
| 135 | for i := 0; i < len(tmp); i += 2 { |
| 136 | accum := 10*int(tmp[i]-'0') + int(tmp[i+1]-'0') |
| 137 | // The bytes are encoded as 2n+1. |
| 138 | m[i/2] = byte(2*accum + 1) |
| 139 | } |
| 140 | // The last byte is encoded as 2n+0. |
| 141 | m[len(m)-1]-- |
| 142 | return e100, m |
| 143 | } |
| 144 | |
| 145 | // encodeEandM encodes the exponent and mantissa, appending the encoding to a byte buffer. |
| 146 | // |
no test coverage detected
searching dependent graphs…