sqrt和sqrtf之间的差异 [英] Difference between sqrt and sqrtf

查看:566
本文介绍了sqrt和sqrtf之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想考虑编码。首先是:

I want to consider to code. First it is:

#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main() {
    int s = 25;
    cout << sqrt(s) << endl;
    return 0;
}

它给我这个错误:

>c:\users\datuashvili\documents\visual studio 2010\projects\training\training\training.cpp(9): error C2668: 'sqrt' : ambiguous call to overloaded function
1>          c:\program files\microsoft visual studio 10.0\vc\include\math.h(589): could be 'long double sqrt(long double)'
1>          c:\program files\microsoft visual studio 10.0\vc\include\math.h(541): or       'float sqrt(float)'
1>          c:\program files\microsoft visual studio 10.0\vc\include\math.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前面的括号中添加float类型, >

If I add type float in brackets in front of s, like this:

#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;

int main() {
    int s = 25;
    cout << sqrt((float)s) << endl;
    return 0;
}



我已经猜到了, code>。另一个变体是,如果我写 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与float类型的sqrt相同?

What is the difference between them? Does it means that sqrtf is same as sqrt for the float type?

推荐答案

这里是 sqrt() sqrtf()的MSDN文档上的一个页面.microsoft.com / en-us / library / f1xa99e6.aspx code> ,这解释了区别:

Here's a page on MSDN documentation for sqrt() and sqrtf(), that explains the difference:


sqrt,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 的重载,这些重载使用float或long double类型。在C程序中, sqrt 总是
,并返回double。

C++ allows overloading, so users can call overloads of sqrt that take float or long double types. In a C program, sqrt always takes and returns double.

返回值

Return Value

sqrt 函数返回x的平方根。如果x
为负数,则 sqrt 会默认返回不确定。

The sqrt function returns the square-root of x. If x is negative, sqrt returns an indefinite, by default.

在C ++中 sqrt()接受 double float long double sqrtf()只接受 float

So the difference in C++ is that sqrt() accepts either a double, a float or a long double while sqrtf() accepts only a float.

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

As the documentation says, the only reason why there's two different versions is because C did not support overloading, so there had to be two functions. C++ allows overloading, so there are actually three different versions of sqrt() taking floating point arguments of various sizes.

所以在C ++中,你的代码片段基本上做同样的事情。然而,在C上,在中会出现从 float double sqrt()调用。

So in C++, both your code snippets do essentially the same thing. On C, however, there would've been a conversion from float to double in the sqrt() call.

这篇关于sqrt和sqrtf之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆