在iOS開發(fā)中不可避免的需要使用數(shù)學(xué)函數(shù)進(jìn)行計(jì)算邪蛔,我們可以使用C標(biāo)準(zhǔn)庫(kù)頭文件<math.h>中定義的數(shù)學(xué)常量宏及數(shù)學(xué)函數(shù)來(lái)進(jìn)行基本的數(shù)學(xué)計(jì)算操作,這些C函數(shù)都可以在微軟的MSDN
API庫(kù)中找到请毛。(https://msdn.microsoft.com/zh-cn/library ),下面是對(duì)iOS開發(fā)中常用的API整理伟葫。
一官卡、 iOS常用示例如:
//計(jì)算最大值、最小值枷恕、絕對(duì)值
#define MAX(A,B) ((A) > (B) ? (A) : (B))
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#define ABS(A) ((A) < 0 ? (-(A)) : (A))
//計(jì)算scrollView索引
int index = ABS(scrollView.contentOffset.x) / scrollView.frame.size.width;
//四舍五入計(jì)算scrollView索引党晋,更適合
int page =roundf((scrollView.contentOffset.x / scrollView.frame.size.width));
相關(guān)函數(shù)申明請(qǐng)查看頭文件math.h
二、具體函數(shù)釋義如下:
/************************************************************
* *
* Math Functions(數(shù)學(xué)函數(shù)) *
* *
***********************************************************/
#pragma mark --- 三角函數(shù) a前綴表示取反 asin反三角函數(shù)
//TODO: 求反余弦值
extern float acosf(float);
extern double acos(double);
extern long double acosl(long double);
//TODO: 求反正弦值
extern float asinf(float);
extern double asin(double);
extern long double asinl(long double);
//TODO: 求反正切值
extern float atanf(float);
extern double atan(double);
extern long double atanl(long double);
//TODO: 求坐標(biāo)值的反正切值
extern float atan2f(float, float);
extern double atan2(double, double);
extern long double atan2l(long double, long double);
//TODO: 求余弦值
extern float cosf(float);
extern double cos(double);
extern long double cosl(long double);
//TODO: 求正弦值
extern float sinf(float);
extern double sin(double);
extern long double sinl(long double);
//TODO: 求正切值
extern float tanf(float);
extern double tan(double);
extern long double tanl(long double);
//TODO: 求反雙曲余弦值
extern float acoshf(float);
extern double acosh(double);
extern long double acoshl(long double);
//TODO: 求反雙曲正弦值
extern float asinhf(float);
extern double asinh(double);
extern long double asinhl(long double);
//TODO: 求反雙曲正切值
extern float atanhf(float);
extern double atanh(double);
extern long double atanhl(long double);
//TODO: 求雙曲余弦值
extern float coshf(float);
extern double cosh(double);
extern long double coshl(long double);
//TODO: 求雙曲正弦值
extern float sinhf(float);
extern double sinh(double);
extern long double sinhl(long double);
//TODO: 求雙曲正切值
extern float tanhf(float);
extern double tanh(double);
extern long double tanhl(long double);
//TODO: 求取自然數(shù)e的冪
extern float expf(float);
extern double exp(double);
extern long double expl(long double);
//TODO: 計(jì)算參數(shù)以 2 為底的指數(shù)
extern float exp2f(float);
extern double exp2(double);
extern long double exp2l(long double);
//TODO: 計(jì)算基礎(chǔ) e 指數(shù)參數(shù)徐块,但不包括 1
extern float expm1f(float);
extern double expm1(double);
extern long double expm1l(long double);
//TODO: 以e為底的對(duì)數(shù)(log10f則為10為底未玻、log2f:2為底 )
extern float logf(float);
extern double log(double);
extern long double logl(long double);
extern float log10f(float);//以10為底的對(duì)數(shù)
extern double log10(double);
extern long double log10l(long double);
extern float log2f(float);//以2為底的對(duì)數(shù)
extern double log2(double);
extern long double log2l(long double);
//TODO: 計(jì)算基礎(chǔ) e 對(duì)數(shù) 1 加上參數(shù)
extern float log1pf(float);
extern double log1p(double);
extern long double log1pl(long double);
//TODO: 提取 _X 指數(shù),作為一個(gè)帶符號(hào)整數(shù)值為浮點(diǎn)格式
extern float logbf(float);
extern double logb(double);
extern long double logbl(long double);
//TODO: 浮點(diǎn)數(shù)分解為整數(shù)和小數(shù)胡控。返回 _X 的有符號(hào)的小數(shù)部分
extern float modff(float, float *);
extern double modf(double, double *);
extern long double modfl(long double, long double *);
//TODO: 計(jì)算從尾數(shù)和指數(shù)的一個(gè)實(shí)數(shù)
extern float ldexpf(float, int);
extern double ldexp(double, int);
extern long double ldexpl(long double, int);
//TODO: 浮點(diǎn)數(shù)分解尾數(shù)和二為底的指數(shù)
extern float frexpf(float, int *);
extern double frexp(double, int *);
extern long double frexpl(long double, int *);
//TODO: 提取_X 指數(shù)為無(wú)符號(hào)整型值
extern int ilogbf(float);
extern int ilogb(double);
extern int ilogbl(long double);
//TODO: 以 FLT_RADIX乘以_X到乘冪運(yùn)算_Y
extern float scalbnf(float, int);
extern double scalbn(double, int);
extern long double scalbnl(long double, int);
//TODO: 將浮點(diǎn)數(shù)乘以 FLT_RADIX 的整數(shù)冪扳剿。
extern float scalblnf(float, long int);
extern double scalbln(double, long int);
extern long double scalblnl(long double, long int);
//TODO: 浮點(diǎn)數(shù)絕對(duì)值
extern float fabsf(float);
extern double fabs(double);
extern long double fabsl(long double);
//TODO: 計(jì)算參數(shù)的實(shí)立方根
extern float cbrtf(float);
extern double cbrt(double);
extern long double cbrtl(long double);
//TODO: 求值直角三角形的斜邊長(zhǎng)度,給出的兩邊為 x 和 y (換言之昼激,根的長(zhǎng)度 x2 + y2)庇绽。
extern float hypotf(float, float);
extern double hypot(double, double);
extern long double hypotl(long double, long double);
//TODO: 求n的m次方的值,計(jì)算以n為底數(shù)的m次冪
extern float powf(float, float);
extern double pow(double, double);
extern long double powl(long double, long double);
//TODO: 開平方
extern float sqrtf(float);
extern double sqrt(double);
extern long double sqrtl(long double);
//TODO: 函數(shù)返回 x 的高斯誤差函數(shù)
extern float erff(float);
extern double erf(double);
extern long double erfl(long double);
//https://msdn.microsoft.com/zh-cn/library/
//TODO: 函數(shù)返回 x 的補(bǔ)余高斯誤差函數(shù)。
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>
====================== */
#pragma mark ---lgammaf...
//TODO: 計(jì)算伽瑪參數(shù)絕對(duì)值的自然對(duì)數(shù)
extern float lgammaf(float);
extern double lgamma(double);
extern long double lgammal(long double);
//TODO: 計(jì)算_X 的伽瑪函數(shù)
extern float tgammaf(float);
extern double tgamma(double);
extern long double tgammal(long double);
//TODO: 計(jì)算值的上限橙困。向上取整 進(jìn)1
extern float ceilf(float);
extern double ceil(double);
extern long double ceill(long double);
//TODO: 對(duì)參數(shù)向下取整瞧掺,返回參數(shù)的下限。
extern float floorf(float);
extern double floor(double);
extern long double floorl(long double);
//TODO: 通過(guò)使用當(dāng)前舍入方向凡傅,舍入?yún)?shù)為浮點(diǎn)格式的一個(gè)整數(shù)值辟狈,整數(shù)。返回舍入的整數(shù)值。
extern float nearbyintf(float);
extern double nearbyint(double);
extern long double nearbyintl(long double);
//TODO: 將浮點(diǎn)值舍入到最接近的整數(shù)(采用浮點(diǎn)格式)哼转,四舍五入函數(shù)
extern float rintf(float);
extern double rint(double);
extern long double rintl(long double);
//FIXME: 也是四舍五入
extern long int lrintf(float);
extern long int lrint(double);
extern long int lrintl(long double);
//TODO: 四舍五入法
extern float roundf(float);
extern double round(double);
extern long double roundl(long double);
//TODO: 四舍五入明未,取最接近的整數(shù)
extern long int lroundf(float);
extern long int lround(double);
extern long int lroundl(long double);
#pragma mark ---truncf...
/* ======================
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
====================== */
//TODO: 截?cái)鄥?shù)為整數(shù)部分,返回參數(shù)的整數(shù)部分
extern float truncf(float);
extern double trunc(double);
extern long double truncl(long double);
//TODO: 求兩數(shù)整除后的余數(shù)
extern float fmodf(float, float);
extern double fmod(double, double);
extern long double fmodl(long double, long double);
//TODO: 計(jì)算余數(shù) _X 除 _Y
extern float remainderf(float, float);
extern double remainder(double, double);
extern long double remainderl(long double, long double);
//TODO: 計(jì)算第一個(gè)指定參數(shù)除以第二個(gè)指定參數(shù)的余數(shù)壹蔓。也要計(jì)算出第一指定參數(shù)的有效位數(shù)除以第二指定參數(shù)的有效位數(shù)的商亚隅,然后使用在第三參數(shù)中指定的位置返回該商。
extern float remquof(float, float, int *);
extern double remquo(double, double, int *);
extern long double remquol(long double, long double, int *);
//TODO: 用_X的大小和_Y的符號(hào)來(lái)返回值
extern float copysignf(float, float);
extern double copysign(double, double);
extern long double copysignl(long double, long double);
//TODO: 用_X中指示的內(nèi)容返回清掃NaN庶溶,
extern float nanf(const char *);
extern double nan(const char *);
extern long double nanl(const char *);
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);
//TODO: 計(jì)算參數(shù)之間的正整數(shù)差異。(如果 _X 比 _Y 大懂鸵,_X 和 _Y 之間的差異偏螺;否則,+0匆光。)
extern float fdimf(float, float);
extern double fdim(double, double);
extern long double fdiml(long double, long double);
//TODO: 求最大值
extern float fmaxf(float, float);
extern double fmax(double, double);
extern long double fmaxl(long double, long double);
//TODO: 求最小值
extern float fminf(float, float);
extern double fmin(double, double);
extern long double fminl(long double, long double);
//TODO: 計(jì)算第一個(gè)和第二個(gè)指定的參數(shù)的產(chǎn)品套像,然后將第三個(gè)指定的參數(shù)添加到結(jié)果;整個(gè)計(jì)算是作為單個(gè)操作執(zhí)行的终息。表達(dá)式 (_X x _Y) + _Z 的結(jié)果夺巩。整個(gè)計(jì)算都作為單個(gè)操作執(zhí)行,即子表達(dá)式計(jì)算到無(wú)限精度并且僅對(duì)最終結(jié)果四舍五入周崭。
extern float fmaf(float, float, float);
extern double fma(double, double, double);
extern long double fmal(long double, long double, long double);