iOS_数学函数Math Functions
floor/ceil/round 向下取整/向上取整/四舍五入
取整实例1:
double d1 =10.12, d2 = 10.88;
float f1 =10.19893, f2 = 10.77;
DLog(@"floor_d = %f, floor_f = %f, ceil_d = %f, ceil_f = %f, round_d = %f, round_f = %f", floor(d1), floorf(f1), ceil(d1), ceilf(f1), round(d1), roundf(f1));
//输出 floor_d = 10.000000, floor_f = 10.000000, ceil_d = 11.000000, ceil_f = 11.000000, round_d = 10.000000, round_f = 10.000000
DLog(@"floor_d2 = %f, floor_f2 = %f, ceil_d2 = %f, ceil_f2 = %f, round_d2 = %f, round_f2 = %f", floor(d2), floorf(f2), ceil(d2), ceilf(f2), round(d2), roundf(f2));
//输出 floor_d2 = 10.000000, floor_f2 = 10.000000, ceil_d2 = 11.000000, ceil_f2 = 11.000000, round_d2 = 11.000000, round_f2 = 11.000000
//取整与取余
float f1 =0.0;
DLog(@"8.99的小数部分:modff(2.0, &f1) = %.2f, 整数部分:f1 = %.2f",modff(8.99, &f1), f1);
//输出 8.99的小数部分:modff(2.0, &f1) = 0.99, 整数部分:f1 = 8.00
DLog(@"fmodf(9.0, 2.0) = %f, fmodf(10.0, 2.0) = %f", fmodf(9.0,2.0), fmodf(10.0,2.0)); 取余
//输出 fmodf(9.0, 2.0) = 1.000000, fmodf(10.0, 2.0) = 0.000000
三角函数:
float f1 =10.19893;
DLog(@"acosf = %f, asinf = %f, atanf = %f", acosf(f1), asinf(f1), atanf(f1));
//输出 acosf = nan, asinf = nan, atanf = 1.473059
DLog(@"cosf = %f, sinf = %f, tanf = %f", cosf(f1), sinf(f1), tanf(f1));
//输出 cosf = -0.715014, sinf = -0.699110, tanf = 0.977757
DLog(@"acoshf = %f, asinhf = %f, atanhf = %f", acoshf(f1), asinhf(f1), atanhf(f1));
//输出 acoshf = 3.013018, asinhf = 3.017825, atanhf = nan
对数与指数
float f1 =100.0, f2 = 4.0;
DLog(@"对数: logf(100) = %f, log10f(100) = %f, log2f(4) = %f", logf(f1), log10f(f1), log2f(f2));
//输出 对数: logf(100) = 4.605170, log10f(100) = 2.000000, log2f(4) = 2.000000
DLog(@"指数: pow(2, 3) = %f", pow(2, 3));
//输出 指数: pow(2, 3) = 8.000000
DLog(@"开平方:sqrt(100) = %f", sqrt(f1));
//输出 开平方:sqrt(100) = 10.000000
float x =16.4, y;
int n;
y = frexpf(x, &n);
DLog(@"y = frexpf(%f, &n) = %f, n = %d", x, y, n);
//输出 y = frexpf(16.400000, &n) = 0.512500, n = 5
//即 16.4 = 0.5125 * 2^5
DLog(@"5 * 2^3 = ldexpf(5, 3) = %f",ldexpf(5,3));
// 5 * 2^3 = ldexpf(5, 3) = 40.000000
/******************************************************************************
* *
* Math Functions *
* *
******************************************************************************/
————————————————————————————————
反三角函数
extern float acosf(float);
extern double acos(double);
extern longdouble acosl(longdouble); 结果介于[0,PI]
extern float asinf(float); 结果介于[-PI/2, PI/2]
extern float atanf(float); 反正切(主值),结果介于[-PI/2, PI/2]
extern float atan2f(float,float); 反正切(整圆值),结果介于[-PI, PI]
三角函数
extern float cosf(float);余弦
extern float sinf(float);正弦
extern float tanf(float);正切
双曲三角函数
extern float acoshf(float);双曲反余弦(Hyperbolic arc cosine)函数
extern float asinhf(float);双曲反正弦(Hyperbolic arc sine)函数
extern float atanhf(float);双曲反正切(Hyperbolic arc tangent)函数
extern float coshf(float); 双曲余弦(Hyperbolic cosine)函数
extern float sinhf(float); 双曲正弦(Hyperbolic sine)函数
extern float tanhf(float); 双曲正切(Hyperbolic tangent)函数
————————————————————————————————
指数与对数
extern float expf(float); 求取自然数e的幂
extern float exp2f(float); 计算以2为底的指数
extern float expm1f(float);以 2为底的指数结果再减 1
extern float logf(float);以e为底的对数
extern float log10f(float);以10为底的对数
extern float log2f(float);以2为底的对数
extern float log1pf(float); 计算 1与给定值 x 的和(1+x)的自然对数(ln(1+x))
extern float logbf(float);返回以 FLT_RADIX 为底,|x| 的对数值,返回值为浮点数
extern float modff(float,float *); 取整与取余 , 例,float f1 =0.0; float f2 = modff(8.99, &f1); 结果:f1 = 8 f2 = 0.99
extern float ldexpf(float,int); 计算 参数1乘以2的 参数2次幂 // 5 * 2^3 = ldexpf(5, 3) = 40.000000
externfloat frexpf(float,int *);把一个浮点数分解为尾数和2为底的指数, 例 16.4 = 0.5125*2^5
extern int ilogbf(float);返回以 FLT_RADIX为底,|x| 的对数值,返回值为整数
extern float scalbnf(float,int); x * FLT_RADIXy
extern float scalblnf(float,long int); 参数1乘以FLT_RADIX的整数幂 x * FLT_RADIXy
extern float fabsf(float); 绝对值
extern float cbrtf(float);计算参数的立方根 3√x
extern float hypotf(float,float); 计算两个数平方的和的平方根(√x2+y2)(求直角三角形的斜边长度)
extern float powf(float,float); 计算以m为底数的n次幂, powf(m, n)
extern float sqrtf(float); 开平方
extern float erff(float);返回x的高斯误差函数
extern float erfcf(float); 返回x的补余高斯误差函数
/* 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
extern float lgammaf(float);γ 函数的自然对数
extern float tgammaf(float);γ 函数 (伽玛函数)
————————————————————————————————
浮点数的最近整数操作
extern float ceilf(float); 向上取整
extern float floorf(float); 向下取整
externfloat nearbyintf(float);通过使用当前舍入方向,舍入参数为浮点格式的一个整数值,整数。返回舍入的整数值。
extern float rintf(float);四舍五入
extern longint lrintf(float);四舍五入
extern float roundf(float);四舍五入
extern longint lroundf(float);四舍五入
/* 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 longlong int llrintf(float);
extern longlong int llroundf(float);
#endif /* !(__DARWIN_NO_LONG_LONG) */
extern float truncf(float);幅度(到 0的距离,即绝对值)不大于给定值的最近整数
extern float fmodf(float,float); 获得浮点数除法操作的余数
extern float remainderf(float,float); 获得浮点数除法操作的带符号余数
extern float remquof(float,float, int *); 获得浮点数除法操作的带符号余数,且返回符号及操作结果的最后三位组成的整数
externfloat copysignf(float,float);用参数1的大小和参数2的符号来返回值 //输出 copysignf(2.5, -1.0) = -2.500000, copysignf(4.0, 3) = 4.000000
extern float nanf(constchar *); 将执行时定义的字符串作为静态化非数型(Quiet NaN)操作所需的值
extern float nextafterf(float,float); 返回x之后y方向上的下一个可描述(Representable)值
extern double nexttoward(double,long double);返回 x 之后y 方向上的下一个可描述值
externfloat fdimf(float,float);计算两参数之间正整数差异//输出 fdimf(2.5, 3.7) = 0.000000, fdimf(5.4, 2.2) = 3.200000
extern float fmaxf(float,float); 求最大值
extern float fminf(float,float); 求最小值
extern float fmaf(float,float, float);乘累加(Multiply-add)
#define isgreater(x, y) __builtin_isgreater((x),(y))
#define isgreaterequal(x, y) __builtin_isgreaterequal((x),(y))
#define isless(x, y) __builtin_isless((x),(y))
#define islessequal(x, y) __builtin_islessequal((x),(y))
#define islessgreater(x, y) __builtin_islessgreater((x),(y))
#define isunordered(x, y) __builtin_isunordered((x),(y))
/* Legacy BSD API: please use C99 INFINITY macro instead. */
extern float __inff(void)__OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_9, __IPHONE_NA, __IPHONE_NA);
extern double __inf(void)__OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_9, __IPHONE_NA, __IPHONE_NA);
extern longdouble __infl(void)__OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_9, __IPHONE_NA, __IPHONE_NA);
/* Implementation detail; please use the standard C NAN macro instead. */
extern float __nan(void)__OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_NA);
参考:http://classfoo.com/ccby/article/hoTQby
http://www.cnblogs.com/zhao-jie-li/p/5559600.html
https://msdn.microsoft.com/zh-cn/library