Page 1 of 4
For-Next loop with non integer values
Posted: Sat Dec 12, 2009 2:27 pm
by charvista
In PureBasic it seems that a For-Next loop only work with integer values, that's still true in version 4.40. I wonder why fractions are not allowed, despite the fact it is so basical and can be used very often.
Perhaps there is a workaround for the following code?
Code: Select all
OpenWindow(0, 200, 200, 200, 200, "For-Next Loop with Double", #PB_Window_SystemMenu)
For i.d = 1 To 10.5 Step 0.5
Debug i.d
Next
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 2:34 pm
by ts-soft
Code: Select all
OpenWindow(0, 200, 200, 200, 200, "For-Next Loop with Double", #PB_Window_SystemMenu)
Define.d i = 1
Repeat
i + 0.5
Debug i
Until i = 10.5
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Code: Select all
OpenWindow(0, 200, 200, 200, 200, "For-Next Loop with Double", #PB_Window_SystemMenu)
Define.d i = 1
While i < 10.5
i + 0.5
Debug i
Wend
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 2:37 pm
by RASHAD
Code: Select all
OpenWindow(0, 200, 200, 200, 200, "For-Next Loop with Double", #PB_Window_SystemMenu)
For i = 10 To 105 Step 5
x.f = i/10
Debug x
Next
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 2:59 pm
by charvista
@ts-soft
Thank you, but the code is not complete. It should begin with 1 and 10.5 both values included.
@Rashad
Thank you, looks simple but not universal. When using variables, we don't know if we need to multiply the values with a factor or 10 or 100 or 1000 etc.
The workaround must work exactly like it would have been done with a For-Next loop, with values unknown in advance (could be integer, double, positive, negative, or null)
So far, using the ts-soft code I have this, the Until a=z is universal but not correct as the last value is not printed in Debug.
Code: Select all
; Going up
Define.d a = 1 ; start
Define.d z = 10.5 ; end
Define.d s = 0.5 ; step
Repeat
Debug a
a + s
Until a = z
Debug""
Debug""
; Going down
Define.d a = 8.5 ; start
Define.d z = -2 ; end
Define.d s = -0.5 ; step
Repeat
Debug a
a + s
Until a = z
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 3:03 pm
by Kaeru Gaman
forget Until EQUAL, it will mess up your loops when you got bigger ranges because of the natural incertaincy of floats.
take BIGGER THAN, and you got what you want: last value included and no incertaincy trap.
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 3:06 pm
by charvista
You are right, Kaeru Gaman. Good point.
However, if I use GREATHER THAN then the Going up loop will work, but Going down will not. And I need an universal loop.
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 3:15 pm
by Kaeru Gaman
may I ask what espacially you need it for?
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 3:22 pm
by Kaeru Gaman
PS
the point is, you CAN build a universal loop (see below)
but it looks quite complicated because of much conditions around.
sure, if you want the compiler to provide such a loop, it will look at least the same under the hood if not even more complicated.
most times it would be better to distinguishe the cases when you want to count up or count down, and write seperated loops for it.
here the promised code:
Code: Select all
Define Value.d
Define Stepper.d
Define Limit.d
Define LoopFlag.l
;{ ** FOR **
Value = 1
Limit = 10.5
Stepper = 0.5
EndFlag = 0
If Stepper = 0
EndFlag = 1
EndIf
Repeat
;} ** **
Debug Value
;{ ** NEXT **
Value + Stepper
If Stepper > 0
If Value > Limit
EndFlag = 1
EndIf
ElseIf Stepper < 0
If Value < Limit
EndFlag = 1
EndIf
EndIf
Until EndFlag
;} ** **
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 4:30 pm
by RASHAD
Universal I think
Code: Select all
OpenWindow(0, 200, 200, 200, 200, "For-Next Loop with Double", #PB_Window_SystemMenu)
startv.f = 1.205 ;Only for test
endv.f = 2.834567 ;For test
stepv.f = 0.2045 ;For test
multip.f = 1000000 ;(Up to your upper limit (may be google))
startv = startv * multip
endv = endv * multip
stepv = stepv * multip
For i = startv To endv
x.d = i/multip
Debug x
If stepv > 0
i = i + stepv
Else
i = i - stepv
EndIf
Next
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 4:40 pm
by Kaeru Gaman
@RASHAD
but very, very limited:
your limits for startv and endv is the upper bound of a signed long, 2.147.483.647 = 2^31-1
the bigger your factor multip gets, the closer the limits for your start and end values get.
additionally, using floats and multiplying all with multip, gives the incertaincies a much bigger weight...
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 4:42 pm
by charvista
The idea is to write a procedure that will change the colour gradually (going up when it becomes warmer, and going down when it gets colder).
I wonder if the makers of PureBasic could implement the support of For-Next loops with floats, this should solve everything
In the meanwhile, I tried to write a procedure that will solve it. I think I got it....
PS: the zSgn() function is to get the sign of a number (-1 for negative, 0 for zero, 1 for positive)
Code: Select all
Procedure.i zSgn(Val.d)
If Val.d > 0 : ProcedureReturn 1 : EndIf
If Val.d < 0 : ProcedureReturn -1 : EndIf
ProcedureReturn 0
EndProcedure
Procedure.d zNext(current.d, stop.d, stepping.d)
If stepping.d > 0 ; going up
If current.d <= stop.d
ProcedureReturn current.d + stepping.d
EndIf
ElseIf stepping.d < 0 ; going down
If current.d >= stop.d
ProcedureReturn current.d + stepping.d
EndIf
EndIf
EndProcedure
A.d = 2
Z.d = -2
S.d = -0.16
C.d = A.d ; startpoint
Debug "======== START LOOP"
While C.d * zSgn(S.d) <= Z.d * zSgn(S.d) ; Here I want to perform a For C.d = A.d To Z.d Step S.d
Debug StrD(C.d, 2) ; do my stuff here
C.d = zNext(C.d, Z.d, S.d) : Delay(100)
Wend
Debug "======== END LOOP"
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 4:55 pm
by Kaeru Gaman
The idea is to write a procedure that will change the colour gradually (going up when it becomes warmer, and going down when it gets colder).
I don't really get how you mean it, sorry.
what occurs to me when I think about this is everything else but a loop.
for color gradients I most times use calculative solutions, calculating the color value directly out of the other values.
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 5:01 pm
by RASHAD
@KG
You gave him a good shot ( After Thomas of course )
I gave him another one
So, what is his problem?
Fred can answer him
Anyway Happy new year man (Do't till me it is still 12/12)
Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 5:16 pm
by charvista
The problem is not only the question of colours but also (and especially) some programming loops from other languages that I will convert to PureBasic. And since most other languages support floats in for-next loops, I discovered the non-ability in PureBasic. Hence my question. And I'm trying to make an simple and universal code to replace the missing float support.
The same occurred with the Modulo (the % operator). The modulo does not support float either. But for that I have written a procedure. Here is it:
Code: Select all
Procedure.d zMod(Dividend.d, Divisor.d)
ProcedureReturn Divisor - ((Int(Dividend / Divisor) +1) * Divisor - Dividend)
EndProcedure
It took one hour to write it!

I hope you understand. I'm just making PureBasic more complete and more powerful!

Re: For-Next loop with non integer values
Posted: Sat Dec 12, 2009 5:22 pm
by Kaeru Gaman
@Rashad
also Happy New Year
sure, in the end it is HIS problem, if he wants to code fish or not...
but this also means he will come back in some weeks and ask for new solutions to new problems that result out of that fish.
additionally, I'm just curious what he meant with "going up when it becomes warmer, and going down when it gets colder"
for me that has nothing at all to do with a float loop.
@charvista
I'm just making PureBasic more complete and more powerful!
well, no. your adding some fish to a place where wasn't fish before.
there are REASONS why Modulo does not support floats and the For loop doesn't either.
you can search the bloody forums to find dozens of threads about it.
PS:
porting code from one language to another by translating 1:1 ...
trying to port, doing it is not even possible ... is a really useless idea.
PureBasic is a different language.
if you want to solve tasks, program solutions.
don't copy solutions from other languages, because then they are not longer solutions.