Skip to main content

Arithmetic Operators

These operations perform basic arithmetic (add, subtract, divide, etc.) on numeric types (integer and float).

Unary Operators

Absolute (+)

Used to get the absolute version of a numeric value; negative values become positive.

knot
const foo = 5;
const bar = -10;
+foo == 5;
+bar == 10;
+(foo + bar) == 5;
knot
const foo = 5;
const bar = -10;
+foo == 5;
+bar == 10;
+(foo + bar) == 5;

Negate (-)

Used to negate a numeric value; negative values become positive and positive values become negative.

knot
const foo = 5;
const bar = -10;
-foo == -5;
-bar == 10;
-(foo - bar) == -5;
knot
const foo = 5;
const bar = -10;
-foo == -5;
-bar == 10;
-(foo - bar) == -5;

Binary Operators

info

These operators accept asymmetric input types, meaning that an integer can be added directly to a float for example.

Add (+)

This operation adds two numeric values together. If either value is a float then the resulting type is also float otherwise it is integer.

knot
const foo = 5;
const bar = 1.4;
foo + 3 == 8;
foo + bar == 6.4;
knot
const foo = 5;
const bar = 1.4;
foo + 3 == 8;
foo + bar == 6.4;

Subtract (-)

This operation subtracts one numeric value from another. If either value is a float then the resulting type is also float otherwise it is integer.

knot
const foo = 5;
const bar = 8.4;
foo - 2 == 3;
foo - bar == -3.4;
bar - 1.4 == 7.0;
knot
const foo = 5;
const bar = 8.4;
foo - 2 == 3;
foo - bar == -3.4;
bar - 1.4 == 7.0;

Multiply (*)

This operation multiplies two numeric values together. If either value is a float then the resulting type is also float otherwise it is integer.

knot
const foo = 5;
const bar = 1.5;
foo * 2 == 10;
foo * bar == 7.5;
knot
const foo = 5;
const bar = 1.5;
foo * 2 == 10;
foo * bar == 7.5;

Divide (/)

This operation divides one numeric value by another. No matter the type of the left-hand or right-hand values the result is always of type float.

knot
const foo = 6;
const bar = 1.5;
foo / 4 == 1.5;
foo / bar == 4.0;
knot
const foo = 6;
const bar = 1.5;
foo / 4 == 1.5;
foo / bar == 4.0;

Exponentiation (^)

This operation calculates one numeric value to the power of another. No matter the type of the left-hand or right-hand values the result is always of type float.

knot
const foo = 5;
const bar = 1.5;
foo ^ 2 == 25.0;
foo ^ bar == 11.18033989;
knot
const foo = 5;
const bar = 1.5;
foo ^ 2 == 25.0;
foo ^ bar == 11.18033989;