在C ++中找到多维数据集根? [英] finding cube root in C++?
问题描述
当我尝试找到一个数字的立方根时,奇怪的事情发生。
Strange things happen when i try to find the cube root of a number.
下面的代码返回未定义。在cmd:-1。#IND
The following code returns me undefined. In cmd : -1.#IND
cout<<pow(( double )(20.0*(-3.2) + 30.0),( double )1/3)
在cmd:4.93242414866094
While this one works perfectly fine. In cmd : 4.93242414866094
cout<<pow(( double )(20.0*4.5 + 30.0),( double )1/3)
从数学方式来看,它必须工作,因为我们可以从一个负数获得立方根。
Pow来自Visual C ++ 2010 math.h library。任何想法?
From mathematical way it must work since we can have the cube root from a negative number. Pow is from Visual C++ 2010 math.h library. Any ideas?
推荐答案
pow(x,y)
c $ c>< cmath> 如果x为负且y不为整数,则不起作用。
pow(x, y)
from <cmath>
does NOT work if x is negative and y is non-integral.
code> std :: pow ,如C标准和 cppreference :
This is a limitation of std::pow
, as documented in the C standard and on cppreference:
错误处理
Error handling
- 错误是按照math_errhandling中指定的报告的
- 如果base是有限和负数,exp是有限和非整数,
- 如果base为零,exp为零,则可能会发生域错误。
- 如果base为零并且exp为负,则可能会发生域错误或极点错误。
- Errors are reported as specified in math_errhandling
- If base is finite and negative and exp is finite and non-integer, a domain error occurs and a range error may occur.
- If base is zero and exp is zero, a domain error may occur.
- If base is zero and exp is negative, a domain error or a pole error may occur.
解决这个限制的方法:
-
Cube-rooting与使用1/3的力量是一样的,所以你可以做
std :: pow(x,1/3。)
。
您可以使用 std :: cbrt
。 C ++ 11引入了平方根和立方根函数,但没有通用的第n个根函数来克服 std :: pow
的限制。
这篇关于在C ++中找到多维数据集根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!