[Tutor] why gtk.Entry does not give me right answers?

Steven D'Aprano steve at pearwood.info
Mon Apr 23 17:08:13 CEST 2012

Lion Chen wrote:
> Hi, All,
> i am trying write a calculator, now normal calculation works ok. but if
> i enter long numbers, for example 333333333333333333 + 7, it gives me
> "333333333333333312".....
> there are 18 "3"s, if the length is more than 17, the calculation will
> be wrong on Windows and Ubuntu.

You are losing precision by converting into a float. Watch:

>>> n = 333333333333333333
>>> n + 7
>>> int(float(n))
>>> int(float(n)+7)

Your integer value 333...333 requires 59 bits to store the entire number, but
Python floats are C doubles, which only have 52 bits available for the numeric
digits. (The other 12 bits encode the sign and exponent.)


You cannot store 3333...333 exactly as a float. The two closest floats are:


so all numbers between them will be rounded up or down.

Some possible solutions:

* Don't try to mix infinite-precision integer arithmetic with finite-precision
  float arithmetic.

* Don't convert to float unless you really need to, e.g. if the string
  contains a decimal point.

* Use the decimal module instead of floats. You still have finite precision,
  but you can choose how many *decimal* places to store instead of having a
  fixed number of *binary* places. (But decimal is much slower.)

By the way, the code that you sent is unreadable. Please make sure that you
send plain text email, and that indentation is not lost. Without indentation,
it is impossible to understand your code.


More information about the Tutor mailing list