写在前面,很基础,但是很重要
要求传入的数据类型为long double
,那么输出这种类型的数据要用%Lf
的方式
要求传入的数据类型为double
,那么输出这种类型的数据要用%f
的方式
要求传入的数据类型为float
,那么输出这种类型的数据要用lf
的方式
言归正传,往下看
1.cbrtf()
开立方根。例如27的立方根为3。
float value = 27;
NSLog(@"cbrtf(27)的值为%.f",cbrtf(value) );

2.sqrt()
开平方根。例如的16平方根为4。
float value = 16;
NSLog(@"sqrt(16)的值为%.f",sqrt(value) );

3.abs() 、labs()
整数绝对值
int a = -14;
int b = -8;
NSLog(@"\n abs(-14)的值为%d, \n labs(-8)的值为%ld",abs(a),labs(b) );

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);

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) );

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));

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));

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));

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));

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));

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));

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);

网友评论