Floats in For/ Next?

Everything else that doesn't fall into one of the other PB categories.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
Last edited by netmaestro on Tue Feb 21, 2006 1:59 pm, edited 2 times in total.
BERESHEIT
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
Last edited by netmaestro on Tue Feb 21, 2006 2:04 pm, edited 3 times in total.
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
Last edited by netmaestro on Tue Feb 21, 2006 2:06 pm, edited 2 times in total.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
Last edited by netmaestro on Tue Feb 21, 2006 2:07 pm, edited 2 times in total.
BERESHEIT
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Post 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:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

the reason the more! <-- very sloppy english :-)
Last edited by blueznl on Tue Feb 14, 2006 8:44 am, edited 1 time in total.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

If you're going to go to the trouble of "cleaning up" the loop counter anyways, why not just use While-Wend?
Last edited by netmaestro on Tue Feb 21, 2006 2:09 pm, edited 2 times in total.
BERESHEIT
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Post 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.
Manol
User
User
Posts: 11
Joined: Thu May 05, 2005 1:19 pm
Location: Spain

Post by Manol »

And this one?:

Code: Select all

i.f=1.00
Repeat
i+0.2
Debug i
Until i>2.8
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post 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
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post 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:
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post 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!)
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
Post Reply