Page 3 of 4
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 2:47 pm
by Thorium
PB wrote:> 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
Nice macros, i recreated some of the float count problems with it:
Endless loop because of to small fraction, no actual addition takes place:
Code: Select all
Forr(a.f, 1, 1.1, 0.00000001) ; For a = 1 To 1.1 Step 0.00000001
Debug a ; 4.5, 4.0, 3.5, 3.0, 2.5, [...]
Nextt()
4 iterations happening counting to ~1.15, should iterate 3 times and count to ~1.1:
Code: Select all
Forr(a.f, 1, 1.1, 0.05) ; For a = 1.0 To 1.1 Step 0.05
Debug a ; 4.5, 4.0, 3.5, 3.0, 2.5, [...]
Nextt()
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 4:03 pm
by Thorium
I posted a fix for wrong iteration count on your thread.
It's slow and you still should use integer.
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 4:29 pm
by krzysztof
I thank all of You for detailed and clear explanations!

Now I understand reason to use only integers in the loops.
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 5:02 pm
by BorisTheOld
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 8:28 pm
by krzysztof
PB wrote:...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...
Interesting problem. I compared the speed (cycle time) of two types of loops, using FreeBasic, JustBasic and RunBasic (I apologize for writing about other dialects):
Code: Select all
x = 1
y = 1.0000000001
For i = 1 To 2000000000
x = x * y
Next i
and
Code: Select all
i = 1
x = 1
y = 1.0000000001
While i <= 2000000000
x = x * y
i = i + 1
Wend
Results?
In the case of FreeBasic, the two speeds (cycle time) are the same, about 11 seconds. JustBasic is very slow, so I have to decrease the number down to 2000000, and the 1st speed is 10 s, and 2nd, 13 seconds. In the case of RunBasic, the cycle time of "while wend" loop is also worse than "for to next".
charvista wrote: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.
But when we talk about advantages of "while wend" loop of Pure, we have to compare it to "for to next " loop in other dialects, I think. Pure is easy??? Hmmm, not for me.
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 8:43 pm
by Thorium
krzysztof wrote:
In the case of FreeBasic, the two speeds (cycle time) are the same, about 11 seconds. JustBasic is very slow, so I have to decrease the number down to 2000000, and the 1st speed is 10 s, and 2nd, 13 seconds. In the case of RunBasic, the cycle time of "while wend" loop is also worse than "for to next".
And now check for errors. Whats slows it down is the error correction, which is probably not in place.
In C++ you can do it to and fast also, but you get the same errors i posted some posts up.
It's a trap thats not very Basic like in my opinion.
Oh, i just saw you didnt actualy step fractions, so whats the point of your benchmark?
Re: For-Next loop with non integer values
Posted: Tue Sep 17, 2013 10:35 pm
by charvista
Thorium wrote:Oh, i just saw you didnt actualy step fractions, so whats the point of your benchmark?
Correct, but Thorium did not tell you why. A variable without type that is used for the first time in the program becomes automatically an integer type, that is, with the extension .i, and your variable x has no extension, so it becomes x.i
Then, your variable y, also without extension, so it becomes y.i and even when you wrote y=0.00001 it becomes y=0.
So, the first time that the variable y is used, it must have the extension .d (for a double) or .f (for floating point) so y.d=0.000001 will work.
But then, your variable x cannot be incremented with 0.000001 because it is an integer, thus if you want to have x with decimals, you need to declare it as a double or a floating: x.d or x.f
Once the variable has been declared (with the statement Declare or by adding an extension), you can omit the extension, because it is known.
With code you can understand it much better:
Code: Select all
DisableDebugger
StartTime=ElapsedMilliseconds()
a.i = 1
b.i = 1
For i = 1 To 2000000000
a = a * b
Next i
ElapsedTime=ElapsedMilliseconds()-StartTime
et.d=ElapsedTime/1000
MessageRequester("Timer",Str(ElapsedTime)+" milliseconds, meaning "+StrD(et,2)+" seconds.")
StartTime=ElapsedMilliseconds()
x.d = 1
y.d = 1.0000000001
For i = 1 To 2000000000
x = x * y
Next i
ElapsedTime=ElapsedMilliseconds()-StartTime
et.d=ElapsedTime/1000
MessageRequester("Timer",Str(ElapsedTime)+" milliseconds, meaning "+StrD(et,2)+" seconds.")
StartTime=ElapsedMilliseconds()
i = 1
x.d = 1
y.d = 1.0000000001
While i <= 2000000000 ; NOT decreased to 2000000
x = x * y
i = i + 1
Wend
ElapsedTime=ElapsedMilliseconds()-StartTime
et.d=ElapsedTime/1000
MessageRequester("Timer",Str(ElapsedTime)+" milliseconds, meaning "+StrD(et,2)+" seconds.")
EnableDebugger
On my computer, the first example takes 5 seconds, the second examples takes 7.5 seconds, and the third one 7.5 seconds as well.
As you see, using integers is 33% faster.
krzysztof wrote:But when we talk about advantages of "while wend" loop of Pure, we have to compare it to "for to next " loop in other dialects, I think. Pure is easy??? Hmmm, not for me.
Not necessarily. Every programming language is constructed on base of a solid idea, and the For-Next were implemented only for simple high speed loops. For slower loops (with decimals or with variable step), a While-Wend or Repeat-Until should be used.
As long as there is a way to do what we want to achieve, we may be very happy!

Re: For-Next loop with non integer values
Posted: Wed Sep 18, 2013 10:14 am
by krzysztof
Thank You charvista, for explanation and nice code. I run it on my laptop, and here are results:
1st loop 7.9 sec
2nd loop 18 sec
3rd loop 306 sec !!! No, I am not wrong, it is correct result in my case. First I thought, that my laptop freezed or seemd to calculate it forever, but after 306 seconds I obtained result.
Re: For-Next loop with non integer values
Posted: Wed Sep 18, 2013 10:45 am
by charvista
You are welcome Krzysztof.
Yes, sometimes we have unpredictable results. Of course, it depends of the speed of the computer.
Mine is a desktop with Intel Core i7 running at 3.07GHz, and I have 12 GB RAM installed, to give you an idea, because you like to compare

(but the size of my screen does not affect the calculations

)
Re: For-Next loop with non integer values
Posted: Wed Sep 18, 2013 5:31 pm
by krzysztof
My result for third loop, 306 sec, was not the error of mine. I checked it several times, and I obtained the same results. But now after rebooting the computer, I run this code again, and received better result od 18.5 seconds. The 306 sec may look suspicious, but recently the similar situation was with my code for Mandelbrot set. Once it was printed very quick within a few seconds, but after rebooting of computer, the same code worked so slow, that I have to kill it by "x" button.Strange, but it happens on my laptop with Intel duo core 2.1 GHz, 4 GB RAM and Vista unfortunately.
I run Your code on laptop of my friend (Intel i7, 2.2 GHz, 12 GB RAM) with results: 6.5 s, 8.61 s, and 8.61 s.
Re: For-Next loop with non integer values
Posted: Wed Sep 18, 2013 5:55 pm
by KJ67
Cześć
Just some input on the speeds
1. 5,84 s
2. 7,71 s
3, 7,70 s
Tested on a i7-3610QM @ 2.3GHz & Win7 64 bit.
Re: For-Next loop with non integer values
Posted: Wed Sep 18, 2013 6:58 pm
by charvista
krzysztof wrote:My result for third loop, 306 sec...
... and Vista unfortunately.
Maybe Sir Vista was too tired to count further
Well, I used Vista uninterrupted for about 6 years (which is a very long time), and at the end of the 6th year, the system became really slow. Same for Win2000 and XP I used before. I believe those Windows versions do not always clean the memory properly, I think that's why you got better results after rebooting your computer.
Windows 7 was much better, as computer slowing down became a rare fact. And Windows 8 64-bits (the system I am using today) has even higher performances.
But the fact that you got repeatedly 306 secs only in the While-Wend loop before reboot, shows that there is a big difference with the For-Next loop (in PureBasic).
But when the system is clean, they are both as fast.
Now that the PureBasic's For-Next's strong performance is proven, I hope that you can life with it

Re: For-Next loop with non integer values
Posted: Wed Sep 18, 2013 7:19 pm
by krzysztof
charvista wrote:Thorium wrote:Oh, i just saw you didnt actualy step fractions, so whats the point of your benchmark?
Correct, but Thorium did not tell you why. A variable without type that is used for the first time in the program becomes automatically an integer type, that is, with the extension .i, and your variable x has no extension, so it becomes x.i
I am sorry, I forgot to explain. I gave two examples of general codes (loops) used by me, with no details used by FreeBasic, because I did not want to have troubles again, this time due to speaking about different dialects of Basic and showing appropriate codes in FreeBasic. During such calculations I usually use ULongInt type of integer and Single float.
charvista wrote:Maybe Sir Vista was too tired to count further
Vista is sure not the best product of Microsoft, but I have it, and I am affraid to install Win7: searching for drivers, this will not work, that will not work, driver is bad, wrong version, contact the dealer, dealer is busy, does not know my problem, call the Microsoft directly, contact the supplier again, etc. It is a good idea to test codes using rebooted computer with clear memory.
charvista wrote:Now that the PureBasic's For-Next's strong performance is proven, I hope that you can life with it

I am totally sure to use integers in "For To Next" loops and float in other types of loops

Many thank all of Yoy for nice discussion, clear codes and help!
Re: For-Next loop with non integer values
Posted: Wed Sep 18, 2013 8:44 pm
by Thorium
krzysztof wrote:I am totally sure to use integers in "For To Next" loops and float in other types of loops

I feel like the point of the thread gets lost. It's not about using floats inside a for next or while loop. It's about counting with floats by incrementing them, which you should never do, no matter what type of loop. Languages that do allow that suffer from problems like infinit loops, wrong iteration count, etc. or actualy work correctly but terribly slow because of the corrections (rounded compare) needed to be done, not because of the fact they do float operations.
Re: For-Next loop with non integer values
Posted: Wed Sep 18, 2013 9:05 pm
by krzysztof
Now I completely do not understand what's going on. The problem and previous answers were clear: loop "For To Next" does not work with floats, because it could reduce the accuracy of calculations and the speed of loop. "For To Next" is reserved only for integers. If we want to use floats, we have to use "Repeat Until" or "While Wend" loop, which work correctly with float. That was the explanation with appropriate example codes, and so I understood it.