Math functions - Syntax & Usage | AutoHotkey

AutoHotkey

Math Functions

Functions for performing various mathematical operations such as rounding, exponentiation, squaring, etc.

Table of Contents

  • General Math
    • Abs: Returns the absolute value of a number.
    • Ceil: Returns a number rounded up to the nearest integer.
    • Exp: Returns e raised to the Nth power.
    • Floor: Returns a number rounded down to the nearest integer.
    • Log: Returns the logarithm (base 10) of a number.
    • Ln: Returns the natural logarithm (base e) of a number.
    • Max [v1.1.27+]: Returns the highest value of one or more numbers.
    • Min [v1.1.27+]: Returns the lowest value of one or more numbers.
    • Mod: Returns the remainder of a division.
    • Round: Returns a number rounded to N decimal places.
    • Sqrt: Returns the square root of a number.
  • Trigonometry
    • Sin: Returns the trigonometric sine of a number.
    • Cos: Returns the trigonometric cosine of a number.
    • Tan: Returns the trigonometric tangent of a number.
    • ASin: Returns the arcsine (the number whose sine is the specified number) in radians.
    • ACos: Returns the arccosine (the number whose cosine is the specified number) in radians.
    • ATan: Returns the arctangent (the number whose tangent is the specified number) in radians.
  • Error-handling

General Math

Abs

Returns the absolute value of Number.

Value := Abs(Number)

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

MsgBox, % Abs(-1.2) ; Returns 1.2

Ceil

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

Value := Ceil(Number)
MsgBox, % Ceil(1.2)  ; Returns 2
MsgBox, % Ceil(-1.2) ; Returns -1

Exp

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

Value := Exp(N)

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

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

Value := Floor(Number)
MsgBox, % Floor(1.2)  ; Returns 1
MsgBox, % Floor(-1.2) ; Returns -2

Log

Returns the logarithm (base 10) of Number.

Value := Log(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

Returns the natural logarithm (base e) of Number.

Value := Ln(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 [v1.1.27+]

Returns the highest value of one or more numbers.

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

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 [v1.1.27+]

Returns the lowest value of one or more numbers.

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

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

Returns the remainder when Dividend is divided by Divisor.

Value := Mod(Dividend, 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

Returns Number rounded to N decimal places.

Value := Round(Number , N)

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

Returns the square root of Number.

Value := Sqrt(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

Returns the trigonometric sine of Number.

Value := Sin(Number)

Number must be expressed in radians.

MsgBox, % Sin(1.2) ; Returns 0.932039

Cos

Returns the trigonometric cosine of Number.

Value := Cos(Number)

Number must be expressed in radians.

MsgBox, % Cos(1.2) ; Returns 0.362358

Tan

Returns the trigonometric tangent of Number.

Value := Tan(Number)

Number must be expressed in radians.

MsgBox, % Tan(1.2) ; Returns 2.572152

ASin

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

Value := ASin(Number)

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

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

Value := ACos(Number)

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

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

Value := ATan(Number)
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.