Jump to content

For Loop issue


Recommended Posts

I'm terribly confused. I've got a simple "for" loop. I changed the increment to .1, and suddenly the loop never counts up to the max. For example,

For $i = 0 To $i = 100 Step .1 
ConsoleWrite($i & @LF) 
Next

outputs this:

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

shouldn't it go from 0 to 100 in .1 steps. ie, after 1 is 1.1 1.2 1.3 and so on until it reaches 100?

Also, it has trouble with negative numbers. ie, using -5 to 5 step 1 it will print

-5

-4

-3

-2

-1

0

and stop at zero?! I'm so confused. I want to count in decimal steps from -5 to 5, but I can't do either, even separately. Am I doing something wrong, or is this simply a limitation of the language?

Link to comment
Share on other sites

  • Moderators

danielmohr91,

Go and read the Help file page for For again and check the syntax. :)

You might find that this runs as you want:

For $i = 0 To 100 Step .1
ConsoleWrite($i & @LF)
Next

Spot the difference. ;)

M23

Edit: But do not be surprised if some of the numbers are not exactly 0.1 apart. That is a limitation of floating point number representation, not an Autoit bug! ;)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Something al little odd about the ouput for me

22.1

22.2

22.3

22.4

22.5000000000001

22.6000000000001

22.7000000000001

22.8000000000001

22.9000000000001

23.0000000000001

23.1000000000001

23.2000000000001

23.3000000000001

23.4000000000001

23.5000000000001

23.6000000000001

23.7000000000001

23.8000000000001

23.9000000000001

24.0000000000001

It does make it back to normal, then goes ski-wiff again.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Something al little odd about the ouput for me

....

It does make it back to normal, then goes ski-wiff again.

Nothing odd about this as this is simply how computers handle floating point numbers (in sum, without complete accuracy).

Please have a look here for more on this subject: What Every Computer Scientist Should Know About Floating-Point Arithmetic

I'm not sure how AutoIt does its for loops internally, but I know that in Java and C, if you are looping with a floating point index such as a double, your stop test should always use relational operator such as >= or <= and not just the equivalence operator, because it's all too easy for one floating point number to not be 100% equal to another even though logic tells you that they should be equal.

Edited by Fubarable
Link to comment
Share on other sites

danielmohr91,

Go and read the Help file page for For again and check the syntax. ;)

You might find that this runs as you want:

For $i = 0 To 100 Step .1
ConsoleWrite($i & @LF)
Next

Spot the difference. :)

M23

Edit: But do not be surprised if some of the numbers are not exactly 0.1 apart. That is a limitation of floating point number representation, not an Autoit bug! :P

Ha he. Thanks man. Wow, I feel like an idiot. I've been teaching myself JAVA and C++, and now coming back to AutoIt is the most unnatural thing. I keep mixing up the syntax. Things like declaring types for variables, and putting a semi-colon after every line lol. Thanks SO much for your help ;)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...