指点成金-最美分享吧

登录

iOS 中常用的几种函数 (向上,向下,四舍五入)取整

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了iOS 中常用的几种函数 (向上,向下,四舍五入)取整相关的知识,希望对你有一定的参考价值。

参考技术A 2.高斯函数,向下取整

floor(1.0/4)=0;

floor(4.0/4)=1;

floor(5.0/4)=1;

floor(8.0/4)=2;

floor()方法是向下取整,类似于数学中的高斯函数 [].取得不大于浮点数的最大整数,对于正数来说是舍弃浮点数部分,对于复数来说,舍弃浮点数部分后再减1.

3.ceil函数,向上取整。

如:

ceil(1.0/4)=1;

ceil(4.0/4)=1;

ceil(5.0/4)=2;

ceil(8.0/4)=2;

ceil()方法是向上取整, 括号内是double类型

这两个函数都是math.h库里面的,直接使用就行,ios不需要再次倒入头文件,类似这个数学函数还有很多,如

1、 三角函数

double sin (double); 正弦

double cos (double);余弦

double tan (double);正切

2 、反三角函数

double asin (double); 结果介于[-PI/2, PI/2]

double acos (double); 结果介于[0, PI]

double atan (double); 反正切(主值), 结果介于[-PI/2, PI/2]

double atan2 (double, double); 反正切(整圆值), 结果介于[-PI, PI]

3 、双曲三角函数

double sinh (double);

double cosh (double);

double tanh (double);

4 、指数与对数

double exp (double);求取自然数e的幂

double sqrt (double);开平方

double log (double); 以e为底的对数

double log10 (double);以10为底的对数

double pow(double x, double y);计算以x为底数的y次幂

float powf(float x, float y); 功能与pow一致,只是输入与输出皆为浮点数

5 、取整

double ceil (double); 取上整

double floor (double); 取下整

6 、绝对值

double fabs (double);求绝对值

double cabs(struct complex znum) ;求复数的绝对值

7 、标准化浮点数

double frexp (double f, int *p); 标准化浮点数, f = x * 2^p, 已知f求x, p ( x介于[0.5, 1] )

double ldexp (double x, int p); 与frexp相反, 已知x, p求f

8 、取整与取余

double modf (double, double*); 将参数的整数部分通过指针回传, 返回小数部分

double fmod (double, double); 返回两参数相除的余数

9 、其他

double hypot(double x, double y);已知直角三角形两个直角边长度,求斜边长度

double ldexp(double x, int exponent);计算x*(2的exponent次幂)

double poly(double x, int degree, double coeffs [] );计算多项式

nt matherr(struct exception *e);数学错误计算处理程序

C中常用的数学函数

rand() ----随机数abs() / labs() ----整数绝对值fabs() / fabsf() / fabsl() ----浮点数绝对值floor() / floorf() / floorl() ----向下取整ceil() / ceilf() / ceill() ----向上取整round() / roundf() / roundl() ----四舍五入sqrt() / sqrtf() / sqrtl() ----求平方根fmax() / fmaxf() / fmaxl() ----求最大值fmin() / fminf() / fminl() ----求最小值hypot() / hypotf() / hypotl() ----求直角三角形斜边的长度fmod() / fmodf() / fmodl() ----求两数整除后的余数modf() / modff() / modfl() ----浮点数分解为整数和小数frexp() / frexpf() / frexpl() ----浮点数分解尾数和二为底的指数sin() / sinf() / sinl() ----求正弦值sinh() / sinhf() / sinhl() ----求双曲正弦值cos() / cosf() / cosl() ----求余弦值cosh() / coshf() / coshl() ----求双曲余弦值tan() / tanf() / tanl() ----求正切值tanh() / tanhf() / tanhl() ----求双曲正切值asin() / asinf() / asinl() ----求反正弦值asinh() / asinhf() / asinhl() ----求反双曲正弦值acos() / acosf() / acosl() ----求反余弦值acosh() / acoshf() / acoshl() ----求反双曲余弦值atan() / atanf() / atanl() ----求反正切值atan2() / atan2f() / atan2l() ----求坐标值的反正切值atanh() / atanhf() / atanhl() ----求反双曲正切值

 

以上是关于iOS 中常用的几种函数 (向上,向下,四舍五入)取整的主要内容,如果未能解决你的问题,请参考以下文章