C ++中的acos()函數(shù)以弧度形式返回數(shù)字(參數(shù))的反余弦值。
此函數(shù)在<cmath>頭文件中定義。
[數(shù)學] cos-1x = acos(x) [C++];
double acos(double x); float acos(float x); long double acos(long double x); double acos (T x); //為整型
acos()函數(shù)采用[-1,1]范圍內(nèi)的單個強制性參數(shù)。這是因為余弦值在1到-1的范圍內(nèi)。
假設參數(shù)在[-1,1]范圍內(nèi),則acos()函數(shù)返回[0,π]范圍內(nèi)的值。
如果參數(shù)大于1或小于-1,則acos()返回,NaN即不是數(shù)字。
參數(shù)(x) | 返回值 |
---|---|
x = [-1,1] | [0,π]以弧度為單位 |
-1> x或x> 1 | NaN(非數(shù)字) |
#include <iostream> #include <cmath> using namespace std; int main() { double x = 0.0, result; result = acos(x); cout << "acos(x) = " << result << " radians" << endl; cout << "acos(x) = " << result*180/3.1415 << " degrees" << endl; return 0; }
運行該程序時,輸出為:
acos(x) = 1.5708 radians acos(x) = 90.0027 degrees
#include <iostream> #include <cmath> using namespace std; int main() { int x = -1; double result; result = acos(x); cout << "acos(x) = " << result << " radians" << endl; // 將結(jié)果轉(zhuǎn)換為角度 cout << "acos(x) = " << result*180/3.1415 << " degrees"; return 0; }
運行該程序時,輸出為:
acos(x) = 3.14159 radians acos(x) = 180.005 degrees