Arduino - 數學庫
Arduino 數學庫 (math.h) 包含許多用於操作浮點數的有用數學函式。
庫宏
以下是 math.h 標頭檔案中定義的宏:
以下是 math.h 標頭檔案中定義的宏列表
宏
值
描述
M_E
2.7182818284590452354
常數 e。
M_LOG2E
1.4426950408889634074
/* log_2 e */
e 的以 2 為底的對數
M_1_PI
0.31830988618379067154
/* 1/pi */
常數 1/pi
M_2_PI
0.63661977236758134308
/* 2/pi */
常數 2/pi
M_2_SQRTPI
1.12837916709551257390
/* 2/sqrt(pi) */
常數 2/sqrt(pi)
M_LN10
2.30258509299404568402
/* log_e 10 */
10 的自然對數
M_LN2
0.69314718055994530942
/* log_e 2 */
2 的自然對數
M_LOG10E
0.43429448190325182765
/* log_10 e */
e 的以 10 為底的對數
M_PI
3.14159265358979323846
/* pi */
常數 pi
M_PI_2
3.3V1.57079632679489661923
/* pi/2 */
常數 pi/2
M_PI_4
0.78539816339744830962
/* pi/4 */
常數 pi/4
M_SQRT1_2
0.70710678118654752440
/* 1/sqrt(2) */
常數 1/sqrt(2)
M_SQRT2
1.41421356237309504880
/* sqrt(2) */
2 的平方根
acosf
-
acos() 函式的別名
asinf
-
asin() 函式的別名
atan2f
-
atan2() 函式的別名
cbrtf
-
cbrt() 函式的別名
ceilf
-
ceil() 函式的別名
copysignf
-
copysign() 函式的別名
coshf
-
cosh() 函式的別名
expf
-
exp() 函式的別名
fabsf
-
fabs() 函式的別名
fdimf
-
fdim() 函式的別名
floorf
-
floor() 函式的別名
fmaxf
-
fmax() 函式的別名
fminf
-
fmin() 函式的別名
fmodf
-
fmod() 函式的別名
frexpf
-
frexp() 函式的別名
hypotf
-
hypot() 函式的別名
INFINITY
-
INFINITY 常量
isfinitef
-
isfinite() 函式的別名
isinff
-
isinf() 函式的別名
isnanf
-
isnan() 函式的別名
ldexpf
-
ldexp() 函式的別名
log10f
-
log10() 函式的別名
logf
-
log() 函式的別名
lrintf
-
lrint() 函式的別名
lroundf
-
lround() 函式的別名
庫函式
math.h 標頭檔案中定義了以下函式:
以下是 math.h 標頭檔案中定義的函式列表:
序號
庫函式和描述
1
double acos (double __x)
acos() 函式計算 __x 的反餘弦的主值。返回值的範圍是 [0, pi] 弧度。對於不在範圍 [-1, +1] 內的引數,會發生域錯誤。
2
double asin (double __x)
asin() 函式計算 __x 的反正弦的主值。返回值的範圍是 [-pi/2, pi/2] 弧度。對於不在範圍 [-1, +1] 內的引數,會發生域錯誤。
3
double atan (double __x)
atan() 函式計算 __x 的反正切的主值。返回值的範圍是 [-pi/2, pi/2] 弧度。
4
double atan2 (double __y, double __x)
atan2() 函式計算 __y / __x 的反正切的主值,使用兩個引數的符號來確定返回值的象限。返回值的範圍是 [-pi, +pi] 弧度。
5
double cbrt (double __x)
cbrt() 函式返回 __x 的立方根。
6
double ceil (double __x)
ceil() 函式返回大於或等於 __x 的最小整數值,表示為浮點數。
7
static double copysign (double __x, double __y)
copysign() 函式返回 __x,但帶有 __y 的符號。即使 __x 或 __y 為 NaN 或零,它們也能工作。
8
double cos(double __x)
cos() 函式返回 __x 的餘弦值,以弧度為單位。
9
double cosh (double __x)
cosh() 函式返回 __x 的雙曲餘弦。
10
double exp (double __x)
exp() 函式返回 __x 的指數值。
11
double fabs (double __x)
fabs() 函式計算浮點數 __x 的絕對值。
12
double fdim (double __x, double __y)
fdim() 函式返回 max(__x - __y, 0)。如果 __x 或 __y 或兩者都是 NaN,則返回 NaN。
13
double floor (double __x)
floor() 函式返回小於或等於 __x 的最大整數值,表示為浮點數。
14
double fma (double __x, double __y, double __z)
fma() 函式執行浮點乘加運算。這是操作 (__x * __y) + __z,但中間結果不會舍入到目標型別。這有時可以提高計算的精度。
15
double fmax (double __x, double __y)
fmax() 函式返回兩個值 __x 和 __y 中較大的一個。如果一個引數是 NaN,則返回另一個引數。如果兩個引數都是 NaN,則返回 NaN。
16
double fmin (double __x, double __y)
fmin() 函式返回兩個值 __x 和 __y 中較小的一個。如果一個引數是 NaN,則返回另一個引數。如果兩個引數都是 NaN,則返回 NaN。
17
double fmod (double __x, double__y)
fmod() 函式返回 __x / __y 的浮點餘數。
18
double frexp (double __x, int * __pexp)
frexp() 函式將浮點數分解為標準化分數和 2 的整數冪。它將整數儲存在 __pexp 指向的 int 物件中。如果 __x 是一個正常的浮點數,frexp() 函式返回的值 v,使得 v 的大小在區間 [1/2, 1) 或零內,並且 __x 等於 v 乘以 2 的 __pexp 次冪。如果 __x 為零,則結果的兩部分都為零。如果 __x 不是有限數,則 frexp() 返回 __x 本身,並透過 __pexp 儲存 0。
注意 - 此實現允許零指標作為指令來跳過儲存指數。
19
double hypot (double __x, double__y)
hypot() 函式返回 sqrt(__x*__x + __y*__y)。這是邊長為 __x 和 __y 的直角三角形的斜邊長度,或者點 (__x, __y) 到原點的距離。最好使用此函式而不是直接公式,因為誤差要小得多。__x 和 __y 小的時候不會下溢。如果結果在範圍內,則不會上溢。
20
static int isfinite (double __x)
isfinite() 函式如果 __x 是有限的:不是正無窮大或負無窮大,也不是 NaN ,則返回非零值。
21
int isinf (double __x)
isinf() 函式如果引數 __x 是正無窮大,則返回 1;如果 __x 是負無窮大,則返回 -1;否則返回 0。
注意 - GCC 4.3 可以用內聯程式碼替換此函式,該程式碼對正負無窮大都返回 1 值(gcc bug #35509)。
22
int isnan (double __x)
isnan() 函式如果引數 __x 表示“非數字”(NaN )物件,則返回 1,否則返回 0。
23
double ldexp (double __x, int __exp )
ldexp() 函式將浮點數乘以 2 的整數冪。它返回 __x 乘以 2 的 __exp 次冪的值。
24
double log (double __x)
log() 函式返回引數 __x 的自然對數。
25
double log10(double __x)
log10() 函式返回引數 __x 以 10 為底的對數。
26
long lrint (double __x)
lrint() 函式將 __x 四捨五入到最接近的整數,其中四捨五入到偶數方向。(也就是說,1.5 和 2.5 都四捨五入到 2)。此函式類似於 rint() 函式,但返回值型別不同,並且可能發生溢位。
返回值
四捨五入後的長整數。如果 __x 不是有限數或發生溢位,則此實現返回 LONG_MIN 值 (0x80000000)。
27
long lround (double __x)
lround() 函式將 __x 四捨五入到最接近的整數,但將四捨五入的情況遠離零(而不是四捨五入到最接近的偶數)。此函式類似於 round() 函式,但返回值型別不同,並且可能發生溢位。
返回值
四捨五入後的長整數。如果 __x 不是有限數或發生溢位,則此實現返回 LONG_MIN 值 (0x80000000)。
28
double modf (double __x, double * __iptr )
modf() 函式將引數 __x 分解為整數部分和小數部分,兩者都與引數具有相同的符號。它將整數部分作為雙精度浮點數儲存在 __iptr 指向的物件中。
modf() 函式返回 __x 的帶符號小數部分。
注意 − 此實現跳過透過零指標寫入。但是,GCC 4.3 可以用內聯程式碼替換此函式,該內聯程式碼不允許使用 NULL 地址來避免儲存。
29
float modff (float __x, float * __iptr)
modf() 的別名。
30
double pow (double __x, double __y)
pow() 函式返回 __x 的 __y 次冪的值。
31
double round (double __x)
round() 函式將 __x 四捨五入到最接近的整數,但將四捨五入的情況遠離零(而不是四捨五入到最接近的偶數)。不可能發生溢位。
返回值
四捨五入後的值。如果 __x 是整數或無窮大,則返回 __x 本身。如果 __x 是 NaN ,則返回 NaN 。
32
int signbit (double __x)
signbit() 函式如果 __x 的值設定了其符號位,則返回非零值。這與 `__x < 0.0` 不同,因為 IEEE 754 浮點數允許零帶符號。比較 `-0.0 < 0.0` 為假,但 `signbit (-0.0)` 將返回非零值。
33
double sin (double __x)
sin() 函式返回 __x 的正弦值,以弧度為單位。
34
double sinh (double __x)
sinh() 函式返回 __x 的雙曲正弦值。
35
double sqrt (double __x)
sqrt() 函式返回 __x 的非負平方根。
36
double square (double __x)
square() 函式返回 __x * __x。
注意 − 此函式不屬於 C 標準定義。
37
double tan (double __x)
tan() 函式返回 __x 的正切值,以弧度為單位。
38
double tanh ( double __x)
tanh() 函式返回 __x 的雙曲正切值。
39
double trunc (double __x)
trunc() 函式將 __x 四捨五入到絕對值不大的最接近整數。
示例
以下示例演示如何使用最常見的 math.h 庫函式 −
double double__x = 45.45 ;
double double__y = 30.20 ;
void setup() {
Serial.begin(9600);
Serial.print("cos num = ");
Serial.println (cos (double__x) ); // returns cosine of x
Serial.print("absolute value of num = ");
Serial.println (fabs (double__x) ); // absolute value of a float
Serial.print("floating point modulo = ");
Serial.println (fmod (double__x, double__y)); // floating point modulo
Serial.print("sine of num = ");
Serial.println (sin (double__x) ) ;// returns sine of x
Serial.print("square root of num : ");
Serial.println ( sqrt (double__x) );// returns square root of x
Serial.print("tangent of num : ");
Serial.println ( tan (double__x) ); // returns tangent of x
Serial.print("exponential value of num : ");
Serial.println ( exp (double__x) ); // function returns the exponential value of x.
Serial.print("cos num : ");
Serial.println (atan (double__x) ); // arc tangent of x
Serial.print("tangent of num : ");
Serial.println (atan2 (double__y, double__x) );// arc tangent of y/x
Serial.print("arc tangent of num : ");
Serial.println (log (double__x) ) ; // natural logarithm of x
Serial.print("cos num : ");
Serial.println ( log10 (double__x)); // logarithm of x to base 10.
Serial.print("logarithm of num to base 10 : ");
Serial.println (pow (double__x, double__y) );// x to power of y
Serial.print("power of num : ");
Serial.println (square (double__x)); // square of x
}
void loop() {
}
結果
cos num = 0.10
absolute value of num = 45.45
floating point modulo =15.25
sine of num = 0.99
square root of num : 6.74
tangent of num : 9.67
exponential value of num : ovf
cos num : 1.55
tangent of num : 0.59
arc tangent of num : 3.82
cos num : 1.66
logarithm of num to base 10 : inf
power of num : 2065.70