sqrt和sqrtf的区别

Difference between sqrt and sqrtf

本文关键字:区别 sqrtf sqrt      更新时间:2023-10-16

我想考虑编码。第一个是:

#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()可以接受doublefloatlong double,而sqrtf()只接受float

正如文档所说,有两个不同版本的唯一原因是因为C不支持重载,所以必须有两个函数。c++允许重载,因此实际上有三个不同版本的sqrt()接受不同大小的浮点实参。

所以在c++中,你的两个代码片段基本上做同样的事情。然而,在C语言上,在sqrt()调用中会有从floatdouble的转换。

C不支持函数重载。这意味着每种类型必须有一个不同的函数。因此sqrt对应double, sqrtf对应float。由于double是C语言中浮点数的"首选"类型,因此"默认"版本是double。它们都是C标准库的一部分,在math.h中。

在c++中,应该使用重载的sqrt(在cmath中定义,在命名空间std中)

在c++中,sqrt函数被重载,可以接受double, afloatlong double作为参数。当你通过一个int,所有可以调用三个,编译器无法选择一个而不是其他人,所以这个称呼是模棱两可的。如果你显式转换从intfloat,当然,三个中的一个是完全匹配的,这使得它比其他两个更好,所以它被称为。

函数sqrtf来自C;在C语言中,没有重载;sqrt是总是sqrt(double), sqrtffloat。因为这个函数没有重载,调用它不会导致歧义。

sqrtf是c的继承。

在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

注意:我使用了一些疯狂的编译器标志来突出硬件上的差异。除非你真的知道你在做什么,否则你可能应该使用更保守的标志。