Math Functions

AutoHotkey

Math Functions

Quick Reference:

General Math

Abs Absolute value

Value := Abs(Number)

Returns the absolute value of Number.

The return value is the same type as Number (integer or floating point).

MsgBox, % Abs(-1.2) ; Returns 1.2

Ceil Rounding up

Value := Ceil(Number)

Returns Number rounded up to the nearest integer (without any .00 suffix).

MsgBox, % Ceil(1.2)  ; Returns 2
MsgBox, % Ceil(-1.2) ; Returns -1

Exp Exponential

Value := Exp(N)

Returns e (which is approximately 2.71828182845905) raised to the Nth power.

N may be negative and may contain a decimal point. To raise numbers other than e to a power, use the ** operator.

MsgBox, % Exp(1.2) ; Returns 3.320117

Floor Rounding down

Value := Floor(Number)

Returns Number rounded down to the nearest integer (without any .00 suffix).

MsgBox, % Floor(1.2)  ; Returns 1
MsgBox, % Floor(-1.2) ; Returns -2

Log Decimal logarithm

Value := Log(Number)

Returns the logarithm (base 10) of Number.

The result is formatted as floating point. If Number is negative, an empty string is returned.

MsgBox, % Log(1.2) ; Returns 0.079181

Ln Natural logarithm

Value := Ln(Number)

Returns the natural logarithm (base e) of Number.

The result is formatted as floating point. If Number is negative, an empty string is returned.

MsgBox, % Ln(1.2) ; Returns 0.182322

Max Highest value [v1.1.27+]

Value := Max(Number1 [, Number2, ...])

Returns the highest value of one or more numbers.

If one of the input values is non-numeric, an empty string is returned.

MsgBox, % Max(2.11, -2, 0) ; Returns 2.11

You can also specify a variadic parameter to compare multiple values within an array. For example:

array := [1, 2, 3, 4]
MsgBox, % Max(array*) ; Returns 4

Min Lowest value [v1.1.27+]

Value := Min(Number1 [, Number2, ...])

Returns the lowest value of one or more numbers.

If one of the input values is non-numeric, an empty string is returned.

MsgBox, % Min(2.11, -2, 0) ; Returns -2

You can also specify a variadic parameter to compare multiple values within an array. For example:

array := [1, 2, 3, 4]
MsgBox, % Min(array*) ; Returns 1

Mod Remainder after division (Modulo)

Value := Mod(Dividend, Divisor)

Returns the remainder when Dividend is divided by Divisor.

The sign of the result is always the same as the sign of the first parameter. If either input is a floating point number, the result is also a floating point number. If the second parameter is zero, the function yields a blank result (empty string).

MsgBox, % Mod(7.5, 2) ; Returns 1.5 (2 x 3 + 1.5)

Round Rounding

Value := Round(Number [, N])

Returns Number rounded to N decimal places.

If N is omitted or 0, Number is rounded to the nearest integer:

MsgBox, % Round(3.14)    ; Returns 3

If N is positive number, Number is rounded to N decimal places:

MsgBox, % Round(3.14, 1) ; Returns 3.1

If N is negative, Number is rounded by N digits to the left of the decimal point:

MsgBox, % Round(345, -1) ; Returns 350
MsgBox, % Round(345, -2) ; Returns 300

Unlike Transform Round, the result has no .000 suffix whenever N is omitted or less than 1. [v1.0.44.01+]: A value of N greater than zero displays exactly N decimal places rather than obeying SetFormat. To avoid this, perform another math operation on Round()'s return value; for example: Round(3.333, 1)+0.

Sqrt Square root

Value := Sqrt(Number)

Returns the square root of Number.

The result is formatted as floating point. If Number is negative, the function yields a blank result (empty string).

MsgBox, % Sqrt(16) ; Returns 4

Trigonometry

Note: To convert a radians value to degrees, multiply it by 180/pi (approximately 57.29578). To convert a degrees value to radians, multiply it by pi/180 (approximately 0.01745329252). The value of pi (approximately 3.141592653589793) is 4 times the arctangent of 1.

Sin Sine

Value := Sin(Number)

Returns the trigonometric sine of Number.

Number must be expressed in radians.

MsgBox, % Sin(1.2) ; Returns 0.932039

Cos Cosine

Value := Cos(Number)

Returns the trigonometric cosine of Number.

Number must be expressed in radians.

MsgBox, % Cos(1.2) ; Returns 0.362358

Tan Tangent

Value := Tan(Number)

Returns the trigonometric tangent of Number.

Number must be expressed in radians.

MsgBox, % Tan(1.2) ; Returns 2.572152

ASin Arcsine

Value := ASin(Number)

Returns the arcsine (the number whose sine is Number) in radians.

If Number is less than -1 or greater than 1, the function yields a blank result (empty string).

MsgBox, % ASin(0.2) ; Returns 0.201358

ACos Arccosine

Value := ACos(Number)

Returns the arccosine (the number whose cosine is Number) in radians.

If Number is less than -1 or greater than 1, the function yields a blank result (empty string).

MsgBox, % ACos(0.2) ; Returns 1.369438

ATan Arctangent

Value := ATan(Number)

Returns the arctangent (the number whose tangent is Number) in radians.

MsgBox, % ATan(1.2) ; Returns 0.876058

Error-Handling

Invalid operations such as divide by zero generally yield a blank result (empty string).

Abs, Max, Min and Mod return an empty string if any of their incoming parameters are non-numeric. Most math functions do not perform strict type-checking, so may treat non-numeric values as zero or another number. For example, Round("1.0foo") produces 1. However, this is expected to change in AutoHotkey v2.