exp int // exponent
}
+// at returns the i'th mantissa digit, starting with the most significant digit at 0.
+func (d *decimal) at(i int) byte {
+ if 0 <= i && i < len(d.mant) {
+ return d.mant[i]
+ }
+ return '0'
+}
+
// Maximum shift amount that can be done in one pass without overflow.
// A Word has _W bits and (1<<maxShift - 1)*10 + 9 must fit into Word.
const maxShift = _W - 4
}
}
-// Possibly optimization: The current implementation of nat.string takes
-// a charset argument. When a right shift is needed, we could provide
-// "\x00\x01...\x09" instead of "012..9" (as in nat.decimalString) and
-// avoid the repeated +'0' and -'0' operations in decimal.shr (and do a
-// single +'0' pass at the end).
-
// shr implements x >> s, for s <= maxShift.
func shr(x *decimal, s uint) {
// Division by 1<<s using shift-and-subtract algorithm.
// Now we can figure out the minimum number of digits required.
// Walk along until d has distinguished itself from upper and lower.
for i, m := range d.mant {
- l := byte('0') // lower digit
- if i < len(lower.mant) {
- l = lower.mant[i]
- }
- u := byte('0') // upper digit
- if i < len(upper.mant) {
- u = upper.mant[i]
- }
+ l := lower.at(i)
+ u := upper.at(i)
// Okay to round down (truncate) if lower has a different digit
// or if lower is inclusive and is exactly the result of rounding
if prec > 0 {
buf = append(buf, '.')
for i := 0; i < prec; i++ {
- ch := byte('0')
- if j := d.exp + i; 0 <= j && j < len(d.mant) {
- ch = d.mant[j]
- }
- buf = append(buf, ch)
+ buf = append(buf, d.at(d.exp+i))
}
}