如何在 C++ 中获得立方根?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18103769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I obtain the cube root in C++?
提问by Hava Darabi
I know how to obtain the square rootof a number using the sqrt
function.
我知道如何使用该函数获取数字的平方根sqrt
。
How can I obtain the cube rootof a number?
如何获得一个数的立方根?
回答by Lightness Races in Orbit
sqrt
stands for "square root", and "square root" means raising to the power of 1/2
. There is no such thing as "square root with root 2", or "square root with root 3". For other roots, you change the first word; in your case, you are seeking how to perform cube rooting.
sqrt
代表“平方根”,“平方根”的意思是提高到 的幂1/2
。没有“根为 2 的平方根”或“根为 3 的平方根”这样的东西。对于其他词根,您更改第一个单词;在您的情况下,您正在寻找如何执行立方根。
Before C++11, there is no specific function for this, but you can go back to first principles:
在 C++11 之前,没有具体的函数,但你可以回到最初的原则:
- Square root:
std::pow(n, 1/2.)
(orstd::sqrt(n)
) - Cube root:
std::pow(n, 1/3.)
(orstd::cbrt(n)
since C++11) - Fourth root:
std::pow(n, 1/4.)
- etc.
- 平方根:(
std::pow(n, 1/2.)
或std::sqrt(n)
) - 立方根:(
std::pow(n, 1/3.)
或std::cbrt(n)
自 C++11 起) - 第四根:
std::pow(n, 1/4.)
- 等等。
If you're expecting to pass negative values for n
, avoid the std::pow
solution — it doesn't support negative inputs with fractional exponents, and this is why std::cbrt
was added:
如果您希望为 传递负值n
,请避免使用该std::pow
解决方案 -它不支持带有小数指数的负输入,这就是std::cbrt
添加的原因:
std::cout << std::pow(-8, 1/3.) << '\n'; // Output: -nan
std::cout << std::cbrt(-8) << '\n'; // Output: -2
N.B. That .
is really important, because otherwise 1/3
uses integer division and results in 0
.
注意这.
真的很重要,因为否则1/3
使用整数除法并导致0
.
回答by triclosan
回答by OrangeDog
include <cmath>
std::pow(n, 1./3.)
Also, in C++11 there is cbrt
in the same header.
此外,在 C++11 中有cbrt
相同的头文件。
傻瓜数学。
回答by Sebastian Redl
The nth root of x is equal to x^(1/n), so use std::pow
. But I don't see what this has to with operator overloading.
x 的第 n 个根等于 x^(1/n),因此使用std::pow
. 但我不明白这与运算符重载有什么关系。
回答by SpongeBobFan
sqrt()
is not an operator.- You cannot overload
sqrt()
.
sqrt()
不是运营商。- 你不能超载
sqrt()
。
Please explain why do you need to overload sqrt()
so we can help you to do what you want.
请解释为什么您需要超载,sqrt()
以便我们可以帮助您做您想做的事。
回答by Pranjal
I would discourage any of the above methods as they didn't work for me. I did pow(64, 1/3.) along with pow(64, 1./3.) but the answer I got was 3
Here's my logic.
我不鼓励上述任何方法,因为它们对我不起作用。我做了 pow(64, 1/3.) 和 pow(64, 1./3.) 但我得到的答案是 3
这是我的逻辑。
ans = pow(n, 1/3.);
if (pow(ans, 3) != n){
ans++;
}
回答by Sagar Kumar
Actually the round must go for the above solutions to work.
实际上,必须进行回合才能使上述解决方案起作用。
The Correct solution would be
正确的解决方案是
ans = round(pow(n, 1./3.));
ans = round(pow(n, 1./3.));
回答by Akash Vichare
The solution for this problem is
这个问题的解决方案是
cube_root = pow(n,(float)1/3);
and you should #include <math.h>
library file
你应该#include <math.h>
库文件
Older standards of C/C++ don't support cbrt() function.
较旧的 C/C++ 标准不支持 cbrt() 函数。
When we write code like cube_root = pow(n,1/3);
the compiler thinks 1/3 = 0
(division problem in C/C++), so you need to do typecasting using (float)1/3
in order to get the correct answer
当我们像cube_root = pow(n,1/3);
编译器所想的那样编写代码时1/3 = 0
(C/C++ 中的除法问题),因此您需要进行类型转换 using(float)1/3
才能得到正确答案
#include<iostream.h>
#include<conio.h>
#include<math.h>
using namespace std;
int main(){
float n = 64 , cube_root ;
clrscr();
cube_root = pow(n , (float)1/3);
cout<<"cube root = "<<cube_root<<endl;
getch();
return 0;
}
cube root = 4
立方根 = 4