美文网首页基础应用
iOS 最有可能用到的数学函数

iOS 最有可能用到的数学函数

作者: CoderZb | 来源:发表于2018-11-28 15:45 被阅读5次

写在前面,很基础,但是很重要

要求传入的数据类型为long double,那么输出这种类型的数据要用%Lf的方式
要求传入的数据类型为double,那么输出这种类型的数据要用%f的方式
要求传入的数据类型为float,那么输出这种类型的数据要用lf的方式


言归正传,往下看

1.cbrtf()开立方根。例如27的立方根为3。
float value = 27;
NSLog(@"cbrtf(27)的值为%.f",cbrtf(value) );
image.png
2.sqrt()开平方根。例如的16平方根为4。
float value = 16;
NSLog(@"sqrt(16)的值为%.f",sqrt(value) );
image.png
3.abs() 、labs()整数绝对值
    int a = -14;
    int b = -8;
    NSLog(@"\n abs(-14)的值为%d, \n labs(-8)的值为%ld",abs(a),labs(b) );
image.png
4.rand()、random()、arc4random()随机数iOS 随机数rand()、random()、arc4random()用法
    srand((unsigned)time(0));// 使用rand()函数,前面要加这句代码。unsigned无符号型,修饰int、char。
    int i = rand() % 10;
    NSLog(@"rand()函数[0,10)出来的随机数为%d",i);

    srandom((unsigned)time(0));// 使用srandom()函数,前面要加这句代码。unsigned无符号型,修饰int、char。
    int j = random() % 20;
    NSLog(@"random()函数[0,20)出来的随机数为%d",j);

    int k = arc4random() % 30 ;
    NSLog(@"arc4random()函数[0,20)出来的随机数为%d",k);
image.png
5.fabs() / fabsf() / fabsl()浮点数绝对值
        float a = -17.32;
        float b = -5.21;
        float c = -2.73;
        NSLog(@"\n fabs(-17.32)的值为%f, \n labs(-5.21)的值为%lf \n labs(-2.73)的值为%Lf",fabs(a),fabsf(b),fabsl(c) );
image.png
6.fmax() / fmaxf() / fmaxl()求两个数的最大值
NSLog(@"\n fmax(5.2,7.9)的值为%f  \n fmaxl(5.2,7.9)的值为%lf \n fmaxl(5.2,7.9)的值为%Lf \n",fmax(5.2,7.9),fmaxf(5.2,7.9),fmaxl(5.2,7.9));
image.png
7.fmin() / fminf() / fminl()求两个数的最小值
NSLog(@"\n fmin(5.2,7.9)的值为%f  \n fminl(5.2,7.9)的值为%lf \n fminl(5.2,7.9)的值为%Lf \n",fmin(5.2,7.9),fminf(5.2,7.9),fminl(5.2,7.9));
image.png
8.fmod() / fmodf() / fmodl()求两数整除后的余数
NSLog(@"\n fmodf(9,2)的值为%f  \n fmod(9,2)的值为%lf \n fmodl(9,2)的值为%Lf \n",fmodf(9,2),fmod(9,2),fmodl(9,2));
image.png
9.round() / roundf() / roundl()四舍五入
    float a = 99.2983;
    float b = 99.7983;
    float c = 100.2983;
    float d = 100.7983;
    NSLog(@"\n roundf(99.2983)的值为 %.2f,\n roundf(99.7983)的值为 %.2f,\n roundf(100.2983)的值为 %.2f,\n roundf(100.7983)的值为 %.2f",roundf(a),roundf(b),roundf(c),roundf(d));
image.png
ceil() / ceilf() / ceill()向上取整iOS 保留两位小数并且向上取整
    float a = 99.2983;
    float b = 99.7983;
    float c = 100.2983;
    float d = 100.7983;
    NSLog(@"\n ceilf(99.2983)的值为 %.2f,\n ceilf(99.7983)的值为 %.2f,\n ceilf(100.2983)的值为 %.2f,\n ceilf(100.7983)的值为 %.2f",ceilf(a),ceilf(b),ceilf(c),ceilf(d));
image.png
floor() / floorf() / floorl()向下取整
    float a = 99.2983;
    float b = 99.7983;
    float c = 100.2983;
    float d = 100.7983;
    NSLog(@"\n floorf(99.2983)的值为 %.2f,\n floorf(99.7983)的值为 %.2f,\n floorf(100.2983)的值为 %.2f,\n floorf(100.7983)的值为 %.2f",floorf(a),floorf(b),floorf(c),floorf(d));
image.png
modf() / modff() / modfl()浮点数分解为整数和小数
    float a =0.0;
    NSLog(@"\n 7.63的\n 小数部分:modff(7.63, &a) = %.2f, \n 整数部分:a = %.2f",modff(7.63, &a), a);
image.png



相关文章

  • iOS 随机数rand()、random()、arc4rando

    rand() random() arc4random() 可以看看我的这篇文章,iOS 最有可能用到的数学函数

  • iOS 最有可能用到的数学函数

    写在前面,很基础,但是很重要 要求传入的数据类型为long double,那么输出这种类型的数据要用%Lf的方式要...

  • ios 中的数学函数

    在学习Python中应用到很多数学函数,回到ios中验证ios中也有很多数学函数,之前只用过 取整数 ,取最大最小...

  • 常用函数

    mark:iOS开发中可能会用到的一些函数 iOS中的round/ceil/floorf函数略解

  • [Math] unity 可能用到的数学函数

    Mathf.Abs 绝对值 计算并返回指定参数 f 绝对值。 Mathf.Acos 反余弦 static func...

  • 数学基础(高等数学)

    一些在机器学习中可能用到的数学基础以及常用的数学公式 机器学习中常见的函数 符号函数 取整函数 狄利克雷函数 取最...

  • iOS 笔试题小记数学函数

    iOS_数学函数Math Functions(计算,取整,平方立方,逆序运算,排序等等) 参考资料:IOS数学函数...

  • iOS开发中可能会用到的一些函数

    参考文档: iOS开发中可能会用到的一些函数 工程中代码:ceil() / ceilf() / ceill() -...

  • iOS中常用的C函数

    iOS中经常会用到与数学上相关的一些函数,这些函数在C语言中都可以找到,下面列举一些比较常用的函数。 1.随机数 ...

  • undefined reference to `sqrt'

    在gcc下用到数学函数,如sqrt。在gcc时要加上 -lm 参数,这样告诉编译器我要用到数学函数了 。 http...

网友评论

    本文标题:iOS 最有可能用到的数学函数

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