Serhiy Storchaka added the comment: > But why only using 2 digits for log2(10) estimation?
Because the difference between 3 and 3.3 is 10%, and the difference between 3.3 and exact log2(10) is only 1%. Yes, we can use more digits, but the effect of any additional digit is decreased in geometric progression. If use simple and fast formula that avoids integer multiplication overflow "digits + digits // d", the effect of additional precision is virtually vanished. >>> PyLong_SHIFT, _PyLong_DECIMAL_SHIFT = 15, 4 >>> (3 * _PyLong_DECIMAL_SHIFT) // (1 * PyLong_SHIFT - 3 * >>> _PyLong_DECIMAL_SHIFT) 4 >>> (33 * _PyLong_DECIMAL_SHIFT) // (10 * PyLong_SHIFT - 33 * >>> _PyLong_DECIMAL_SHIFT) 7 >>> (33219 * _PyLong_DECIMAL_SHIFT) // (10000 * PyLong_SHIFT - 33219 * >>> _PyLong_DECIMAL_SHIFT) 7 >>> PyLong_SHIFT, _PyLong_DECIMAL_SHIFT = 30, 9 >>> (3 * _PyLong_DECIMAL_SHIFT) // (1 * PyLong_SHIFT - 3 * >>> _PyLong_DECIMAL_SHIFT) 9 >>> (33 * _PyLong_DECIMAL_SHIFT) // (10 * PyLong_SHIFT - 33 * >>> _PyLong_DECIMAL_SHIFT) 99 >>> (332 * _PyLong_DECIMAL_SHIFT) // (100 * PyLong_SHIFT - 332 * >>> _PyLong_DECIMAL_SHIFT) 249 >>> (3321 * _PyLong_DECIMAL_SHIFT) // (1000 * PyLong_SHIFT - 3321 * >>> _PyLong_DECIMAL_SHIFT) 269 >>> (33219 * _PyLong_DECIMAL_SHIFT) // (10000 * PyLong_SHIFT - 33219 * >>> _PyLong_DECIMAL_SHIFT) 290 >>> (332192 * _PyLong_DECIMAL_SHIFT) // (100000 * PyLong_SHIFT - 332192 * >>> _PyLong_DECIMAL_SHIFT) 291 >>> (332192809 * _PyLong_DECIMAL_SHIFT) // (100000000 * PyLong_SHIFT - >>> 332192809 * _PyLong_DECIMAL_SHIFT) 291 The best approximation with minimal denominator is 485/146. >>> PyLong_SHIFT, _PyLong_DECIMAL_SHIFT = 15, 4 >>> (485 * _PyLong_DECIMAL_SHIFT) // (146 * PyLong_SHIFT - 485 * >>> _PyLong_DECIMAL_SHIFT) 7 >>> PyLong_SHIFT, _PyLong_DECIMAL_SHIFT = 30, 9 >>> (485 * _PyLong_DECIMAL_SHIFT) // (146 * PyLong_SHIFT - 485 * >>> _PyLong_DECIMAL_SHIFT) 291 ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25402> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com