Floats in For/ Next?

Everything else that doesn't fall into one of the other PB categories.
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Floats in For/ Next?

Post by Amiga5k »

Trying out PB4b1 and I'm surprised to see that I still can't do this:

Code: Select all

For a.f = 0 To 40 Step .5
  PrintN(StrF(a))
Next
Yes, there are other ways to do the above, but it would be nice if For Next supported all "core" number types.

Russell
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

For now you can do something like that:

Code: Select all

Macro FFor(var, ffrom, fto, fstep = 1.0)
	var = ffrom - fstep
	While var <= fto - fstep
		var + fstep
EndMacro


x.f = 7.0
FFor(x, 11.5, 13.0, 0.5)
	Debug x
	
Wend
Athlon64 3700+, 1024MB Ram, Radeon X1600
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Floats in For/ Next?

Post by chris319 »

Amiga5k wrote:Trying out PB4b1 and I'm surprised to see that I still can't do this:

Code: Select all

For a.f = 0 To 40 Step .5
  PrintN(StrF(a))
Next
Yes, there are other ways to do the above, but it would be nice if For Next supported all "core" number types.

Russell
I would elevate this to "necessity" from "nicety".
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

personally, i think coding for / next loops with anything but ints is wrong, i'd always suggest to use

Code: Select all

n.f = 0
while n.f < 100
  ...
  n.f = n.f+1.7
wend
but i agree, it's a matter of taste...
( 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... )
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post by Amiga5k »

...Except that every other BASIC dialect currently available has supported floats in For/Next loops for a long time now. ;) (Can't remember if Commodore Basic did or not...) :twisted:

But yeah, a Float can be faked using Macros, functions or what have you. But those miss the point: It probably should have already been in there a while ago. Probably a simple oversight, which is understandable. This is really the only core language functionality I have a problem with, especially now that we finally have the new types for character (.c), double (.d) and quad (.q). And fixed length strings (yay!). And default values for function parameters. And... (Yes, overall PB is looking VERY good now!)

But still no double precision floats :( Maybe next time? :)

Russell
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

.d ?
( 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... )
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> every other BASIC dialect currently available has supported floats in
> For/Next loops for a long time now. ;) (Can't remember if Commodore
> Basic did or not...) :twisted:

It sure did:

Image

;)

Although, I note with interest that it doesn't show "3" as the final print...?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

Well, "3.something" is larger than 3 ;)
<°)))o><²³
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post by walker »

2.8 + 0.2 = 3.0 :roll:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

walker, you're right, but in floats is 2.8 + 0.2 not the same as 3.0 as in float 2.8 might be 2.80000000000000001 and 0.2 might be actually 0.20000000000000001 so the total might be 3.00000000000000002 and that's more than 3...

that's why i never use for next for floats :-)
( 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... )
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Post by chris319 »

personally, i think coding for / next loops with anything but ints is wrong

...Except that every other BASIC dialect currently available has supported floats in For/Next loops for a long time now.
As do C, Perl, and a bunch of languages I never program in. People coming to PB from other languages or dialects of BASIC may see this as a shortcoming and that could hinder the advancement of PB. As for 3.00000000000000002 being > 3, I say leave that to the programmer to deal with, not the language developer.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

blueznl wrote:walker, you're right, but in floats is 2.8 + 0.2 not the same as 3.0 as in float 2.8 might be 2.80000000000000001 and 0.2 might be actually 0.20000000000000001 so the total might be 3.00000000000000002 and that's more than 3...

that's why i never use for next for floats :-)
Each basic do things different.
MSX version of the same thing:
Image
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

If the C64 isn't going to show "3" then it shouldn't show "1" or "2" either.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

blueznl wrote:personally, i think coding for / next loops with anything but ints is wrong, i'd always suggest to use

Code: Select all

n.f = 0
while n.f < 100
  ...
  n.f = n.f+1.7
wend
but i agree, it's a matter of taste...
Why is that? Especially when using Longs in For..Next rather then Int, results in faster code.
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 »

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.
Last edited by netmaestro on Tue Feb 21, 2006 2:00 pm, edited 2 times in total.
BERESHEIT
Post Reply