iOS math.h数学函数

在实际工作中有些程序不可避免的需要使用数学函数进行计算,比如地图程序的地理坐标到地图坐标的变换。Objective-C做为ANSI C的扩展,使用C标准库头文件<math.h>中定义的数学常量宏及数学函数来实现基本的数学计算操作,所以不必费神再在Cocoa Foundation中寻找相应的函数和类了。这里列出一些常用宏和数学函数,更详细的信息还是需要去查阅<math.h>头文件。

数学常量:
#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

这里没有列出的三角函数也是属于C标准数学函数的一部分,也可以在<math.h>中查阅。

-----------------------------------------------------

需要 引入头文件 #import <math.h>

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 exp (double);求取自然数e的幂 
  double sqrt (double);开平方 
  double log (double); 以e为底的对数 
  double log10 (double);以10为底的对数 
  double pow(double x, double y);计算以x为底数的y次幂 
  float powf(float x, float y); 功能与pow一致,只是输入与输出皆为浮点数 
5 、取整 
   double ceil (double); 取上整 
  double floor (double); 取下整 
6 、绝对值 
  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 [] );计算多项式 
  nt matherr(struct exception *e);数学错误计算处理程序

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)!!!!!!! }

相关链接:

IOS开发常用数学函数

objective-c适用c数学函数 <math.h>

ios 常用数学函数

时间: 2024-08-14 23:32:34

iOS math.h数学函数的相关文章

PHP 的 BC MATH 系列数学函数

一.常见问题 用 PHP 做计算时经常会遇到精度带来的问题,下面来看两个常见的例子: 1. 运算比较 下面表达式输出的结果不是相等: <?php echo 2.01 - 0.01 == 2 ? '相等' : '不相等'; // 不相等 2. 类型转换 下面表达式输出的结果不是201(如果想输出你想要的结果,需要先转 string 再转 int): <?php $num = intval(2.01 * 100); var_dump($num); // int(200) 你也许会觉得很奇怪,然而这

Objective-C中math.h数学计算公式介绍

1 1. 三角函数 2 double sin (double); 正弦 3 double cos (double);余弦 4 double tan (double);正切 5 6 2 .反三角函数 7 double asin (double); 结果介于[-PI/2, PI/2] 8 double acos (double); 结果介于[0, PI] 9 double atan (double); 反正切(主值), 结果介于[-PI/2, PI/2] 10 double atan2 (doubl

Standard C 之 math.h和float.h

对于C Standard Library 可以参考:http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ 或者 http://www.cplusplus.com/reference/ (一) <math.h> 常用函数: 1. 三角函数 double sin(double);正弦 double cos(double);余弦 double tan(double);正切 2 .反三角函数 double asin (double); 结果介于[-PI/2,

常用数学函数篇abs acos asin atan ceil cos exp frexp ldexp log pow sin sinh sqrt tan tanh

abs(计算整型数的绝对值) 相关函数 labs, fabs 表头文件 #include<stdlib.h> 定义函数 int abs (int j) 函数说明 abs()用来计算参数j的绝对值,然后将结果返回. 返回值 返回参数j的绝对值结果. 范例 #ingclude <stdlib.h> main(){ int ansert; answer = abs(-12); printf("|-12| = %d\n", answer); } 执行 |-12| = 1

Unix/Linux环境C编程入门教程(31) 数学函数带你战胜企业面试

1.函数介绍: abs()acos()asin()atan()atan2()ceil()cos()cosh()exp()frexp()ldexp()log()log10()pow()sin()sinh()sqrt()tan()tanh() abs(计算整型数的绝对值) 相关函数 labs, fabs 表头文件 #include<stdlib.h> 定义函数 int abs (int j) 函数说明 abs()用来计算参数j的绝对值,然后将结果返回. 返回值 返回参数j的绝对值结果. 范例 #i

C库数学函数

头文件<math.h> 所有函数的返回值都是double,三角函数的角度用弧度表示. sin(double x).cos(double x).tan(double x).asin(double x).acos(double x).atan(double x).atan2(double y,double x).sinh(double x)(x的双曲正弦值).cosh(double x)(x的双曲余弦值)tanh(double x)(x的双曲正切值)exp(double x).log(double

数学函数&lt;math.h&gt;

数学函数,拿你该怎么办 先看一下能在编程中用到数学函数的情况 int abs(int i) 返回整型参数i的绝对值 double cabs(struct complex znum) 返回复数znum的绝对值 double fabs(double x) 返回双精度参数x的绝对值 long labs(long n) 返回长整型参数n的绝对值 double exp(double x) 返回指数函数e^x的值 double frexp(double value,int *eptr) 返回value=x*

iOS开发中常用的数学函数

/*---- 常用数学公式 ----*/ //指数运算 3^2 3^3 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(@&q

Python 基础学习之: Python math 模块、cmath 模块 区别是 cmath 模块运算的是复数,math 模块运算的是数学运算 Python数学函数列表及解释 Python math 模块提供了许多对浮点数的数学运算函数。 Python cmath 模块包含了一些用于复数运算的函数

Python math 模块.cmath 模块 Python 中数学运算常用的函数基本都在 math 模块.cmath 模块中. Python math 模块提供了许多对浮点数的数学运算函数. Python cmath 模块包含了一些用于复数运算的函数. cmath 模块的函数跟 math 模块函数基本一致,区别是 cmath 模块运算的是复数,math 模块运算的是数学运算. 要使用 math 或 cmath 函数必须先导入: import math 查看 math 查看包中的内容: impo