Modify

Opened 12 years ago

Closed 12 years ago

#2809 closed Bug (No Bug)

inconsistent work of the For...To...Step...Next loop

Reported by: anonymous Owned by:
Milestone: Component: AutoIt
Version: 3.3.12.0 Severity: None
Keywords: Cc:

Description

In the following example the loop doesn't work as is expected. First loop (For $i=0 To 1 Step 0.1) runs eleven times, as it should, but the second one (For $i=1 To 2 Step 0.1) runs only ten times. Doesn't seem quite right, does it.
Such error also occurs when using some other non-integer values for the Step, and also when difference between X and Y in {For $i=X To Y Step <non-integer>} is bigger than 1(one).

$c=0
ConsoleWrite('from 0 to 1 step 0.1'&@cr)
for $i=0 to 1 step 0.1
	ConsoleWrite($i& ', ')
	$c+=1
next
ConsoleWrite(@cr&'counter = '&$c&@cr&@cr)

$c=0
ConsoleWrite('from 1 to 2 step 0.1'&@cr)
for $i=1 to 2 step 0.1
	ConsoleWrite($i& ', ')
	$c+=1
next
ConsoleWrite(@cr&'counter = '&$c&@cr&@cr)

Attachments (0)

Change History (1)

comment:1 by jchd18, 12 years ago

Resolution: No Bug
Status: newclosed

Such For loops should never be used with step value a floating point number that can't be represented exactly in a double.

This is nothing new: fixed-size binary floating point (e.g. IEEE754) often causes inaccuracies, whatever language the program uses.

To keep away from such issues, always use integers in For loop parameters and scale back index values before use inside the loop.

Modify Ticket

Action
as closed The ticket will remain with no owner.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.