[docs] [issue37454] Clarify docs for math.log1p()

Mark Dickinson report at bugs.python.org
Mon Jul 1 03:18:55 EDT 2019

Mark Dickinson <dickinsm at gmail.com> added the comment:

Ah, now I've looked at the script. There's an issue with using `random.random()` to create "small" values for testing, since its result is always an integer multiple of 2**-53. That means in particular that if x = random.random(), then 1 - x is always *exactly* representable (and 1 + x is also exactly representable approximately half of the time), so there's no loss of accuracy in the intermediate step of computing log(1 + x) if x = -random.random().

Here's what I get if I run your script exactly as it stands (Python 3.7.3, macOS 10.14.5)

    mirzakhani:Desktop mdickinson$ python test.py
    Counter({'equal': 51839, 'offset_log': 41988, 'regular_log': 6173})
    Counter({'equal': 93727, 'regular_log': 6273})

But if I replace each `random.random()` call with `1e-3 * random.random()`, in order to test small values of `x`, here's what I get:

    mirzakhani:Desktop mdickinson$ python test.py
    Counter({'offset_log': 99945, 'equal': 55})
    Counter({'offset_log': 99893, 'equal': 107})


Python tracker <report at bugs.python.org>

More information about the docs mailing list