sqrt和sqrtf的区别
Difference between sqrt and sqrtf
我想考虑编码。第一个是:
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main() {
int s = 25;
cout << sqrt(s) << endl;
return 0;
}
它给了我这个错误:
>c:usersdatuashvilidocumentsvisual studio 2010projectstrainingtrainingtraining.cpp(9): error C2668: 'sqrt' : ambiguous call to overloaded function
1> c:program filesmicrosoft visual studio 10.0vcincludemath.h(589): could be 'long double sqrt(long double)'
1> c:program filesmicrosoft visual studio 10.0vcincludemath.h(541): or 'float sqrt(float)'
1> c:program filesmicrosoft visual studio 10.0vcincludemath.h(127): or 'double sqrt(double)'
1> while trying to match the argument list '(int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
如果我在s前面加上type float,像这样:
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main() {
int s = 25;
cout << sqrt((float)s) << endl;
return 0;
}
我猜是5
。另一种变体是,如果我写sqrtf
,而不是sqrt
,
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main(){
int s=25;
cout << sqrtf((float)s) << endl;
return 0;
}
我也得到了5
。
它们之间的区别是什么?这是否意味着sqrtf与浮点类型的sqrt相同?
以下是sqrt()
和sqrtf()
的MSDN文档页面,解释了差异:
倍根号,sqrtf
计算平方根
参数double sqrt( double x ); float sqrt( float x ); // C++ only long double sqrt( long double x ); // C++ only float sqrtf( float x );
x:非负浮点值
评论
c++允许重载,所以用户可以调用重载sqrt接受浮点或长双精度类型。在C程序中,sqrt总是接受并返回double
sqrt函数返回x的平方根默认情况下,sqrt返回一个不定数。
所以c++的不同之处在于sqrt()
可以接受double
、float
或long double
,而sqrtf()
只接受float
。
正如文档所说,有两个不同版本的唯一原因是因为C不支持重载,所以必须有两个函数。c++允许重载,因此实际上有三个不同版本的sqrt()
接受不同大小的浮点实参。
所以在c++中,你的两个代码片段基本上做同样的事情。然而,在C语言上,在sqrt()
调用中会有从float
到double
的转换。
C不支持函数重载。这意味着每种类型必须有一个不同的函数。因此sqrt
对应double
, sqrtf
对应float
。由于double
是C语言中浮点数的"首选"类型,因此"默认"版本是double
。它们都是C标准库的一部分,在math.h
中。
在c++中,应该使用重载的sqrt
(在cmath
中定义,在命名空间std
中)
在c++中,sqrt
函数被重载,可以接受double
, afloat
或long double
作为参数。当你通过一个int
,所有可以调用三个,编译器无法选择一个而不是其他人,所以这个称呼是模棱两可的。如果你显式转换从int
到float
,当然,三个中的一个是完全匹配的,这使得它比其他两个更好,所以它被称为。
函数sqrtf
来自C;在C语言中,没有重载;sqrt
是总是sqrt(double)
, sqrtf
取float
。因为这个函数没有重载,调用它不会导致歧义。
sqrtf
是c的继承。
sqrt
函数对三种浮点类型有不同的重载。相反,在C中没有重载,因此必须通过函数名来区分不同版本的平方根函数;因此,我们有sqrt
(在C中仅适用于double
s)和sqrtf
用于float
s。
sqrtf()
比sqrt()
快
…但是你要付出精度的代价。
C:Usersceilingcat> type f.cc
#include<iostream>
#include<cmath>
int main(){
double f=0;
int i=0;
for( ; i <= 2000000000; f+=sqrt(i++) );
std::cout.precision( 20 );
std::cout<<f<<'n';
}
C:Usersceilingcat> cl f.cc /Ox /arch:AVX2 /fp:fast /Qfast_transcendentals
C:Usersceilingcat> time ./f.exe
59628479422355.53125
real 0m3.846s
user 0m0.000s
sys 0m0.015s
C:Usersceilingcat> type g.cc
#include<iostream>
#include<cmath>
int main(){
double f=0;
int i=0;
for( ; i <= 2000000000; f+=sqrtf(i++) );
std::cout.precision( 20 );
std::cout<<f<<'n';
}
C:Usersceilingcat> cl g.cc /Ox /arch:AVX2 /fp:fast /Qfast_transcendentals
C:Usersceilingcat> time ./g.exe
59628479422157.773438
real 0m2.104s
user 0m0.000s
sys 0m0.000s
注意:我使用了一些疯狂的编译器标志来突出硬件上的差异。除非你真的知道你在做什么,否则你可能应该使用更保守的标志。
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- C++ - "!pointer"和"pointer == nullptr"的区别?
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- 返回递归调用和仅递归调用的区别
- Qt:remove() 和 rmdir() 有什么区别
- 这 4 个 lambda 表达式之间有什么区别?
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- (double) 和 double() 之间的区别
- 将向量作为类>(值)<向量启动和向量<类>[值]有什么区别
- typedef 枚举和枚举类有什么区别?
- &C::c 和 &(C::c) 有什么区别?
- sqrt和sqrtf的区别