Confusing math problem
davea at davea.name
Fri Feb 22 03:19:15 CET 2013
On 02/21/2013 05:44 PM, Schizoid Man wrote:
> No, I was aware to be honest. I thought ** was just short hand for
> math.pow(). Since ** is the integer operation
It's an integer operation because you started with two ints. Unlike
math.pow, which converts to floats, whatever you feed it.
> I compared the difference and got a large blob of numbers. To make a
> proper comparison I'll need to compare the base and exponent for which
> the numbers are different rather than the numbers themselves. I'm
> following Dave's suggestion of determining the symmetric difference of
> the sets.
But once you have a few that are just plain way off, it doesn't really
matter whether some others differ in the 17th place. All the other
discussion is interesting, but don't forget the main point, that trying
to represent large integers (over 17 or so digits) as floats is going to
lose precision. That can happen a number of ways, and math.pow is just
one of them.
Floats use a finite precision to store their value, and the radix is
binary, not decimal. So figuring where they start to lose precision is
tricky. If you're doing a calculation where all intermediate values are
integers, you're usually better off sticking with int/long.
There are many other kinds of constraints that come up in programming,
and Python usually has an answer for each. But in a machine of finite
size, and when we care at least a little about performance, we
frequently have to pick our algorithm, our set of functions, and our
data format carefully.
Someone else has mentioned the decimal package and the fractions. Each
of those has a lot to offer in specific situations. But none is a panacea.
More information about the Python-list