絕對值函數(shù)
1. 取絕對值函數(shù):y = |x| fabs()
extern float fabsf(float);
extern double fabs(double);
extern long double fabsl(long double);
取整函數(shù)
1. 對x進行向上取整 -- ceil()
extern float ceilf(float x);
extern double ceil(double x);
extern long double ceill(long double x);
2. 對x進行向下取整 -- floor()
extern float floorf(float x);
extern double floor(double x);
extern long double floorl(long double x);
3. 返回一個最接近x的整數(shù) -- rint()
extern float nearbyintf(float x);
extern double nearbyint(double x);
extern long double nearbyintl(long double x);
extern float rintf(float x);
extern double rint(double x);
extern long double rintl(long double x);
extern long int lrintf(float x);
extern long int lrint(double x);
extern long int lrintl(long double x);
4. 對x進行四舍五入取整 -- round()
extern float roundf(float x);
extern double round(double x);
extern long double roundl(long double x);
extern long int lroundf(float x);
extern long int lround(double x);
extern long int lroundl(long double x);
如果我們要實現(xiàn)保留N位小數(shù)的四舍五入時宵荒。我們可以用如下的方法實現(xiàn):
double y = round(x * pow(10, N)) / pow(10, N)
PS: ceil()、floor()、rint()辣之、round()比較
ceil()
double z = ceil(-1.1); //-1
double z = ceil(0.1); // 1
double z = ceil(1.1); // 2
floor()
double z2 = floor(-1.1); //-2
double z2 = floor(0.1); // 0
double z2 = floor(1.1); // 1
rint() //如果有2個數(shù)同樣接近蝎宇,則會返回偶數(shù)的那個
double z3 = rint(-1.1); //-1
double z3 = rint(-1.5); //-2
double z3 = rint(-1.6); //-2
double z3 = rint(0.1); // 0
double z3 = rint(0.5); // 0 0為偶數(shù)
double z3 = rint(0.6); // 1
double z3 = rint(1.1); // 1
double z3 = rint(1.5); // 2
double z3 = rint(1.6); // 2
round() //不考慮正負
double z4 = round(-1.1); //-1
double z4 = round(-1.5); //-2
double z4 = round(-1.6); //-2
double z4 = round(0.1); // 0
double z4 = round(0.5); // 1
double z4 = round(0.6); // 1
double z4 = round(1.1); // 1
double z4 = round(1.5); // 2
double z4 = round(1.6); // 2
比較函數(shù)
1. 如果x>y技潘,返回x減去y的差襟齿,否則返回0 不會出現(xiàn)負數(shù) -- fdim(x,y)
extern float fdimf(float x, float y);
extern double fdim(double x, double y);
extern long double fdiml(long double x, long double y);
2. z = max(x,y) 求最大值 -- fmax(x,y)
extern float fmaxf(float x, float x);
extern double fmax(double x, double x);
extern long double fmaxl(long double x, long double x);
3. z = min(x,y) 求最小值 -- fmin(x,y)
extern float fminf(float x, float y);
extern double fmin(double x, double y);
extern long double fminl(long double x, long double y);
冪函數(shù)
1. 平方根函數(shù):y = √x -- sqrt()
extern float sqrtf(float x);
extern double sqrt(double x);
extern long double sqrtl(long double x);
2. 立方根函數(shù): y = 3√x -- cbrt()
extern float cbrtf(float x);
extern double cbrt(double x);
extern long double cbrtl(long double x);
3. 冪函數(shù):z = x ^ y -- pow(x,y)
extern float powf(float x, float y);
extern double pow(double x, double y);
extern long double powl(long double x, long double y);
4. d =√x^2 + y^2 求直角三角形斜邊的長度 -- hypot(x,y)
extern float hypotf(float x, float y);
extern double hypot(double x, double y);
extern long double hypotl(long double x, long double y);
數(shù)字拆分
1. 返回浮點數(shù)x的整數(shù)部分 -- trunc()
extern float truncf(float x);
extern double trunc(double x);
extern long double truncl(long double x);
如果我們要實現(xiàn)保留N位小數(shù)的截取時铸本。我們可以用如下的方法實現(xiàn):
double y = trunc(x * pow(10, N)) / pow(10, N)
2. 返回x/y的余數(shù)1: z = mod(x, y) -- fmod(x,y)
extern float fmodf(float x, float y);
extern double fmod(double x, double y);
extern long double fmodl(long double x, long double y);
函數(shù)返回余數(shù)r = x - n*y喇潘, 其中n等于x/y的值截取的整數(shù)体斩。
3. 返回x/y的余數(shù)2: z = mod(x, y) -- remainder(x,y)
extern float remainderf(float x, float y);
extern double remainder(double x, double y);
extern long double remainderl(long double x, long double y);
函數(shù)返回余數(shù)r = x - n*y, 其中n等于x/y的值取最接近的整數(shù)颖低,如果有兩個數(shù)都接近x/y絮吵,那么n就取偶數(shù)。
// 7/(2.5) = 2.8
double z = fmod(7, 2.5); 2.8 取 2 - > 7 - 2.5 * 2 = 2
double x = remainder(7, 2.5); // 2.8 取 3 -- > 7 - 2.5 * 3 = -0.5
從上面的描述可以看出fmod和remainder的區(qū)別主要在于x/y的整數(shù)部分的處理不一樣:fmod函數(shù)是取x/y的整數(shù)來算余數(shù)忱屑,而remainder函數(shù)則取最接近x/y的整數(shù)來算余數(shù)蹬敲。
4. 返回x/y的余數(shù)和整數(shù)商 -- remquo(x,y,&quo)
extern float remquof(float x, float y , int *quo);
extern double remquo(double x, double y, int *quo);
extern long double remquol(long double x, long double y, int * quo);
這個函數(shù)和 remainder函數(shù)一樣暇昂,只不過會將整數(shù)商也返回給quo,也就是說r = x - n *y這個等式中伴嗡,r作為函數(shù)的返回急波,而n則返回給quo。
5. 分解出x的整數(shù)和小數(shù)部分 -- modf(x,&p)
extern float modff(float x, float p*);
extern double modf(double x, double p*);
extern long double modfl(long double x, long double p*);
函數(shù)返回小數(shù)部分闹究,整數(shù)部分存儲在p中幔崖。
6. 分解出x的指數(shù)和尾數(shù)部分 -- frexp(x,&p)
extern float frexpf(float x, int * p);
extern double frexp(double x, int * p);
extern long double frexpl(long double x, int * p);
函數(shù)返回尾數(shù)*符號部分,指數(shù)部分存儲在p中渣淤。需要明確的是如果浮點數(shù)x為0或者非規(guī)格化浮點數(shù)時按浮點數(shù)的定義格式返回尾數(shù)和指數(shù)赏寇,而當x為規(guī)格化浮點數(shù)那么返回的值的區(qū)間是[0.5, 1)。這里的返回值和指數(shù)值p和上面介紹的規(guī)格化浮點數(shù)格式: 符號 * (1.尾數(shù)) * 2^指數(shù) 有差異价认。因為按照定義返回的尾數(shù)部分應該是1.xxx嗅定,但是這里的返回值卻是[0.5, 1)。其實這并不矛盾用踩,只是函數(shù)對返回的值做了特殊處理:因為一個正浮點數(shù)可以表示為:1.m * 2^e ==> (2^0 + 0.m) * 2^e ==> (2^0 / 2 + 0.m / 2) *2^(e+1) =>(0.5 + 0.m/2) *2^(e+1)渠退。因此frexp函數(shù)返回的真實值是: 尾數(shù)除以2,而p存儲的是:指數(shù)+1
下面函數(shù)使用的一些例子:
int p1 = 0; double y1 = frexp(16.0, &p); //y1=0.5, p= 5
int p2 = 0; double y2 = frexp(1.0, &p); //y2=0.5, p = 1
int p3 = 0; double y3 = frexp(0.0, &p); //y3=0, p = 0
這個函數(shù)和上面的ldexp函數(shù)為互逆函數(shù)脐彩。要詳細的了解浮點數(shù)存儲格式請參考IEEE754
三角函數(shù)
1. 反余弦函數(shù): y = arccos(x)
extern float acosf(float x);
extern double acos(double x);
extern long double acosl(long double x);
2. 反正弦函數(shù):y = arcsin(x)
extern float asinf(float x);
extern double asin(double x);
extern long double asinl(long double x);
3. 反正切函數(shù): y = arctan(x)
extern float atanf(float x);
extern double atan(double x);
extern long double atanl(long double x);
4. 2個參數(shù)的反正切函數(shù):z = arctan(y/x)
extern float atan2f(float y, float x);
extern double atan2(double y, double x);
extern long double atan2l(long double y, long double x);
因為arctan的定義域是在(-∞, +∞)碎乃,而值域是在(-????/2, ????/2)之間。因此 :
atan2f(-1.0, 0.0) == -????/2; atan2f(1.0, 0.0) == ????/2;
這個函數(shù)提供的另外一個意義在于tan函數(shù)的值其實就是對邊除以鄰邊的結(jié)果惠奸,因此當知道對邊和鄰邊時就可以直接用這個逆三角函數(shù)來求得對應的弧度值梅誓。假如特殊情況下對邊和鄰邊的值都是0.0,那么如果你調(diào)用atan(0.0/0.0)得到的值將是NAN而不是0佛南。因為0.0/0.0的值是NAN,而對NAN調(diào)用atan函數(shù)返回的也是NAN梗掰,但是對atan2(0.0,0.0)調(diào)用返回的結(jié)果就是正確值0。
5. 余弦函數(shù): y = cos(x)
extern float cosf(float x);
extern double cos(double x);
extern long double cosl(long double x);
6. 正弦函數(shù):y = sin(x)
extern float sinf(float x);
extern double sin(double x);
extern long double sinl(long double x);
7. 正切函數(shù):y = tan(x)
extern float tanf(float x);
extern double tan(double x);
extern long double tanl(long double x);
雙曲函數(shù)
1. 反雙曲余弦函數(shù):y = arccosh(x)
extern float acoshf(float x);
extern double acosh(double x);
extern long double acoshl(long double x);
2. 反雙曲正弦函數(shù):y = arcsinh(x)
extern float asinhf(float x);
extern double asinh(double x);
extern long double asinhl(long double x);
3. 反雙曲正切函數(shù):y = arctanh(x)
extern float atanhf(float x);
extern double atanh(double x);
extern long double atanhl(long double x);
4. 雙曲余弦函數(shù):y = cosh(x)
extern float coshf(float x);
extern double cosh(double x);
extern long double coshl(long double x);
5. 雙曲正弦函數(shù):y = sinh(x)
extern float sinhf(float x);
extern double sinh(double x);
extern long double sinhl(long double x);
6. 雙曲正切函數(shù): y = tanh(x)
extern float tanhf(float x);
extern double tanh(double x);
extern long double tanhl(long double x);