I'm working on a 2D-physics engine, and I need a certain variable to be precise only to the hundredths. Are there any methods for basically shaving off all of that unneeded precision?
I have tried things like
"{0:.2f}".format(a)
but that obviously produces a string, not a number.
I'm moving an object based upon its speed, and I want it to(obviously) stop when its value is 0.0, but it keeps going because its speed is really something like 0.0000562351.
-
Round it to the hundreds' place. tutorialspoint.com/python/number_round.htm.Robert Harvey– Robert Harvey2014年06月13日 23:26:13 +00:00Commented Jun 13, 2014 at 23:26
-
1What are you actually trying to achieve? Are you trying to reduce memory consumption? Speed up calculations? I don't know enough about Python internals, but removing precision might not help in the way you want. If you and an explanation of why you want to do this, you will likely get better answers that help you rather than just telling you how to change the value stored in a variable.unholysampler– unholysampler2014年06月13日 23:29:30 +00:00Commented Jun 13, 2014 at 23:29
-
Yeah, that clarification was absolutely necessary, because due to floating-point precision what you're trying will not work.Izkata– Izkata2014年06月14日 00:00:58 +00:00Commented Jun 14, 2014 at 0:00
-
I guess I'll have to find another way, thanks for your insight.FrigidDev– FrigidDev2014年06月14日 00:10:52 +00:00Commented Jun 14, 2014 at 0:10
-
1possible duplicate of What can be done to programming languages to avoid floating point pitfalls?user40980– user409802014年06月14日 00:15:04 +00:00Commented Jun 14, 2014 at 0:15
2 Answers 2
It is typical to compare float
s using a tolerance, rather than by equality. This both avoids some of the issues with floating point arithmetic on computers and allows you to specify an appropriate level of precision. In your case, if you only care about hundredths of a unit:
if abs(speed - limit) < 0.01:
...
For example:
>>> abs(0.0 - 0.0000562351) < 0.01
True
Try using decimal module.
It allows to manipulate precision. With local context you should get enough flexibility.
Example taken from docs:
from decimal import localcontext
with localcontext() as ctx:
ctx.prec = 42 # Perform a high precision calculation
s = calculate_something()
s = +s # Round the final result back to the default precision
with localcontext(BasicContext): # temporarily use the BasicContext
print Decimal(1) / Decimal(7)
print Decimal(355) / Decimal(113)
-
I think your answer is right, but let me suggest you enhance it by showing a snippet on how precision is controlled by the user using
decimal
. (I know it is right there in the documentation, but it would make the answer more self-contained)logc– logc2014年06月24日 08:02:09 +00:00Commented Jun 24, 2014 at 8:02 -