# math function in AutoIt

## Recommended Posts

I'm a newbie to AutoIt. I've using other staff for a while (VB, Perl) and starting to learning this unique language, thanks to the great efforts put up for this wonderful platform. I plan to migrate some of my perl codes into AutoIt.

I check the function list on Wiki, and, surprisingly, found very limited math functions. I'm talking about simple math functions such as average, standard deviation, percentile.

How can I get these functions for AutoIt?

##### Share on other sites

By writing them. Another option is to search the forum, especially Example Scripts, for things you need. Many of those have already been written and posted. For example: Standard Deviation Calculator

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

I'm a newbie to AutoIt. I've using other staff for a while (VB, Perl) and starting to learning this unique language, thanks to the great efforts put up for this wonderful platform. I plan to migrate some of my perl codes into AutoIt.

I check the function list on Wiki, and, surprisingly, found very limited math functions. I'm talking about simple math functions such as average, standard deviation, percentile.

How can I get these functions for AutoIt?

Simple Average Calc (Took 10 mins to write):

```Global \$aTest[10] = [5,4,6,5,45,32,60,56,2,55]
Global \$aTest2[11] = [10,5,4,6,5,45,32,60,56,2,55]
\$rtn = _Avg(\$aTest, 0)
\$rtn2 = _Avg(\$aTest2, 1)
ConsoleWrite("AVG1: " & \$rtn & " AVG2: " & \$rtn2 & @CRLF)

;_Avg (\$aVars, [\$iIndex = 0])
;\$aVars = Array Input of vars to calc
;\$iIndex = 0 or 1 base array
;Sucess: Returns Average
;Failure: Returns -1 if not an array
Func _Avg (\$aVars, \$iIndex = 0)
Local \$equation = 0
If Not IsArray(\$aVars) Then Return -1
If \$iIndex = 1 Then
For \$n = 1 To UBound(\$aVars) - 1
\$equation += \$aVars[\$n]
Next
Return (\$equation / (UBound(\$aVars) - 1))
Else
For \$n = 0 To UBound(\$aVars) - 1
\$equation += \$aVars[\$n]
Next
Return (\$equation / (UBound(\$aVars)))
EndIf
EndFunc```

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

##### Share on other sites

Thanks for the advices! It at least gives me a sense what is expected when I need them.

When build a slightly complicated algorithm from these home-browned calculations, it may be easily crashed. This will end up spending more time to deal with the 'exceptions', or the "unmaintainable in the end".

Edited by gazai

##### Share on other sites

If you code something yourself and only limit it to personal use, you don't really need to invest too much on robustness and error handling since you know to avoid what will break your script.

Good coding practice and standards makes easy to reuse and maintain code, although "reusability" in AutoIt may not always be in the traditional sense unless you're one of those that like to build and use a personal "include" library.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...