这段代码给了我输出:
The sqrt of 1.234 is: 1.52276 why is this diffrent from: 1.11086 1.11086
为什么?
#include <iostream>
using namespace std;
template<typename T>
T sqrt(T x)
{
return x*x;
}
template<typename T>
void print_sqrt(T x)
{
double tmp = sqrt(x);
cout << "The sqrt of " << x << " is: " << sqrt<double>(x) << " why is this diffrent from: " << sqrt(x) << " " << tmp << endl;
}
int main()
{
print_sqrt<double>(1.234);
system("pause");
}
答案 0 :(得分:8)
std::sqrt
函数(在不提供模板参数时调用的函数)计算平方根,而版本计算平方。