角度轉弧度,弧度轉角度
//弧度轉角度
#define Radians_To_Degrees(radians) ((radians) * (180.0 / M_PI))
//角度轉弧度
#define Degrees_To_Radians(angle) ((angle) / 180.0 * M_PI)
eg:計算三角形的三個點
CGFloat R = 200 ;
CGFloat oirin_y = 100 ;
CGPoint point0 = CGPointMake(self.view.frame.size.width/2.0, 0+oirin_y);
CGPoint point1 = CGPointMake(self.view.frame.size.width/2.0 - R/2.0, cos(Degrees_To_Radians(30))*R + oirin_y);
CGPoint point2 = CGPointMake(self.view.frame.size.width/2.0 + R/2.0, cos(Degrees_To_Radians(30))*R + oirin_y);
1、 三角函數(shù)
double sin (double);正弦
double cos (double);余弦
double tan (double);正切
2 、反三角函數(shù)
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 、雙曲三角函數(shù)
double sinh (double);
double cosh (double);
double tanh (double);
4 、指數(shù)與對數(shù)
double exp (double);求取自然數(shù)e的冪
double sqrt (double);開平方
double log (double); 以e為底的對數(shù)
double log10 (double);以10為底的對數(shù)
double pow(double x, double y);計算以x為底數(shù)的y次冪
float powf(float x, float y); 功能與pow一致,只是輸入與輸出皆為浮點數(shù)
5 、取整
double ceil (double); 取上整
double floor (double); 取下整
6 、絕對值
double fabs (double);求絕對值
double cabs(struct complex znum) ;求復數(shù)的絕對值
7 、標準化浮點數(shù)
double frexp (double f, int p); 標準化浮點數(shù), 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); 將參數(shù)的整數(shù)部分通過指針回傳, 返回小數(shù)部分
double fmod (double, double); 返回兩參數(shù)相除的余數(shù)
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);數(shù)學錯誤計算處理程序
objective-c適用c數(shù)學函數(shù) <math.h>
轉載:http://hi.baidu.com/delphi9527/blog/item/2f29650d1302f2306159f319.html
在實際工作中有些程序不可避免的需要使用數(shù)學函數(shù)進行計算,比如地圖程序的地理坐標到地圖坐標的變換。Objective-C做為ANSI C的擴展,使用C標準庫頭文件<math.h>中定義的數(shù)學常量宏及數(shù)學函數(shù)來實現(xiàn)基本的數(shù)學計算操作,所以不必費神再在Cocoa Foundation中尋找相應的函數(shù)和類了。這里列出一些常用宏和數(shù)學函數(shù),更詳細的信息還是需要去查閱<math.h>頭文件。
數(shù)學常量:
#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)
常用函數(shù):
//指數(shù)運算
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
這里沒有列出的三角函數(shù)也是屬于C標準數(shù)學函數(shù)的一部分,也可以在<math.h>中查閱。