OpenSCAD User Manual/Mathematical Operators

From Wikibooks, open books for an open world
Jump to: navigation, search

Scalar Arithmetical Operators[edit]

The scalar arithmetical operators take numbers as operands and produce a new number.

+ add
- subtract
* multiply
/ divide
% modulo

The "-" can also be used as prefix operator to negate a number.

Relational Operators[edit]

All relational operator take numbers as operands and produce a Boolean value. The equal and not-equal operators can also compare Boolean values.

< less than
<= less equal
== equal
!= not equal
>= greater equal
> greater than

Logical Operators[edit]

All logical operators take Boolean values as operands and produce a Boolean value.

&& Logical AND
|| Logical OR
! Logical NOT

Conditional Operator[edit]

The ?: operator can be used to conditionally evaluate one or another expression. It works like the ?: operator from the family of C-like programming languages.

 ? : Conditional operator
Usage Example:
a=1;
b=2;
c= a==b ? 4 : 5;

If a equals b, then c is set to 4, else c is set to 5.
The part "a==b" must be something that evaluates to a boolean value.

Vector-Number Operators[edit]

The vector-number operators take a vector and a number as operands and produce a new vector.

* multiply all vector elements by number
/ divide all vector elements by number

Vector Operators[edit]

The vector operators take vectors as operands and produce a new vector.

+ add element-wise
- subtract element-wise

The "-" can also be used as prefix operator to element-wise negate a vector.

Vector Dot-Product Operator[edit]

The vector dot-product operator takes two vectors as operands and produces a scalar.

* sum of vector element products

Matrix Multiplication[edit]

Multiplying a matrix by a vector, vector by matrix and matrix by matrix

* matrix/vector multiplication