NumDigits returns the number of decimal digits of b.
(b *BigInt)
| 60 | |
| 61 | // NumDigits returns the number of decimal digits of b. |
| 62 | func NumDigits(b *BigInt) int64 { |
| 63 | bl := b.BitLen() |
| 64 | if bl == 0 { |
| 65 | return 1 |
| 66 | } |
| 67 | |
| 68 | if bl <= digitsTableSize { |
| 69 | val := &digitsLookupTable[bl] |
| 70 | // In general, we either have val.digits or val.digits+1 digits and we have |
| 71 | // to compare with the border value. But that's not true for all values of |
| 72 | // bl: in particular, if bl+1 maps to the same number of digits, then we |
| 73 | // know for sure we have val.digits and we can skip the comparison. |
| 74 | // This is the case for about 2 out of 3 values. |
| 75 | if bl < digitsTableSize && digitsLookupTable[bl+1].digits == val.digits { |
| 76 | return val.digits |
| 77 | } |
| 78 | |
| 79 | switch b.Sign() { |
| 80 | case 1: |
| 81 | if b.Cmp(&val.border) < 0 { |
| 82 | return val.digits |
| 83 | } |
| 84 | case -1: |
| 85 | if b.Cmp(&val.nborder) > 0 { |
| 86 | return val.digits |
| 87 | } |
| 88 | } |
| 89 | return val.digits + 1 |
| 90 | } |
| 91 | |
| 92 | n := int64(float64(bl) / digitsToBitsRatio) |
| 93 | var tmp BigInt |
| 94 | e := tableExp10(n, &tmp) |
| 95 | var a *BigInt |
| 96 | if b.Sign() < 0 { |
| 97 | var tmpA BigInt |
| 98 | a = &tmpA |
| 99 | a.Abs(b) |
| 100 | } else { |
| 101 | a = b |
| 102 | } |
| 103 | if a.Cmp(e) >= 0 { |
| 104 | n++ |
| 105 | } |
| 106 | return n |
| 107 | } |
| 108 | |
| 109 | // powerTenTableSize is the magnitude of the maximum power of 10 exponent that |
| 110 | // is stored in the pow10LookupTable. For instance, if the powerTenTableSize |
searching dependent graphs…