This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Tkinter precision loss for doubles
Type: Stage:
Components: Tkinter Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: loewis Nosy List: gvanrossum, loewis, tim.peters
Priority: normal Keywords:

Created on 2003-04-14 15:32 by tim.peters, last changed 2022-04-10 16:08 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
_tkinter.fix gvanrossum, 2003-04-14 15:45 Stopgap fix using repr()
Messages (5)
msg15461 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-04-14 15:32
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.
msg15462 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-04-14 15:45
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?
msg15463 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2003-04-15 20:54
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?
msg15464 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-04-15 21:04
Logged In: YES 
user_id=6380

Thanks!

Re StringVar: I think StringVar should coerce the result to
str(), just like IntVar coerces to int(), etc. Maybe
Variable should grow get() method that does no coercion, and
it should no longer be marked as an internal class.

BTW there are a few places in Tkinter docstrings that
promise 0 or 1 where a bool is now returned.
msg15465 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2003-04-16 19:59
Logged In: YES 
user_id=21627

In Tkinter.py 1.172, StringVars now always returns strings
(potentially Unicode); doc strings have been changed to
mention bools.
History
Date User Action Args
2022-04-10 16:08:08adminsetgithub: 38303
2003-04-14 15:32:08tim.peterscreate