wx.Timer accurate?

From: Tim Roberts <
timr@probo.com>
To: wxPython-users@lists.wxwidgets.org
Date: Thu, 27 Jul 2006 17:09:15 -0700
Subject: Re: wx.Timer accurate?

No. The final result will need to be an integer, anyway; the progress
bar accepts an integer parameter. The integer division is only a

problem because it truncates; by rearranging so the division is the last
operation, the truncation does the minimum amount of damage.

Ok, I’m still a little confused. I understand the difference in the two methods because I ran this code:

def OnTimer(self, event):
    if self.minutes_remaining != 0:
        self.progress.SetValue(self.minutes_passed * 100 / self.time)
        print self.progress.GetValue()
        print self.minutes_passed * (100.0 / self.time)
        self.status.SetLabel('%s minute(s) remaining.' % self.minutes_remaining)
        self.minutes_passed += 1
        self.minutes_remaining -= 1
    else:

        self.timer.Stop()
        self.progress.SetValue(self.minutes_passed * (100.0 / self.time))
        self.status.SetLabel('%s minute(s) have elapsed.' % self.time)
        wx.Sound.Play

(wx.Sound(r’C:\Windows\Media\notify.wav’))

The print statement helps me to see the two different values. So is the point just that the difference between integer and float division is negligible when done without the parentheses? Because somehow float division seems more accurate, and the progress bar’s SetValue() method takes the float result for whatever reason (maybe it truncates it anyway?). It just seems to me that those slight differences in integer and float division should add up eventually and the progress bar won’t fill up all the way (which was my original problem when I had self.minutes_passed * (100 / self.time)).

So my issue, I guess, is that I understand the difference, but I don’t understand why, since there is a difference, they result in the same thing – unless, like I said, the SetValue method is truncating the argument before processing it.

John

···

---------- Forwarded message ----------
On Thu, 27 Jul 2006 14:30:57 -0400, “John Salerno” < > johnjsal@gmail.com> > wrote:

John Salerno wrote:

    From: Tim Roberts < timr@probo.com <mailto:timr@probo.com>>
    To: wxPython-users@lists.wxwidgets.org
    <mailto:wxPython-users@lists.wxwidgets.org>
    Date: Thu, 27 Jul 2006 17:09:15 -0700
    Subject: Re: wx.Timer accurate?

    No. The final result will need to be an integer, anyway; the progress
    bar accepts an integer parameter. The integer division is only a
    problem because it truncates; by rearranging so the division is
    the last
    operation, the truncation does the minimum amount of damage.

Ok, I'm still a little confused. I understand the difference in the two methods because I ran this code:

    def OnTimer(self, event):
        if self.minutes_remaining != 0:
            self.progress.SetValue(self.minutes_passed * 100 / self.time)
            print self.progress.GetValue()
            print self.minutes_passed * (100.0 / self.time)
            self.status.SetLabel('%s minute(s) remaining.' % self.minutes_remaining)
            self.minutes_passed += 1
            self.minutes_remaining -= 1
        else:
            self.timer.Stop()
            self.progress.SetValue(self.minutes_passed * (100.0 / self.time))
            self.status.SetLabel('%s minute(s) have elapsed.' % self.time)
            wx.Sound.Play (wx.Sound(r'C:\Windows\Media\notify.wav'))

The print statement helps me to see the two different values. So is the point just that the difference between integer and float division is negligible when done without the parentheses? Because somehow float division seems more accurate, and the progress bar's SetValue() method takes the float result for whatever reason (maybe it truncates it anyway?). It just seems to me that those slight differences in integer and float division should add up eventually and the progress bar won't fill up all the way (which was my original problem when I had self.minutes_passed * (100 / self.time)).

So my issue, I guess, is that I understand the difference, but I don't understand why, since there *is* a difference, they result in the same thing -- unless, like I said, the SetValue method is truncating the argument before processing it.

In both cases, when minutes_passed==time, the result will be 100 -- except that the floating point one may be slightly off due to the float division. So actually, if your goal is to make sure that the progress bar hits its 100% mark exactly, I'd go with the integer form.

A story: I once had a programmer working for me who wrote this C++ code:

int graylevel = getGraylevel();
if (graylevel/255.0 == 1.0)
{
    doSomethingWithFullBrightness()
}

The code worked perfectly in debug mode, but never worked in release mode. Why? Because in release mode, the compiler decided that multiplication was faster than division, and the code was refactored to:

if (graylevel * (1.0/255.0) == 1.0)

And because of rounding errors with float, the equality test was never true.

I had to give him a lesson in floating point "equality", among other things. :slight_smile:

    Kent

···

    ---------- Forwarded message ----------
    On Thu, 27 Jul 2006 14:30:57 -0400, "John Salerno" < > johnjsal@gmail.com <mailto:johnjsal@gmail.com>> > wrote: