-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
True division of integers could be more accurate #46136
Comments
Division of two longs can produce results that are needlessly >>> from __future__ import division
>>> 10**40/10**39
10.000000000000002 The correct result is, of course, 10.0, which is exactly representable The attached snippet of Python code shows that things don't have to be |
How fast is your algorithm compared to the current algorithm and where |
It would be easy and safe to just use a/b = float(a)/float(b) if both a and b are less For large integers the algorithm I posted should run in time linear in the number of To get proper timings I'd have to code this up properly. I'll do this, unless there's |
Mark Dickinson wrote:
Why don't you ask Tim? He is the right person for the discussion. I'm Christian |
Tim: is this worth fixing? |
A related problem is that float(n) isn't always correctly rounded for an integer >>> n = 2**68 + 2**16 - 1
>>> float(n)
2.9514790517935283e+20 Here the difference between float(n) and the true value of n is around 0.99998 I don't regard this as terribly serious: from looking at the code, I *think* In contrast, the division of two integers can produce results that are up to 3.5 |
To my mind, the inaccurate result is a bug that should be fixed. Note: (3.0a3)
>>> 10e40/10e39
10.0 The rationale for the division change is that (as far as reasonably I have not looked at either algorithm, but if long/long started by |
Here's a patch that fixes the rounding of integer division. It includes a |
An example of a case that's almost 3.5 ulps out (Python 2.6): Python 2.6.2 (r262:71600, Jul 8 2009, 09:56:31)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import division
>>> m = 295147931372582273023
>>> n = 295147932265116303360
>>> m/n
0.99999999697597697 The correctly rounded result would be the float given by |
Stealing this from Tim, with the intention of acting on it in the next |
Here's an updated patch, against py3k. On my machine, a/b is a touch faster with this patch when abs(a), Changing versions to 2.7 and 3.2, but I'm mostly aiming for 3.2. It may not be worth backporting to 2.7, |
Fixed in r77062 (trunk), r77063 (py3k). |
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
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: