# OpenSCAD User Manual/Mathematical Operators

The text in its current form is incomplete. |

### 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]

Relational operators produce a Boolean result from two operands.

< | less than |

<= | less equal |

== | equal |

!= | not equal |

>= | greater equal |

> | greater than |

If both operands are simple numbers, the meaning is self-evident.

If both operands are strings, alphabetical sorting determines equality and order. E.g., "ab" > "aa" > "a".

If both operands are Booleans, *true* > *false*. If one operand is Boolean, the other operand is converted to Boolean before the comparison is made.

If both operands are vectors, OpenSCAD performs an element-by-element comparison and can only result in *true* if the vectors are equal in size and each and every pair of elements results in *true* upon the comparison. Otherwise, *false* is returned.

Vectors of different sizes are treated as unequal for '==' and '!=' operators, and always result in *false* for '>', '>=', '<' and '<=' operators. In fact the same principle applies for all comparison between dissimilar types of operand, e.g. comparing a string with a number.

Note that [1] ≠ 1.

*undef* doesn't equal anything but *undef*. *undef* compares ('>' etc.) anything result in *false*.

*nan* doesn't equal anything. See Numbers.

### Logical Operators[edit]

All logical operators take Booleans as operands and produce a Boolean. Non-Boolean quantities are converted to Booleans before the operator is evaluated.

&& | logical AND |

|| | logical OR |

! | logical unary NOT |

Since `[false]`

is `true`

, `false || [false]`

is also `true`

.

Note that how logical operators deal with vectors is different than relational operators:

`[1, 1] > [0, 2]`

is `false`

, but

`[false, false] && [false, false]`

is `true`

.

### 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. |

### 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]

If both operands of multiplication are simple vectors, the result is a number according to the linear algebra rule for dot product. `c = u*v;`

results in . If the operands' sizes don't match, the result is `undef`

.

### Matrix Multiplication[edit]

If one or both operands of multiplication are matrices, the result is a simple vector or matrix according to the linear algebra rules for matrix product. In the following, A, B, C... are matrices, u, v, w... are vectors. Subscripts i, j denote element indices.

For A a matrix of size n × m and B a matrix of size m × p, their product `C = A*B;`

is a matrix of size n × p with elements

.

`C = B*A;`

results in `undef`

unless n = p.

For A a matrix of size n × m and v a vector of size m, their product `u = A*v;`

is a vector of size n with elements

.

In linear algebra, this is the product of a matrix and a column vector.

For v a vector of size n and A a matrix of size n × m, their product `u = v*A;`

is a vector of size m with elements

.

In linear algebra, this is the product of a row vector and a matrix.

Matrix multiplication is not commutative: , .