# OpenSCAD User Manual/Mathematical Functions
## Trigonometric functions

The trig functions use the C Language mathematics functions, which are based in turn on Binary Floating Point mathematics, which use approximations of Real Numbers during calculation. OpenSCAD's math functions use the C++ 'double' type, inside Value.h/Value.cc,
A good resource for the specifics of the C library math functions, such as valid inputs/output ranges, can be found at the Open Group website math.h & acos

### cos

Mathematical cosine function of degrees. See Cosine
Parameters

#### <degrees>

    Decimal. Angle in degrees.
| Usage example: |
| for(i=[0:36]) translate([i*10,0,0]) cylinder(r=5,h=cos(i*10)*50+60); | OpenSCAD Cos Function‎ |

### sin

Mathematical sine function. See Sine
Parameters

#### <degrees>

    Decimal. Angle in degrees.
* Usage example 1:
* for (i = [0:5]) {
* echo(360*i/6, sin(360*i/6)*80, cos(360*i/6)*80);
* translate([sin(360*i/6)*80, cos(360*i/6)*80, 0 ])
* cylinder(h = 200, r=10);
| Usage example 2: |
| for(i=[0:36]) translate([i*10,0,0]) cylinder(r=5,h=sin(i*10)*50+60); | OpenSCAD Sin Function |

### tan

Mathematical tangent function. See Tangent
Parameters

#### <degrees>

    Decimal. Angle in degrees.
* Usage example:
* for (i = [0:5]) {
* echo(360*i/6, tan(360*i/6)*80);
* translate([tan(360*i/6)*80, 0, 0 ])
* cylinder(h = 200, r=10);

### acos

Mathematical arccosine, or inverse cosine, expressed in degrees. See: Inverse trigonometric functions

### asin

Mathematical arcsine, or inverse sine, expressed in degrees. See: Inverse trigonometric functions

### atan

Mathematical arctangent, or inverse tangent, function. Returns the principal value of the arc tangent of x, expressed in degrees. atan cannot distinguish between y/x and -y/-x and returns angles from -90 to +90. See: atan2 and also Inverse trigonometric functions

### atan2

Mathematical two-argument atan function atan2(y,x) that spans the full 360 degrees. This function returns the full angle between the x axis and the vector(x,y) expressed in degrees, in the range $-180<angle\leq 180$.
Usage examples:

## Other Mathematical Functions
### abs

Mathematical absolute value function. Returns the positive value of a signed decimal number.
Usage examples:

### ceil

Mathematical ceiling function.
Returns the next highest integer value by rounding up value if necessary.
See: Ceil Function

### concat

[Note: Requires version 2015.03]
Return a new vector that is the result of appending the elements of the supplied vectors.
Where an argument is a vector the elements of the vector are individually appended to the result vector. Strings are distinct from vectors in this case.
Usage examples:
Vector of vectors
Note: All vectors passed to the function lose one nesting level. When adding something like a single element [x, y, z] tuples (which are vectors, too), the tuple needs to be enclosed in a vector (i.e. an extra set of brackets) before the concatenation. in the exmple below, a fourth point is added to the polygon path, which used to resemble a triangle, making it a square now:
Contrast with strings

### cross

Calculates the cross product of two vectors in 3D or 2D space. If both vectors are in the 3D, the result is a vector that is perpendicular to both of the input vectors. If both vectors are in 2D space, their cross product has the form [0,0,z] and the cross function returns just the z value of the cross product:
cross([x,y], [u,v]) = x*v - y*u
Note that this is the determinant of the 2×2 matrix [[x,y],[u,v]]. Using any other types, vectors with lengths different from 2 or 3, or vectors not of the same length produces 'undef'.
Usage examples:
For any two vectors a and b in 2D or in 3D, the following holds:
cross(a,b) == -cross(b,a)

### exp

Mathematical exp function. Returns the base-e exponential function of x, which is the number e raised to the power x. See: Exponent

### floor

Mathematical floor function. floor(x) = is the largest integer not greater than x
See: Floor Function

### ln

Mathematical natural logarithm. See: Natural logarithm

### len

Mathematical length function. Returns the length of an array, a vector or a string parameter.
Usage examples:
Results:
This function allows (e.g.) the parsing of an array, a vector or a string.
Usage examples:
Results:
Note that the len function is not defined and raises a warning when a simple variable is passed as the parameter.
This is useful when handling parameters to a module, similar to how shapes can be defined as a single number, or as an [x,y,z] vector; i.e. cube(5) or cube([5,5,5])
For example

### let

[Note: Requires version 2015.03]
Sequential assignment of variables inside an expression. The following expression is evaluated in context of the let assignments and can use the variables. This is mainly useful to make complicated expressions more readable by assigning interim results to variables.
Parameters
Usage example:
Let can also be used to create variables in a Function. (See also: "Let Statement")

### log

Mathematical logarithm to the base 10. Example: log(1000) = 3. See: Logarithm

### lookup

Look up value in table, and linearly interpolate if there's no exact match. The first argument is the value to look up. The second is the lookup table -- a vector of key-value pairs.
Parameters

#### key

    A lookup key

#### <key,value> array

    keys and values
There is a bug in which out-of-range keys return the first value in the list. Newer versions of Openscad should use the top or bottom end of the table as appropriate instead.
Usage example: Create a 3D chart made from cylinders of different heights.
| function get_cylinder_h(p) = lookup(p, [ [ -200, 5 ], [ -50, 20 ], [ -20, 18 ], [ +80, 25 ], [ +150, 2 ] ]); for (i = [-100:5:+100]) { // echo(i, get_cylinder_h(i)); translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h = get_cylinder_h(i)*3); } | OpenSCAD Lookup Function |

### max

Returns the maximum of the parameters. If a single vector is given as parameter, returns the maximum element of that vector.
Parameters

#### <n>

    Two or more decimals

#### <vector>

    Single vector of decimals [Note: Requires version 2014.06].
Usage example:
Results:

### min

Returns the minimum of the parameters. If a single vector is given as parameter, returns the minimum element of that vector.
Parameters

#### <n>

    Two or more decimals

#### <vector>

    Single vector of decimals [Note: Requires version 2014.06].
Usage example:
Results:

### mod

Included in this document only for clarity. The 'modulo' operation exists in OpenSCAD as an operator %, and not as function. See modulo operator (%)

### norm

Returns the euclidean norm of a vector. Note this returns the actual numeric length while len returns the number of elements in the vector or array.
Usage examples:
Results:

### pow

Mathematical power function.
As of version 2021.01 you can use the exponentiation operator ^ instead.
Parameters

#### <base>

    Decimal. Base.

#### <exponent>

    Decimal. Exponent.
Usage examples:

### rands

Random number generator. Generates a constant vector of pseudo random numbers, much like an array. The numbers are doubles not integers. When generating only one number, you still call it with variable[0].
The random numbers generated are a "half open interval"; each is greater than or equal to the minimum, and less than the maximum.
Parameters

#### min_value

    Minimum value of random number range

#### max_value

    Maximum value of random number range

#### value_count

    Number of random numbers to return as a vector

#### seed_value (optional)

    Seed value for random number generator for repeatable results. On versions before late 2015, seed_value gets rounded to the nearest integer.
Usage examples:

### round

The "round" operator returns the greatest or least integer part, respectively, if the numeric input is positive or negative.
Usage examples:
Results:

### sign

Mathematical signum function. Returns a unit value that extracts the sign of a value see: Signum function
Parameters

#### <x>

    Decimal. Value to find the sign of.
Usage examples:
Results:

### sqrt

Mathematical square root function.

#### Usage example
## Infinities and NaNs

How does OpenSCAD deal with inputs like (1/0)? Basically, the behavior is inherited from the language OpenSCAD was written in, the C++ language, and its floating point number types and the associated C math library. This system allows representation of both positive and negative infinity by the special values "Inf" or "-Inf". It also allow representation of creatures like sqrt(-1) or 0/0 as "NaN", an abbreviation for "Not A Number". Explanations can be found on the web, for example the Open Group's site on math.h or Wikipedia's page on the IEEE 754 number format. However, OpenSCAD is its own language so it may not exactly match everything that happens in C. For example, OpenSCAD uses degrees instead of radians for trigonometric functions. Another example is that sin does not throw a "domain error" when the input is 1/0, although it does return NaN.
Here are some examples of infinite input to OpenSCAD math functions and the resulting output, taken from OpenSCAD's regression test system in late 2015.
| 0/0: nan | sin(1/0): nan | asin(1/0): nan | ln(1/0): inf | round(1/0): inf |
| -0/0: nan | cos(1/0): nan | acos(1/0): nan | ln(-1/0): nan | round(-1/0): -inf |
| 0/-0: nan | tan(1/0): nan | atan(1/0): 90 | log(1/0): inf | sign(1/0): 1 |
| 1/0: inf | ceil(-1/0): -inf | atan(-1/0): -90 | log(-1/0): nan | sign(-1/0): -1 |
| 1/-0: -inf | ceil(1/0): inf | atan2(1/0, -1/0): 135 | max(-1/0, 1/0): inf | sqrt(1/0): inf |
| -1/0: -inf | floor(-1/0): -inf | exp(1/0): inf | min(-1/0, 1/0): -inf | sqrt(-1/0): nan |
| -1/-0: inf | floor(1/0): inf | exp(-1/0): 0 | pow(2, 1/0): inf | pow(2, -1/0): 0 |