Page 2 of 4
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 6:01 pm
by Demivec
@charvista: here's a general solution that uses a For/Next loop that requires three setup values (vStart,vEnd,vStep) and uses two working values (vEnd2,J). It handles positive and negative step values and will only execute the loop under the same conditions as a typical For/Next because it actually uses a For/Next loop to implement it.
Code: Select all
vStart.d = 1
vEnd.d = 10.5
vStep.d = 0.5
vEnd2 = (vEnd - vStart) / vStep
i.d = vStart - vStep
For j = 0 To vEnd2
i.d + vStep
Debug i
Next
This would still be considered strange fish.
If I could have thought of a good way to name the actual loop-control-variable "j" it could be made into a macro. Then it could be used like this:
Code: Select all
ForFloat(1,10.5,0.5,i.d)
Debug i
Next
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 8:51 pm
by charvista
@ Demivec
Excellent alternative! You are calculating the number of steps necessary to perform the loop, which is always an integer, so this way we can keep the For/Next structure.
I have tried to write your "macro"... :
Code: Select all
Procedure.d Steps(vStart.d, vEnd.d, vStep.d)
s.i = (vEnd - vStart) / vStep
If vStart.d + s.i * vStep.d <= vEnd.d ; if it reaches exactly the vEnd
s.i + 1 ; then add one step
EndIf
ProcedureReturn s.i
EndProcedure
vStart.d = 1
vEnd.d = 10.5
vStep.d = 0.51
j=Steps(vStart.d, vEnd.d, vStep.d)
Debug Str(j)+ " steps"
For x.i = 0 To j-1
Debug vStart.d + x.i * vStep.d
Next
OOPS... Corrected a typo in the above code!
@ Kaeru Gaman
Yes, I agree completely that PureBasic is a language in its own, and should not be compared to other languages. The same applies with spoken languages.
But I believe that we may get the best of every existing language above the existing codes... so, because the For/Next exists in PureBasic, I am just suggesting to enhance it. I'm letting Fred decide on this.
@ All
If PureBasic is not going to support float in the For/Next in the future, then I will use the solution we found together. To all thank you very much for your help.
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 9:34 pm
by Kaeru Gaman
charvista wrote:... so, because the For/Next exists in PureBasic, I am just suggesting to enhance it. I'm letting Fred decide on this.
it was remarked, suggested and discussed quite often. do a forums search.
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 11:10 pm
by blueznl
Code: Select all
For i.d = 1 To 10.5 Step 0.5
Debug i.d
Next
I think ts-soft's solution was a little off, and it should be something like this:
Code: Select all
i.d = 1
While i.d <= 10.5
Debug i.d
i.d = i.d + 0.5
Wend
Re: For-Next loop with non integer values
Posted: Sun Dec 13, 2009 3:48 pm
by Demivec
charvista wrote:@ Demivec
Excellent alternative! You are calculating the number of steps necessary to perform the loop, which is always an integer, so this way we can keep the For/Next structure.
I have tried to write your "macro"... :
I was thinking of a literal macro but a procedure does the job also.
You did well but you left in some unnecessary steps.
Code: Select all
Procedure.i Steps(vStart.d, vEnd.d, vStep.d)
ProcedureReturn (vEnd - vStart) / vStep ;this value is zero based
EndProcedure
vStart.d = 1
vEnd.d = 10.5
vStep.d = 0.51
j=Steps(vStart.d, vEnd.d, vStep.d)
Debug Str(j + 1)+ " steps" ;add 1 because value is zero-based
For x.i = 0 To j
Debug vStart.d + x.i * vStep.d
Next
Doing the multiplication ensures the accuracy. I was going to mention that before but you beat me to the punch.
What language are you borrowing (translating) solutions from?
Re: For-Next loop with non integer values
Posted: Sun Dec 13, 2009 6:42 pm
by charvista
@ Demivec
I am not sure that your correction is correct... When testing your last example, you are making a step too much. You are making 20 steps, with the last one past the limit (10.69 is over 10.50) and that last one is printed in the Debug window, so it is executed, which is wrong (it had to be 19 steps). That's why I made the additional check in the Steps() procedure...
What language are you borrowing (translating) solutions from?
I'm borrowing code from AutoIt, BBx and ProvideX (these last two are called 'Business Basic' languages).
Re: For-Next loop with non integer values
Posted: Mon Dec 14, 2009 5:35 am
by Demivec
charvista wrote:@ Demivec
I am not sure that your correction is correct... When testing your last example, you are making a step too much. You are making 20 steps, with the last one past the limit (10.69 is over 10.50) and that last one is printed in the Debug window, so it is executed, which is wrong (it had to be 19 steps). That's why I made the additional check in the Steps() procedure...
@charvista: It's due to a rounding error that is performed during the automatic conversion from Double to Integer. The automatic conversion rounds to the nearest number, when it instead needs to round down or truncate the decimal.
Here's the corrected procedure:
Code: Select all
Procedure.i Steps(vStart.d, vEnd.d, vStep.d)
ProcedureReturn Int((vEnd - vStart) / vStep) ;this value is zero based
EndProcedure
Re: For-Next loop with non integer values
Posted: Mon Dec 14, 2009 7:06 pm
by blueznl
Code: Select all
i.d = 1
While i.d <= 10.5
Debug i.d
i.d = i.d + 0.5
Wend
I'm a little confused, why go through all the trouble when the above seems to do it rather well? Am I missing something?
Re: For-Next loop with non integer values
Posted: Mon Dec 14, 2009 7:13 pm
by Kaeru Gaman
blueznl wrote: Am I missing something?
indeed!
Code: Select all
i.d = 1
While i.d <= 10.5
Debug i.d
i.d = i.d - 0.5
Wend
he wants some UNIVERSAL code that can cope with ANY value occuring.
just read the whole topic.
Re: For-Next loop with non integer values
Posted: Mon Dec 14, 2009 7:43 pm
by blueznl
Kaeru Gaman wrote:blueznl wrote: Am I missing something?
he wants some UNIVERSAL code that can cope with ANY value occuring.
just read the whole topic.
Euh, a drop-in replacement? Well, that might make sense, except it just may not
What use is a complex structure where you can change all parameters on one line if a simple program structure works just as well, though I admit the paramters are not all on a single line...
This must simply be too difficult for me to understand, I guess...

Re: For-Next loop with non integer values
Posted: Mon Dec 14, 2009 7:56 pm
by Kaeru Gaman
you are perfectly right somehow.
he asked for it, so we gave it to him.
http://www.purebasic.fr/english/viewtop ... 08#p308808
how reasonable his wish is - I doubt since a while.
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 11:38 am
by krzysztof
This problem also intrigued me and wondered. In some Basic's dialects (FreeBasic, JustBasic, LibertyBasic and probably in more) the simple, nice and clear loop, for instance
works fine. But in PureBasic is prohibited. I read about it, but I did not understand answers or not found suitable answer on this forum. Can I ask you to explain why, in contrast to other dialects, Pure does not allow nonintegers in step? If I missed somewhere on this forum the previous answer to that question, please do not be angry with me. It is not easy to find right detail in thousands of posts.
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 12:28 pm
by Thorium
My guess would be it's not as usefull as it seams but comes at a cost of performance.
In every single case i used a for next loop i needed integer. In 95% you need the counter variable as index variable, which is never a fraction, it's allways a integer. For example array index or memory pointer calculation.
For all cases in which i needed fractions i never needed a for next loop but a repeat or a while loop.
Here is another interessting article about the problems of loop counting with fractions:
https://www.securecoding.cert.org/confl ... p+counters
They speak about the inprecision of float numbers and the problems that comes with that.
Most of that problems are fixed by doing a rounded comparision, which takes performance.
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 2:11 pm
by charvista
Cześć Krzysztof, and yes, I posted this thread in 2009 and it is still not possible to use fractions in a For-Next in the current version.
The reason is to allow very high performances in a loop.
Like Thorium said, you need to use an alternative way, like Repeat or While loops.
You are right, almost any other language allow it, but we have to think in PureBasic and never try to "translate" from any other languages. PureBasic is the best & easiest language I ever tested.
However, I am still willing that it comes, because I have some loops that needs fractions.
So I am now asking Fred (the Administrator & Creator of PB) if a separate
ForD=1.5 to 3.5 step 0.5 would not be a good solution, as we have StrD as well......
Enjoy coding in PureBasic, you'll certainly love it
Cheers!
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 2:16 pm
by PB
> Pure does not allow nonintegers in step?
In one of the many other threads it was due to speed and precision.
Basically, you get faster speeds and perfect precision using loops
such as While/Wend or Repeat/Until instead.
Anyway, here's my macro for a For/Next loop that you may like:
http://www.purebasic.fr/english/viewtop ... 40&t=56685