Tim Peters added the comment:

Steven, you certainly _can_ ;-) check first whether `r**n == x`, but can you 
prove `r` is the best possible result when it's true?  Offhand, I can't.  I 
question it because it rarely seems to _be_ true (in well less than 1% of the 
random-ish test cases I tried).  An expensive test that's rarely true tends to 
make things slower overall rather than faster.

As to whether a Newton step won't make things worse, that requires seeing the 
exact code you're using.  There are many mathematically equivalent ways to code 
"a Newton step" that have different numeric behavior.  If for some unfathomable 
(to me) reason you're determined to stick to native precision, then - generally 
speaking - the numerically best way to do "a correction step" is to code it in 
the form:

    r += correction  # where `correction` is small compared to `r`

Coding a Newton step here as, e.g., `r = ((n-1)*r + x/r**(n-1))/n` in native 
precision would be essentially useless:  multiple rounding errors show up in 
the result's last few bits, but the last few bits are the only ones we're 
_trying_ to correct.

When `correction` is small compared to `r` in `r += correction`, the rounding 
errors in the computation of `correction` show up in correction's last few 
bits, which are far removed from `r`'s last few bits (_because_ `correction` is 
small compared to `r`).  So that way of writing it _may_ be helpful.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue27761>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to