Loss of precision / data corruption with Tkinter.DoubleVar ?!

David Lees abcdebl2nonspammy at verizon.net
Fri Apr 11 22:25:28 CEST 2003


Lonnie Princehouse wrote:
> Greetings, 
> 
> I stumbled across this curiosity yesterday:
> 
> 
>>>>x = 0.33333333333333098
>>>>x
> 
> 0.33333333333333098     
> 
>>>>import Tkinter
>>>>y = Tkinter.DoubleVar(Tkinter.Tk())
>>>>y.set(x)
>>>>y.get()
> 
> 0.33333333333300003  
> 
> What the....?
> 
> Sure, it's only on the order of 10e-13 delta, 
> but why is there any difference at all??
> 
> 
> I hit this because at some point I was trying to do:
> 
> y.set(x)
> .
> .
> .
> if y.get() == x:
>     ...
> 
> and of course it wasn't working properly!
> 
> I have tried this on Python 2.2 for Win32 and Linux/x86
> 
> 
> 
> 
> -Lonnie Princehouse

See:  http://www.python.org/doc/current/tut/node14.html
I do not know what goes on inside Tkinter when you do a 'set', but it is 
bad practice to do the kind of exact comparison in your 'if' statement 
on floating point numbers.

David Lees






More information about the Python-list mailing list