iOS math.h數學函數

來源:互聯網
上載者:User

標籤:

在實際工作中有些程式不可避免的需要使用數學函數進行計算,比如地圖程式的地理座標到地圖座標的變換。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 常用數學函數

iOS math.h數學函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.