Skip to content

Sample simplification#998

Merged
bbayles merged 6 commits intomore-itertools:masterfrom
rhettinger:sample_simplification
Jun 6, 2025
Merged

Sample simplification#998
bbayles merged 6 commits intomore-itertools:masterfrom
rhettinger:sample_simplification

Conversation

@rhettinger
Copy link
Copy Markdown
Contributor

@rhettinger rhettinger commented Jun 6, 2025

Simplify exp(log(x) / k) to x ** (1 / k).

This looks nicer and is more idiomatic.

It is likely more accurate with only one call to pow() instead of nested calls to exp and log.

Also, it runs a bit faster:

% python3.14 -m timeit -s 'from math import log, exp' -s 'x, k = 0.1234, 23' 'exp(log(x) / k)'
5000000 loops, best of 5: 52.4 nsec per loop

% python3.14 -m timeit -s 'from math import log, exp' -s 'x, k = 0.1234, 23' -s 'rk = 1/k' 'x ** rk'
10000000 loops, best of 5: 30.4 nsec per loop

@bbayles bbayles merged commit 569e0ad into more-itertools:master Jun 6, 2025
6 checks passed
@rhettinger rhettinger deleted the sample_simplification branch June 6, 2025 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants