Page 1 of 2

Floats in For/ Next?

Posted: Sun Feb 12, 2006 6:39 am
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

Posted: Sun Feb 12, 2006 12:28 pm
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

Re: Floats in For/ Next?

Posted: Sun Feb 12, 2006 4:44 pm
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".

Posted: Sun Feb 12, 2006 5:39 pm
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...

Posted: Mon Feb 13, 2006 4:13 am
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

Posted: Mon Feb 13, 2006 7:17 am
by blueznl
.d ?

Posted: Mon Feb 13, 2006 7:27 am
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...?

Posted: Mon Feb 13, 2006 11:45 am
by freedimension
Well, "3.something" is larger than 3 ;)

Posted: Mon Feb 13, 2006 7:45 pm
by walker
2.8 + 0.2 = 3.0 :roll:

Posted: Mon Feb 13, 2006 7:54 pm
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 :-)

Posted: Mon Feb 13, 2006 8:15 pm
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.

Posted: Mon Feb 13, 2006 9:06 pm
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

Posted: Mon Feb 13, 2006 9:08 pm
by PB
If the C64 isn't going to show "3" then it shouldn't show "1" or "2" either.

Posted: Mon Feb 13, 2006 9:51 pm
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.

Posted: Mon Feb 13, 2006 10:16 pm
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.