Page 2 of 2

Posted: Tue Feb 14, 2006 12:42 am
by chris319
The following code:

Code: Select all

for a = 1 to 3 step .2
print a
next
can be rewritten as:

Code: Select all

a = 1
while a <= 3
print a
a = a + .2
wend
but when the counter reaches 3.00000000000000002 the highest number you will see on the screen will be 2.8, so you haven't really solved the problem faced by for ... next loops. This is a flaw of floating point numbers which can potentially affect any language implementation. Still, I don't see this as a reason not to enable floating point in PB for ... next loops.

Posted: Tue Feb 14, 2006 1:00 am
by netmaestro
It makes the looping unreliable! This is proven in the Commodore example. How can that not be a reason to implement them? The makers of PB aren't trying to build junk that can't be relied on to work every time - one loop or a billion loops.

Posted: Tue Feb 14, 2006 1:55 am
by chris319
It makes the looping unreliable! This is proven in the Commodore example. How can that not be a reason to implement them? The makers of PB aren't trying to build junk that can't be relied on to work every time - one loop or a billion loops.
As I said earlier, that should be an issue for the end user (programmer) to deal with, not the language developer. This problem will exist with floats no matter what you come up with as a workaround.

Posted: Tue Feb 14, 2006 2:45 am
by netmaestro
As I said earlier, that should be an issue for the end user (programmer) to deal with, not the language developer. This problem will exist with floats no matter what you come up with as a workaround.
Float counters used in a While-Wend loop are accessible to the programmer and can be "cleaned up" after incrementing and before testing to see if it's still in the range, but the only person who can manipulate the counter at that stage in a For-Next loop is the language developer. Anything the programmer does after "For" and before "Next" isn't going to help.

Posted: Tue Feb 14, 2006 5:47 am
by netmaestro
Here is a C example of what I mean:

Image

Obviously that last line was never intended to display. But it does, all due to floating point errors with rounding.

Posted: Tue Feb 14, 2006 8:00 am
by PB
> Look at the Commodore example. Mostly, it hit it on and the loop worked.
> But in one case, that of 3.0, it missed. It didn't print the 3.0 because it
> added 0.2 to 2.8 and got 3.000001 or something and decided it was >3.0
> rather than =3.0 and considered the loop finished.

Perhaps... but look at this (just to throw a spanner in the works) -- I just changed
the Commodore example to count to 5 instead of 3, and it all worked as expected:

Image

Posted: Tue Feb 14, 2006 8:19 am
by netmaestro
That's a perfect example of why it isn't a good thing: Sometimes it works as expected, but when you least expect it, it won't. It's not reliable.

Posted: Tue Feb 14, 2006 8:19 am
by chris319
Float counters used in a While-Wend loop are accessible to the programmer and can be "cleaned up" after incrementing and before testing to see if it's still in the range, but the only person who can manipulate the counter at that stage in a For-Next loop is the language developer. Anything the programmer does after "For" and before "Next" isn't going to help.

Code: Select all

for a = 1 to 3.2 step .2
print a
if a > 3 then exit for
next

or

for a = 1 to 3.2 step .2
print a
if a > 3 then goto there
next
there:

Posted: Tue Feb 14, 2006 8:19 am
by blueznl
the reason the more! <-- very sloppy english :-)

Posted: Tue Feb 14, 2006 8:23 am
by netmaestro
If you're going to go to the trouble of "cleaning up" the loop counter anyways, why not just use While-Wend?

Posted: Tue Feb 14, 2006 5:56 pm
by chris319
What you do to it beforehand doesn't affect this operation.
Rubbish. Both of those code snippets were tested in QBasic and work exactly as you'd expect them to: a never exceeds 3.

You may be interested in the ANSI/ISO definition of EXIT FOR:
Execution of the exit-for-statement shall cause execution to continue at the line following the next-line of the smallest for-loop in which the exit-for-statement occurs.

Posted: Tue Feb 14, 2006 8:21 pm
by Manol
And this one?:

Code: Select all

i.f=1.00
Repeat
i+0.2
Debug i
Until i>2.8

Posted: Wed Feb 15, 2006 6:25 am
by Amiga5k
chris319 wrote:
It makes the looping unreliable! This is proven in the Commodore example. How can that not be a reason to implement them? The makers of PB aren't trying to build junk that can't be relied on to work every time - one loop or a billion loops.
As I said earlier, that should be an issue for the end user (programmer) to deal with, not the language developer. This problem will exist with floats no matter what you come up with as a workaround.
Yes, Chris319, you're right: It SHOULD be up to the programmer to decide if this is appropriate for their needs. So why not make it available? Most of us already know of the 'shortcomings' of floats (not just in PB, but in almost every other language too), but that doesn't mean we don't want to use them from time to time.

PowerBASIC has a "Currency" type that is a different sort of float, that stores the EXACT value of the float. It's quite a bit slower than regular floats, of course, given the additonal overhead, but when extra accuracy is needed some may be willing to risk it.

Anyway, the point is, it doesn't seem like it would be too hard of a thing to add to PB and I dare say that it would be useful to quite a few (and apparently not useful to some others ;) ). Of course, I can make do without it, but it still would be nice to have.

Russell

Posted: Sat Mar 25, 2006 4:56 am
by va!n
I know this is an old and hot discussed topic! I really like v4 but its sad for such a powerfull language that it does not support any float operations when using FOR/NEXT/STEP. Atm i am trying to code a complex function with some for/next stuff in a tree and normaly i have to use floats to get current results. Now i am trying since over 1 hour to get the source work in pure!

I dont want get in fight about posting again about this... but i think purebasic has become more and more a professionael language and a professional language should support this feature (working/giving correct results like in C/C++)

Atm i would transfer 10 euro by paypal only for this (correct working) feature and you may make a lot people happy with this! :wink:

Posted: Tue Mar 28, 2006 2:08 am
by Amiga5k
Hey, you just gave me an idea! We've all heard of "Pay Per View" on television, right? How about "Pay Per Feature"? How many of us would pay $10US for this feature or that? Maybe this could be something to consider? :twisted:

Russell

p.s. Just kidding, although I do agree that this should have been a part of PB since PB was introduced on the Amiga many years ago (Can't think of a single BASIC that doesn't have it...and that's saying quite a bit!)