Add math.ulp(x): unit in the last place #83491
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = None closed_at = <Date 2020-01-13.11:47:40.328> created_at = <Date 2020-01-12.12:33:34.419> labels = ['library', '3.9'] title = 'Add math.ulp(x): unit in the last place' updated_at = <Date 2020-01-15.22:50:31.240> user = 'https://github.com/vstinner'
activity = <Date 2020-01-15.22:50:31.240> actor = 'tim.peters' assignee = 'none' closed = True closed_date = <Date 2020-01-13.11:47:40.328> closer = 'vstinner' components = ['Library (Lib)'] creation = <Date 2020-01-12.12:33:34.419> creator = 'vstinner' dependencies =  files =  hgrepos =  issue_num = 39310 keywords = ['patch'] message_count = 14.0 messages = ['359846', '359859', '359882', '359883', '359891', '359898', '359927', '359929', '359937', '359938', '359939', '359940', '360032', '360082'] nosy_count = 8.0 nosy_names = ['tim.peters', 'brett.cannon', 'rhettinger', 'mark.dickinson', 'vstinner', 'stutzbach', 'steven.daprano', 'miss-islington'] pr_nums = ['17965', '17982', '17994'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue39310' versions = ['Python 3.9']
The text was updated successfully, but these errors were encountered:
In bpo-39288, I added math.nextafter(x, y) function. I propose to now add math.ulp() companion function.
Examples from tests of my PR:
Unit in the last place:
In numpy, I found two references to ULP:
Attached PR implements math.ulp(x).
See also math.isclose() and its PEP-485 which mentions ULP (Unit in the Last Place)... in the "Inappropriate uses" section :-)
Extract of an old python-ideas discussion on adding nextafter():
"It's also a little weird to jump from nextafter to isclose, since AFAIK
The math.ulp() documentation explicitly says: https://docs.python.org/dev/library/math.html#math.ulp
ULP stands for “Unit in the Last Place”.
The term "ulp" is commonly used when talking about IEEE 754 floating point numbers. It is used in numpy and Java for example.
test_math.py already had an ulp() function which was a pure Python implementation.
The term is also commonly used in math articles.
If you don't know the term "ulp", it's likely a *good thing*. You didn't have to suffer with rounding issues :-D
I hear what you are saying, but "ulp" is the standard term of art. Sure
Mathematics and numeric programming are rife with short names that are
to mention just a few. "ulp" is a technical, and subtle, concept to
At least ulp is a TLA from English, unlike (say) "sine" which ultimately
It's a good point. I guess we have a choice between using the domain-specific standard-ish name (which should be immediately meaningful to experts, but doesn't give much of a hint to non-experts) or using something more descriptive (which then risks confusing experts until they figure out "oh, that's just ulp").
There's also the option of spelling it out as "unit_in_last_place", but I'm not sure that benefits anyone.
For meaningful descriptive names, "float_resolution" or "gap_to_next" are about the best I can come up with. "precision" is too ambiguous.
NumPy has "numpy.spacing". But this exhibits exactly the trap of not using the "ulp" name: on a first glance, I incorrectly decided that NumPy didn't implement a ulp function. Then, having found
If we can get people can coalesce around a preferred alternative name, we could consider changing this.
In a different context, spelling out some variant of Hypertext_Transfer_Protocol would be as wrong-headed to avoid the "cryptic" http.