Re: Pixel Mania



"Mike Williams" <mike@xxxxxxxxxxxxxxxxx> wrote in message
news:OWitWXwiHHA.4496@xxxxxxxxxxxxxxxxxxxxxxx
It wasn't my silly fault. And I certainly wouldn't call someone silly for
making an error, even if it was an error, unless you are yourself totally
flame proof, which in the case of most of us here, including yourself, is
simply not true! You have made many errors yourself, just as all of us
have.

Sure I have. But when I do I'm happy to say it was a silly mistake.

I was checking out some code to help someone out of a problem. He was
peforming some drawing using a simple "pixel by pixel" approach and I
noticed that although he was stepping round a For Next loop using an
integer step value of 1, he was adding the current value of the loop
counter to the coordinates of a smaller image he was also working with. It
just so happened that the code that he was using to set the position of
the image just happened to use a floating point variable, and every time
that variable hit exactly 0.5 the entire smaller image would be drawn into
the background image with each alternate vertical line missing. Okay, so
perhaps he was not wise to use a floating point variable when working with
pixels, but nevertheless he had a problem which only manifested itself
occasionally, and which was causing him great headaches.

Right, he was being a sloppy programmer by using floating point. Surely
using a floating point in a loop has to ring alarm bells. I'm always
suspicious that repetitively adding 1 will give results that are not quite
integers towards the end. I'm suspicious that it won't complete the required
no of loops because the number might not be quite bang on an integer and I'm
suspicious of how it will round. Unexpected results shouldn't really have
been too unexpected here, something was bound to go wrong. He should have
rounded once at the start instead of rounding hundreds of times.

The bankers' rounding of course was what was causing his "alternate
vertical lines" problem, and without the bankers' rounding the image would
have been drawn perfectly.

Not necessarily. The number could have wandered off an integer having 1
repetitively added to it and gone under a rounding limit on some pixels and
not others. Just the limitations of floating point could have made some
values round down while others up if the starting value fell near 0.5 or
something.5.

I suppose you would say that his problem was really the floating point
variable when working with pixels in the way that he was, and I would
agree with you, and that in fact is what I told him. But I didn't tell him
he was silly!

You should have.

If you've got the time to spare perhaps you'd like to travel over here and
tell him yourself? Let me know when you're coming so that he can tell the
night club owner that he'll need to get someone else on the door for that
night :-)

Send me a ticket and I'll be over in a flash. He'll have a good laugh and
agree it was a silly piece of code.

Michael


.