- 分享一些iOS开发中的常用函数,简单粗暴,能不自己去写逻辑计算的就不要自己去写,如果自己不是特别牛,性能考虑特全面,那就乖乖用系统封装的吧 !!!!
苹果给出来的蛮多的,长期以来我常用的,顺手拈来使用的也就几个,下面列出所有的,然后给大家加好注释和例子,这些东西看一眼有个印象,将来有这样的情景能想起来在查询使用:
Math Functions
demo数据:
CGFloat temp = 1.1569;
// 向上取整
extern float ceilf(float);
extern double ceil(double);
extern long double ceill(long double);
ceilf(temp), ceil(temp), ceill(temp) 值为:2
// 向下取整
extern float floorf(float);
extern double floor(double);
extern long double floorl(long double);
floorf(temp), floor(temp), floorl(temp) 值为:1
// 求反余弦值
extern float acosf(float);
extern double acos(double);
extern long double acosl(long double);
// 求反双曲正弦值
extern float asinf(float);
extern double asin(double);
extern long double asinl(long double);
// 求反正切值
extern float atanf(float);
extern double atan(double);
extern long double atanl(long double);
// 求坐标值的反正切值
extern float atan2f(float, float);
extern double atan2(double, double);
extern long double atan2l(long double, long double);
// 求反余弦值
extern float cosf(float);
extern double cos(double);
extern long double cosl(long double);
// 求正弦值
extern float sinf(float);
extern double sin(double);
extern long double sinl(long double);
// 求正切值
extern float tanf(float);
extern double tan(double);
extern long double tanl(long double);
// 求反双曲余弦值
extern float acoshf(float);
extern double acosh(double);
extern long double acoshl(long double);
// 求反双曲正弦值
extern float asinhf(float);
extern double asinh(double);
extern long double asinhl(long double);
// 求反双曲正切值
extern float atanhf(float);
extern double atanh(double);
extern long double atanhl(long double);
// 求双曲余弦值
extern float coshf(float);
extern double cosh(double);
extern long double coshl(long double);
// 求双曲正弦值
extern float sinhf(float);
extern double sinh(double);
extern long double sinhl(long double);
// 求双曲正切值
extern float tanhf(float);
extern double tanh(double);
extern long double tanhl(long double);
测试数据:
int a = 3;
// 求e的x次方的值
extern float expf(float);
extern double exp(double);
extern long double expl(long double);
expf(a)=20.085537, exp(a)=20.085536923187668, expl(a)=20.0855369231876677415
// 2的x的次方的值
extern float exp2f(float);
extern double exp2(double);
extern long double exp2l(long double);
exp2f(a), exp2(a), exp2l(a)值为:8
// 计算基础e指数参数但不包括 1
extern float expm1f(float);
extern double expm1(double);
extern long double expm1l(long double);
// 求以e为底的对数值
extern float logf(float);
extern double log(double);
extern long double logl(long double);
int b = 8;
logf(b)=2.07944155, log(b)=2.0794415416798357, logl(b)=2.07944154167983592829
// 求以10为底的对数值
extern float log10f(float);
extern double log10(double);
extern long double log10l(long double);
int b = 100;
log10f(b), log10(b), log10l(b)值为:2
// 求以2为底的对数值
extern float log2f(float);
extern double log2(double);
extern long double log2l(long double);
int b = 8;
log2f(b), log2(b), log2l(b)值为:3
// 计算基础e对数1加上参数
extern float log1pf(float);
extern double log1p(double);
extern long double log1pl(long double);
// 提取 _X 指数作为一个带符号整数值为浮点格式
extern float logbf(float);
extern double logb(double);
extern long double logbl(long double);
// 浮点数分解为整数和小数。返回 _X 的有符号的小数部分
extern float modff(float, float *);
extern double modf(double, double *);
extern long double modfl(long double, long double *);
extern float ldexpf(float, int);
extern double ldexp(double, int);
extern long double ldexpl(long double, int);
// 计算从尾数和指数的一个实数
extern float frexpf(float, int *);
extern double frexp(double, int *);
extern long double frexpl(long double, int *);
// 提取_X 指数为无符号整型值
extern int ilogbf(float);
extern int ilogb(double);
extern int ilogbl(long double);
// 以 FLT_RADIX乘以_X到乘幂运算_Y
extern float scalbnf(float, int);
extern double scalbn(double, int);
extern long double scalbnl(long double, int);
// 将浮点数乘以 FLT_RADIX 的整数幂
extern float scalblnf(float, long int);
extern double scalbln(double, long int);
extern long double scalblnl(long double, long int);
// 浮点数绝对值
extern float fabsf(float);
extern double fabs(double);
extern long double fabsl(long double);
int b = -8.90111;
fabsf(b), fabs(b), fabsl(b)值为:8
// 计算参数的实立方根
extern float cbrtf(float);
extern double cbrt(double);
extern long double cbrtl(long double);
int b = 8;
cbrtf(b), cbrt(b), cbrtl(b)值为:2
// 求值直角三角形的斜边长度,给出的两边为 x 和 y (换言之,根的长度 x2 + y2)
extern float hypotf(float, float);
extern double hypot(double, double);
extern long double hypotl(long double, long double);
hypotf(3, 4),hypot(3, 4), hypotl(3, 4)值为:5
// 求n的m次方的值,计算以n为底数的m次幂
extern float powf(float, float);
extern double pow(double, double);
extern long double powl(long double, long double);
powf(3, 3)值为27
// 开平方
extern float sqrtf(float);
extern double sqrt(double);
extern long double sqrtl(long double);
sqrtf(9)、sqrt(9)、sqrtl(9)值为:3
// 函数返回 x 的高斯误差函数
extern float erff(float);
extern double erf(double);
extern long double erfl(long double);
// 函数返回 x 的补余高斯误差函数
extern float erfcf(float);
extern double erfc(double);
extern long double erfcl(long double);
/* lgammaf, lgamma, and lgammal are not thread-safe. The thread-safe
variants lgammaf_r, lgamma_r, and lgammal_r are made available if
you define the _REENTRANT symbol before including <math.h> */
// 计算伽玛参数绝对值的自然对数
extern float lgammaf(float);
extern double lgamma(double);
extern long double lgammal(long double);
// 计算_X 的伽玛函数
extern float tgammaf(float);
extern double tgamma(double);
extern long double tgammal(long double);
// 四舍五入取整
通过使用当前舍入方向,舍入参数为浮点格式的一个整数值,整数。返回舍入的整数值
extern float nearbyintf(float);
extern double nearbyint(double);
extern long double nearbyintl(long double);
nearbyintf(9.0110)=9,nearbyintf(9.599)=10
// 将浮点值舍入到最接近的整数(采用浮点格式),四舍五入取整
extern float rintf(float);
extern double rint(double);
extern long double rintl(long double);
// 四舍五入取整
extern long int lrintf(float);
extern long int lrint(double);
extern long int lrintl(long double);
// 四舍五入取整
extern float roundf(float);
extern double round(double);
extern long double roundl(long double);
// 四舍五入,取最接近的整数
extern long int lroundf(float);
extern long int lround(double);
extern long int lroundl(long double);
/* long long is not part of C90. Make sure you are passing -std=c99 or
-std=gnu99 or higher if you need these functions returning long longs */
#if !(__DARWIN_NO_LONG_LONG)
extern long long int llrintf(float);
extern long long int llrint(double);
extern long long int llrintl(long double);
extern long long int llroundf(float);
extern long long int llround(double);
extern long long int llroundl(long double);
#endif /* !(__DARWIN_NO_LONG_LONG) */
// 返回浮点数x的整数部分
extern float truncf(float);
extern double trunc(double);
extern long double truncl(long double);
truncf(1.9)=1
// 返回x/y的余数
extern float fmodf(float, float);
extern double fmod(double, double);
extern long double fmodl(long double, long double);
fmodf(9,2) = 1
// 返回x/y的余数
extern float remainderf(float, float);
extern double remainder(double, double);
extern long double remainderl(long double, long double);
// 返回x/y的余数和整数商
extern float remquof(float, float, int *);
extern double remquo(double, double, int *);
extern long double remquol(long double, long double, int *);
int a;
int ttt = remquof(4, 3, &a);
最后得倒a=1,ttt=1
// 将y的符号赋值给x并返回具有和y相同符号的x值
extern float copysignf(float, float);
extern double copysign(double, double);
extern long double copysignl(long double, long double);
copysignf(-9, 8) = 9
// 生成一个quient NAN浮点数
extern float nanf(const char *);
extern double nan(const char *);
extern long double nanl(const char *);
// 返回x在y方向上的下一个可表示的浮点数
extern float nextafterf(float, float);
extern double nextafter(double, double);
extern long double nextafterl(long double, long double);
extern double nexttoward(double, long double);
extern float nexttowardf(float, long double);
extern long double nexttowardl(long double, long double);
// 返回x减去y的差如果x>y,否则返回0
extern float fdimf(float, float);
extern double fdim(double, double);
extern long double fdiml(long double, long double);
fdimf(29,3)=26,fdimf(2,3)=0,fdimf(2,2)=0
// 返回x和y中大的数字
extern float fmaxf(float, float);
extern double fmax(double, double);
extern long double fmaxl(long double, long double);
fmaxf(2,1)=2,fmaxf(2,2)=2
// 返回x和y中小的数字
extern float fminf(float, float);
extern double fmin(double, double);
extern long double fminl(long double, long double);
// 浮点乘加运算:w = x*y + z
extern float fmaf(float, float, float);
extern double fma(double, double, double);
extern long double fmal(long double, long double, long double);