美文网首页
Objective-C中的数学函数math.h

Objective-C中的数学函数math.h

作者: 翼须付出 | 来源:发表于2018-10-22 14:50 被阅读0次

    数学函数库,一些数学计算的公式的具体实现是放在math.h里,具体有:

    /******************************************************************
     *                        Math Functions                          *
     ******************************************************************/
    
    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);
        
    extern float expf(float);
    extern double exp(double);
    extern long double expl(long double);
    
    extern float exp2f(float);
    extern double exp2(double); 
    extern long double exp2l(long double); 
    
    extern float expm1f(float);
    extern double expm1(double); 
    extern long double expm1l(long double); 
    
    extern float logf(float);
    extern double log(double);
    extern long double logl(long double);
    
    extern float log10f(float);
    extern double log10(double);
    extern long double log10l(long double);
    
    extern float log2f(float);
    extern double log2(double);
    extern long double log2l(long double);
    
    extern float log1pf(float);
    extern double log1p(double);
    extern long double log1pl(long double);
    
    extern float logbf(float);
    extern double logb(double);
    extern long double logbl(long double);
    
    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 *);
    
    extern int ilogbf(float);
    extern int ilogb(double);
    extern int ilogbl(long double);
    
    extern float scalbnf(float, int);
    extern double scalbn(double, int);
    extern long double scalbnl(long double, int);
    
    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);
    
    extern float cbrtf(float);
    extern double cbrt(double);
    extern long double cbrtl(long double);
    
    extern float hypotf(float, float);
    extern double hypot(double, double);
    extern long double hypotl(long double, long double);
    
    extern float powf(float, float);
    extern double pow(double, double);
    extern long double powl(long double, long double);
    
    extern float sqrtf(float);
    extern double sqrt(double);
    extern long double sqrtl(long double);
    
    extern float erff(float);
    extern double erf(double);
    extern long double erfl(long double);
    
    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);
    
    extern float tgammaf(float);
    extern double tgamma(double);
    extern long double tgammal(long double);
    
    extern float ceilf(float);
    extern double ceil(double);
    extern long double ceill(long double);
    
    extern float floorf(float);
    extern double floor(double);
    extern long double floorl(long double);
    
    extern float nearbyintf(float);
    extern double nearbyint(double);
    extern long double nearbyintl(long double);
    
    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) */
    
    extern float truncf(float);
    extern double trunc(double);
    extern long double truncl(long double);
    
    extern float fmodf(float, float);
    extern double fmod(double, double);
    extern long double fmodl(long double, long double);
    
    extern float remainderf(float, float);
    extern double remainder(double, double);
    extern long double remainderl(long double, long double);
    
    extern float remquof(float, float, int *);
    extern double remquo(double, double, int *);
    extern long double remquol(long double, long double, int *);
    
    extern float copysignf(float, float);
    extern double copysign(double, double);
    extern long double copysignl(long double, long double);
    
    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);
    
    extern float fdimf(float, float);
    extern double fdim(double, double);
    extern long double fdiml(long double, long double);
    
    extern float fmaxf(float, float);
    extern double fmax(double, double);
    extern long double fmaxl(long double, long double);
    
    extern float fminf(float, float);
    extern double fmin(double, double);
    extern long double fminl(long double, long double);
    
    extern float fmaf(float, float, float);
    extern double fma(double, double, double);
    extern long double fmal(long double, long double, long double);
    
    #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))
    

    常用函数:

    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 frexp(double value,int *exp);这是一个将value值拆分成小数部分f和(以2为底的)指数部分exp,并返回小数部分f,即f*2^exp。其中f取值在0.5~1.0范围或者0。
    double ldexp(double x,int exp);这个函数刚好跟上面那个frexp函数功能相反,它的返回值是x*2^exp
    double modf(double value,double *iptr);拆分value值,返回它的小数部分,iptr指向整数部分。
    double log (double); 以e为底的对数
    double log10 (double);以10为底的对数
    double pow(double x,double y);计算x的y次幂
    float powf(float x,float y); 功能与pow一致,只是输入与输出皆为浮点数
    double exp (double);求取自然数e的幂
    double sqrt (double);开平方
    
    5 、取整
    double ceil (double); 取上整,返回不比x小的最小整数
    double floor (double); 取下整,返回不比x大的最大整数,即[高斯函数](http://baike.baidu.com/view/856844.htm)[x]
    
    6 、绝对值
    int abs(int i); 求整型的绝对值
    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 []);计算多项式
    int matherr(struct exception *e);数学错误计算处理程序
    source: 《C & C++ Code Capsules》
    
    10、取绝对值   
    int abs(int i);                   // 处理int类型的取绝对值
    double fabs(double i); //处理double类型的取绝对值
    float fabsf(float i);           /处理float类型的取绝对值
    
    11、取随机数
    Objective-C 没有提供相关的函数生成随机数,不过C供了rand(), srand(), random(), srandom(), arc4random(),randomize()几个函数。要引用头文件#include<stdlib.h>
    其中,random()和randomize()函数的使用的方法分别与rand()和srand()函数的使用方法对应类似。 arc4random()不用seed
    1)   srand((unsigned)time(0));  //不加这句每次产生的随机数不变         int i = rand() % 5;      
    2)   srandom(time(0));         int i = random() % 5;
    3)   int i = arc4random() % 5 ;
    注:rand()和random()实际并不是一个真正的伪随机数发生器,在使用之前需要先初始化随机种子,否则每次生成的随机数一样。 arc4random() 是一个真正的伪随机算法,不需要生成随机种子,因为第一次调用的时候就会自动生成。而且范围是rand()的两倍。在iPhone中,RAND_MAX是 0x7fffffff (2147483647),而arc4random()返回的最大值则是 0x100000000 (4294967296)。
    精确度比较:arc4random()  >  random()  >  rand()。
    常用方法:arc4random
    1)获取一个随机整数范围在:[0,100)包括0,不包括100 int x = arc4random() % 100;
    2) 获取一个随机数范围在:[500,1000),包括500,不包括1000 int y = (arc4random() % 501) + 500;
    3)获取一个随机整数,范围在[from,to),包括from,不包括to -(int)getRandomNumber:(int)from to:(int)to {     return (int)(from + (arc4random() % (to – from + 1))); //+1,result is [from to]; else is [from, to)!!!!!!! }
    

    数学常量:

    #define M_E         2.71828182845904523536028747135266250   // e
    #define M_LOG2E     1.44269504088896340735992468100189214   // log 2e
    #define M_LOG10E    0.434294481903251827651128918916605082  // log 10e
    #define M_LN2       0.693147180559945309417232121458176568  // log e2
    #define M_LN10      2.30258509299404568401799145468436421   // log e10
    #define M_PI        3.14159265358979323846264338327950288   // pi
    #define M_PI_2      1.57079632679489661923132169163975144   // pi/2
    #define M_PI_4      0.785398163397448309615660845819875721  // pi/4
    #define M_1_PI      0.318309886183790671537767526745028724  // 1/pi
    #define M_2_PI      0.636619772367581343075535053490057448  // 2/pi
    #define M_2_SQRTPI  1.12837916709551257389615890312154517   // 2/sqrt(pi)
    #define M_SQRT2     1.41421356237309504880168872420969808   // sqrt(2)
    #define M_SQRT1_2   0.707106781186547524400844362104849039  // 1/sqrt(2)
    

    常用函数:

    //指数运算
    NSLog(@"%.f", pow(3,2) ); //result 9
    NSLog(@"%.f", pow(3,3) ); //result 27
    
    //开平方运算
    NSLog(@"%.f", sqrt(16) ); //result 4
    NSLog(@"%.f", sqrt(81) ); //result 9
    
    //上舍入
    NSLog(@"res: %.f", ceil(3.000000000001)); //result 4
    NSLog(@"res: %.f", ceil(3.00)); //result 3
    
    //下舍入
    NSLog(@"res: %.f", floor(3.000000000001)); //result 3
    NSLog(@"res: %.f", floor(3.9999999)); //result 3
    
    //四舍五入
    NSLog(@"res: %.f", round(3.5)); //result 4
    NSLog(@"res: %.f", round(3.46)); //result 3
    NSLog(@"res: %.f", round(-3.5)); //NB: this one returns -4
    
    //最小值
    NSLog(@"res: %.f", fmin(5,10)); //result 5
    
    //最大值
    NSLog(@"res: %.f", fmax(5,10)); //result 10
    
    //绝对值
    NSLog(@"res: %.f", fabs(10)); //result 10
    NSLog(@"res: %.f", fabs(-10)); //result 10
    
    NSArray * arr = [NSArray arrayWithObjects:@"10",@"50",@"9", nil];
    NSInteger max = [[arr valueForKeyPath:@"@max.intValue"] integerValue];
    max = max;//50
    //另一种思路,在一些情况下可以很快获取,同样去最小值用min,要比循环方便多了,如果数组中放的是其他对象也可以用@max.property尽心筛选,不过类型都是NSNumber,没测试过其他类型
    

    iOS开发

    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() ----求反双曲正切值
    

    输出语法

    %@                   对象
    %d, %i               整数
    %u                    无符整形
    %f                     浮点/双字
    %x, %X              二进制整数
    %o                    八进制整数
    %zu size_t
    %p                    指针
    %e                    浮点/双字 (科学计算)
    %g                    浮点/双字
    %s C                字符串
    %.*s                  Pascal字符串
    %c                    字符
    %C                    unichar
    %lld                   64位长整数(long long)
    %llu                   无符64位长整数
    %Lf                    64位双字
    
    结构体输出
    NSStringFromCGPoint
    NSStringFromCGSize
    NSStringFromCGRect
    NSStringFromCGAffineTransform
    NSStringFromUIEdgeInsets
    例如:
    NSLog(@"rect1: %@", NSStringFromCGRect(rect1));
    

    相关文章

      网友评论

          本文标题:Objective-C中的数学函数math.h

          本文链接:https://www.haomeiwen.com/subject/ctqgzftx.html