[Python-bugs-list] [ python-Bugs-721171 ] Tkinter precision loss for doubles

SourceForge.net noreply@sourceforge.net
Tue, 15 Apr 2003 13:54:35 -0700


Bugs item #721171, was opened at 2003-04-14 17:32
Message generated for change (Comment added) made by loewis
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=721171&group_id=5470

Category: Tkinter
Group: None
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Tim Peters (tim_one)
Assigned to: Martin v. Löwis (loewis)
Summary: Tkinter precision loss for doubles

Initial Comment:
DoubleVar.set() (I think) goes thru PyObject_Str(), 
causing precision loss for doubles:

>>> import Tkinter
>>> import math
>>> x = Tkinter.DoubleVar(Tkinter.Tk())
>>> x.set(math.pi)
>>> x.get()
3.1415926535900001   # not good
>>> math.pi
3.1415926535897931   # original precision
>>> eval(str(math.pi))
3.1415926535900001   # reproduces Tk result
>>>

Reported on c.l.py.  Guido says you may know how to 
create a "native" Tcl/Tk double in this case instead.

----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2003-04-15 22:54

Message:
Logged In: YES 
user_id=21627

I have now fixed this for _tkinter.c 1.154, by passing all
.set arguments with the same object type to Tcl, and
retrieving all Tcl variables with their native type.

This causes slight semantic changes (like the one being
reported as a bug here); most notably:

>>> y=Tkinter.StringVar(Tkinter.Tk())
>>> y.set(3.14)
>>> y.get()
3.1400000000000001

So a StringVar may not return a string anymore if .set did
not enter a string. Does that need to get fixed?

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-04-14 17:45

Message:
Logged In: YES 
user_id=6380

If a proper fix using Tcl float/double objects doesn't work
for some reason, maybe the attached fix (_tkinter.fix) would
work?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=721171&group_id=5470