April 25, 2024, 10:51:05 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


DOUBLE Variable

Started by KenD, January 17, 2008, 11:29:36 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

KenD

Happy New Year to all,

I have been checking loop times with different settings

Can anyone tell me why the following will not compile and run

Def x:DOUBLE
Dim time:DOUBLE
SETPRECISION 7
time = MILLISECS()
FOR x = 1 TO 100000000
'loop only
NEXT x

time = MILLISECS()-time
PRINT "100 million FOR NEXT loops, default STEP: ", LEFT$(STR$(time),8), " miliseconds"
DO:UNTIL INKEY$<>""
===========================
If Def x:INT is used instead of Def x:DOUBLE everything is fine.

Thanks.

Ken.


LarryMc

Simple answer.  From the EBasic Help file:
QuoteFOR

FOR variable = start TO end OPT step
FOR pData = EACH pList {AS type}

Description
The first form of FOR loops from start to end. The second iterates through a linked list.

Parameters
variable - Integer counter variable. Must be of type CHAR, INT, UINT, or WORD
start - Starting count of the loop.
end - Ending count of the loop.
step - Optional stepping value. Defaults to 1.

Some times the answer is just "cause".

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

GWS

It's to do with rounding errors Ken.

Fortran allows you to have:

FOR x = -0.3 to -2.1 step -0.3

but it's fraught with problems - will the loop do 7 iterations or 6?  It would be machine dependent.

So it's much better to simply have loop control variables as integers where precision and rounding is not involved. :)

However, suppose you really want a loop to stride in steps of 0.1 from 0.0 to 1.0 ...  :o?
Just use steps from 0 to 10, and inside your loop, scale the control variable by 0.1 - so your values go 0.0, 0.1, 0.2, ... etc.

You would have found, by the way, that a statements like:


def x:float
def a[10]:int

x = 5.0
a[x] = 3



would also complain.  The array index has to be integer .. you can't have an array element a[1.5]  ...  ::)
It's got to be a[1] or a[2].

Even trying:

a[x] = 3

where x = 7.0000000 wouldn't work, because the value 7 might well be stored as 6.9999999.   Back to the rounding error again.

best wishes, :)

Graham

Tomorrow may be too late ..

LarryMc

Graham

Good explanation of why it's "just cause". ;)

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

KenD

Larry/Graham thanks for the prompt reply.

I am tryong to do comparison tests against HotBasic and was trying to use "DOUBLE" for incrementation.

Again thanks.

Ken.

Ionic Wind Support Team

Using double for the counter variable would be about 4 times slower than using an integer.  Because the loop has to access the FPU each time just to add 1.

Since your loop is using whole numbers it wouldn't make much sense.  However if you really wanted to use a DOUBLE precision FPU variable as a counter then use a while loop

x = 1.0
while x < 100000000.0
x+=1
endwhile

However using something like that as a language comparison wouldn't yeild any useful data.  Since it is entirely processor dependant, even different series of Intel dual core processors will yield different results depending on cache and pipelining.  Some compilers won't even include a "do nothing" for loop when they go through an optimization step.  Ran into that myself with a few C compilers that just ignored a loop I had written.

The best test of a language is throughput.  Open a 10,000 line text file containing random numbers on each line, sort them with a simple bubble sort, print the results to an edit control, then save results to a new file.  Run the test 50 times and compare the results to 4 or 5 other languages creating a graph of total time.  Try and make the code as identical as possible within the confines of the syntax of the languages.

After running the tests use any hand optimizations allowed by each language, see if different orders of code changes the results.  The you will have a good overvirew of a languages throughput capabilities.  The test would exercise memory, strings, loops, file I/O, array handling and FPU access speed if you use decimal numbers in the text file.  I think you would be surprised by the results.

Paul.

Ionic Wind Support Team