It is currently Fri Mar 05, 2021 7:44 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 50 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
 Post subject: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 2:27 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Sep 23, 2008 11:38 pm
Posts: 868
Location: Belgium
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:
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

_________________
- Future conversation forecasting not yet implemented.
- If the future had copied a program from now, they would have called it version -1.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 2:34 pm 
Offline
Always Here
Always Here
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 5756
Location: Berlin - Germany
Code:
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 2:37 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3881
Code:
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

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 2:59 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Sep 23, 2008 11:38 pm
Posts: 868
Location: Belgium
@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:
; 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

_________________
- Future conversation forecasting not yet implemented.
- If the future had copied a program from now, they would have called it version -1.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 3:03 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 19, 2006 1:57 pm
Posts: 4828
Location: Germany
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.

_________________
oh... and have a nice day.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 3:06 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Sep 23, 2008 11:38 pm
Posts: 868
Location: Belgium
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.

_________________
- Future conversation forecasting not yet implemented.
- If the future had copied a program from now, they would have called it version -1.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 3:15 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 19, 2006 1:57 pm
Posts: 4828
Location: Germany
may I ask what espacially you need it for?

_________________
oh... and have a nice day.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 3:22 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 19, 2006 1:57 pm
Posts: 4828
Location: Germany
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:
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
;} **      **

_________________
oh... and have a nice day.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 4:30 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3881
Universal I think

Code:

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

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 4:40 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 19, 2006 1:57 pm
Posts: 4828
Location: Germany
@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...

_________________
oh... and have a nice day.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 4:42 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Sep 23, 2008 11:38 pm
Posts: 868
Location: Belgium
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:
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"

_________________
- Future conversation forecasting not yet implemented.
- If the future had copied a program from now, they would have called it version -1.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 4:55 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 19, 2006 1:57 pm
Posts: 4828
Location: Germany
Quote:
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.

_________________
oh... and have a nice day.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 5:01 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3881
@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)

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 5:16 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Sep 23, 2008 11:38 pm
Posts: 868
Location: Belgium
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:
Procedure.d zMod(Dividend.d, Divisor.d)
    ProcedureReturn Divisor - ((Int(Dividend / Divisor) +1) * Divisor - Dividend)
EndProcedure

It took one hour to write it! :D
I hope you understand. I'm just making PureBasic more complete and more powerful! :D

_________________
- Future conversation forecasting not yet implemented.
- If the future had copied a program from now, they would have called it version -1.


Top
 Profile  
Reply with quote  
 Post subject: Re: For-Next loop with non integer values
PostPosted: Sat Dec 12, 2009 5:22 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 19, 2006 1:57 pm
Posts: 4828
Location: Germany
@Rashad
also Happy New Year :D

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

_________________
oh... and have a nice day.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 50 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye