For-Next loop with non integer values

Just starting out? Need help? Post your questions and find answers here.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: For-Next loop with non integer values

Post 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. :wink:

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. :D

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
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: For-Next loop with non integer values

Post 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.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: For-Next loop with non integer values

Post 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.
oh... and have a nice day.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: For-Next loop with non integer values

Post 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
( 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
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: For-Next loop with non integer values

Post 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. :wink:

What language are you borrowing (translating) solutions from?
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: For-Next loop with non integer values

Post 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).
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: For-Next loop with non integer values

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

Re: For-Next loop with non integer values

Post 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?
( 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
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: For-Next loop with non integer values

Post 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.
oh... and have a nice day.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: For-Next loop with non integer values

Post 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... :-)
( 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
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: For-Next loop with non integer values

Post 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.
oh... and have a nice day.
krzysztof
User
User
Posts: 16
Joined: Thu Aug 15, 2013 4:36 pm

Re: For-Next loop with non integer values

Post 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

Code: Select all

for i = 1 to 5 step 0.3
next i
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.
Thorium
Addict
Addict
Posts: 1271
Joined: Sat Aug 15, 2009 6:59 pm

Re: For-Next loop with non integer values

Post 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.
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: For-Next loop with non integer values

Post 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 :D
Cheers!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: For-Next loop with non integer values

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply