Modify

Opened 10 years ago

Closed 10 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 Changed 10 years ago by jchd18

  • Resolution set to No Bug
  • Status changed from new to closed

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.

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.