1. 程式人生 > >C++三角函式用法錯誤error C2668: 'tan' : ambiguous call to overloaded function原因及解決方法

C++三角函式用法錯誤error C2668: 'tan' : ambiguous call to overloaded function原因及解決方法

注意tan、atan等三角函式不能接受整數,如:tan(1)會報錯“error C2668: 'tan' : ambiguous call to overloaded function” ,改為浮點型即可正確計算。

1、錯誤程式碼如下:

#include <iostream>
#include <cmath>

using namespace std;
#define PI 3.1415926

int main()
{
	float tanValue = tan(1);
	cout<<"tan(1) = "<<tanValue<<endl;

	float atanValue = atan(1);  
	cout<<"atan(1) = "<<atanValue<<endl;

	cin.get();
	return 0;
}

報錯資訊如下:

2、修改後的正確程式碼如下:

#include <iostream>
#include <cmath>

using namespace std;
#define PI 3.1415926

int main()
{
	float tanValue = tan(1.0f);
	cout<<"tan(1) = "<<tanValue<<endl;

	float atanValue = atan(1.0f);  
	cout<<"atan(1) = "<<atanValue<<endl;

	cin.get();
	return 0;
}

正確輸出結果如下: