# OpenSCAD用戶手冊/數學函數

## 三角函數

### cos

<degrees>

 用例： ``` for(i=[0:36]) translate([i*10,0,0]) cylinder(r=5,h=cos(i*10)*50+60); ``` OpenSCAD餘弦函數示例‎

### sin

<degrees>

 用例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); } ```

 用例2： ``` for(i=[0:36]) translate([i*10,0,0]) cylinder(r=5,h=sin(i*10)*50+60); ``` OpenSCAD正弦函數示例

### tan

<degrees>

 用例： ``` 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); } ```

## 其他數學函數

### abs

```abs(-5.0);
abs(0);
abs(8.0);
```

```5.0
0.0
8.0
```

### ceil

See: 向上取整函數

```echo(ceil(4.4),ceil(-4.4));     // 生成结果ECHO: 5, -4
```

### concat

[請注意: 需要使用版本 2015.03]

```echo(concat("a","b","c","d","e","f"));          // 生成结果ECHO: ["a", "b", "c", "d", "e", "f"]
echo(concat(["a","b","c"],["d","e","f"]));      // 生成结果ECHO: ["a", "b", "c", "d", "e", "f"]
echo(concat(1,2,3,4,5,6));                      // 生成结果ECHO: [1, 2, 3, 4, 5, 6]
```

```echo(concat([ [1],[2] ], [ [3] ]));             // 生成结果ECHO: [[1], [2], [3]]
```

```echo(concat([1,2,3],[4,5,6]));                   // 生成结果ECHO: [1, 2, 3, 4, 5, 6]
echo(concat("abc","def"));                       // 生成结果ECHO: ["abc", "def"]
echo(str("abc","def"));                          // 生成结果ECHO: "abcdef"
```

### cross

```echo(cross([2, 3, 4], [5, 6, 7]));     // 生成结果ECHO: [-3, 6, -3]
echo(cross([2, 1, -3], [0, 4, 5]));    // 生成结果ECHO: [17, -10, 8]
echo(cross([2, 3, 4], "5"));           // 生成结果ECHO: undef
```

### exp

```echo(exp(1),exp(ln(3)*4));    // 生成结果ECHO: 2.71828, 81
```

### floor

```echo(floor(4.4),floor(-4.4));    // 生成结果ECHO: 4, -5
```

### len

```str1="abcdef"; len_str1=len(str1);
echo(str1,len_str1);

a=6; len_a=len(a);
echo(a,len_a);

array1=[1,2,3,4,5,6,7,8]; len_array1=len(array1);
echo(array1,len_array1);

array2=[[0,0],[0,1],[1,0],[1,1]]; len_array2=len(array2);
echo(array2,len_array2);

len_array2_2=len(array2[2]);
echo(array2[2],len_array2_2);
```

```ECHO: "abcdef", 6
ECHO: 6, undef
ECHO: [1, 2, 3, 4, 5, 6, 7, 8], 8
ECHO: [[0, 0], [0, 1], [1, 0], [1, 1]], 4
ECHO: [1, 0], 2
```

```str2="4711";
for (i=[0:len(str2)-1])
echo(str("digit ",i+1,"  :  ",str2[i]));
```

```ECHO: "digit 1  :  4"
ECHO: "digit 2  :  7"
ECHO: "digit 3  :  1"
ECHO: "digit 4  :  1"
```

```module doIt(size) {
if (len(size) == undef) {
// 若size为一个数，就把它作为x、y、z(原本undef)
do([size,size,size]);
} else {
// 若size是一个向量(也可能是一个字符串，但是这也太蠢了)
do(size);
}
}

doIt(5);	// 等价于[5,5,5]
doIt([5,5,5]);	// similar to cube(5) v's cube([5,5,5])
```

### let

[請注意: 需要使用版本 2015.03]

```let (var1 = value1, var2 = f(var1), var3 = g(var1, var2)) 表达式
```

```echo(let(a = 135, s = sin(a), c = cos(a)) [ s, c ]); // ECHO: [0.707107, -0.707107]
```

### lookup

key

<key,value> 數組

 用例： 此例將創建一張由不同高度的圓柱體構成的3D圖表。 ``` 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查找函數示例

### max

```max(n,n{,n}...)
max(vector)
```
<n>

<vector>

```max(3.0,5.0)
max(8.0,3.0,4.0,5.0)
max([8,3,4,5])
```

```5
8
8
```

### min

```min(n,n{,n}...)
min(vector)
```
<n>

<vector>

```min(3.0,5.0)
min(8.0,3.0,4.0,5.0)
min([8,3,4,5])
```

```3
3
3
```

Looking for mod - 它並不是一個函數，參見 modulo operator (%)

### norm

```a=[1,2,3,4];
b="abcd";
c=[];
d="";
e=[[1,2,3,4],[1,2,3],[1,2],[1]];
echo(norm(a)); //5.47723
echo(norm(b)); //undef
echo(norm(c)); //0
echo(norm(d)); //undef
echo(norm(e[0])); //5.47723
echo(norm(e[1])); //3.74166
echo(norm(e[2])); //2.23607
echo(norm(e[3])); //1
```

```ECHO: 5.47723
ECHO: undef
ECHO: 0
ECHO: undef
ECHO: 5.47723
ECHO: 3.74166
ECHO: 2.23607
ECHO: 1
```

### pow

<base>

<exponent>

```for (i = [0:5]) {
translate([i*25,0,0]) {
cylinder(h = pow(2,i)*5, r=10);
echo (i, pow(2,i));
}
}
```
```echo(pow(10,2)); // 意即10^2 或 10*10
// 结果: ECHO: 100

echo(pow(10,3)); // 意即10^3 或 10*10*10
// 结果: ECHO: 1000

echo(pow(125,1/3)); // 意即125^(0.333...)，等价于计算125的立方根
// 结果: ECHO: 5
```

### rands

min_value

max_value

value_count

seed_value (可選項)

```// 获取单个随机数
single_rand = rands(0,10,1)[0];
echo(single_rand);
```
```// 获取由4个元素构成的向量
seed=42;
random_vect=rands(5,15,4,seed);
echo( "Random Vector: ",random_vect);
sphere(r=5);
for(i=[0:3]) {
rotate(360*i/4) {
translate([10+random_vect[i],0,0])
sphere(r=random_vect[i]/2);
}
}
// ECHO: "Random Vector: ", [8.7454, 12.9654, 14.5071, 6.83435]
```

### round

"round"運算符返回參數的四捨五入值。

```round(x.5) = x+1.
round(x.49) = x.
round(-(x.5)) = -(x+1).
round(-(x.49)) = -x.

round(5.4); //-> 5
round(5.5); //-> 6
round(5.6); //-> 6
```

### sign

<x>

```sign(-5.0);
sign(0);
sign(8.0);
```

```-1.0
0.0
1.0
```

### sqrt

```translate([sqrt(100),0,0])sphere(100);
```

## 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". Some very nice 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 it's 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