我需要有关如何获得某个数字的第n个根的帮助.
用户输入他想要root的数字n和数字.我需要在没有cmath lib和分而治之的方法下解决这个问题.
这是我的代码还没有工作:
#include<iostream>
using namespace std;
float pow(float a,float c){
if (a == 0)
return 0;
else if(a == 1)
return 1;
else{
float p = pow(a,(c/2));
if(c%2)
return p*p*a;
else
return p*p;
}
}
int main(){
float a,b;
float c;
cout << "Enter positive number:(base)" << endl;
do{
cin >> a;
}while (a < 0);
cout << "Enter number: (root)" << endl;
cin >> b;
c = 1/b;
cout << "Result:"<<pow(a,c) << endl;
system("pause");
return 0;
} …Run Code Online (Sandbox Code Playgroud) 这应该是不言自明的
>>> (1+2j).real #Normal Usage
1.0
>>> (1+2j).imag #Normal Usage
2.0
>>> 1+2j.real #Normal Usage
1.0
>>> 1+2j.imag #Flips Out
3.0
>>> 123+657j.real
123.0
>>> 123+657j.imag
780.0
Run Code Online (Sandbox Code Playgroud)
z.imag以某种方式累加了实部和虚部.
我发现这很有趣..这是一个错误还是这是一个故意的功能?
我认为答案应该是否定的,但我今天编写了一些代码,完美编译并使用pow和sqrt显示正确的答案,即使一开始我忘了添加
#include<cmath>
我在这里缺少什么?
int n;
cin>>n;
unsigned long long int a,s;
cin>>a;
s=(2*pow(10,n)+a);
Run Code Online (Sandbox Code Playgroud)
但是,当我给出大的n类似17或18然后我的输出并s没有按预期进行。请参阅输出图像,例如:when n=17,a=67576676767676788那么s=267576676767676800理想情况下应该是 2*10^17 + 67576676767676788
我正在尝试学习 C++,我正在使用 MS Visual Studio 2019。我有以下代码:
#include <iostream>
int main()
{
std::cout << pow(10, 2);
}
Run Code Online (Sandbox Code Playgroud)
如何在不包含的情况下编译和运行而不会出错cmath?在解决方案中,只有一个包含上述代码的文件。
编辑测试用例和编译器不一致。最近我做了一个更改,使用更现代的 < cmath > 而不是 < math.h >。虽然编译得很好,没有任何警告,但对一个文件的这一行更改导致我的测试用例失败。根据我的理解,cmath 只是将大部分函数/变量放在 std 命名空间中,并且我没有对该命名空间进行任何更改。
我会注意到我正在使用
开方
abs (因为我在这里使用双精度,所以我假设编译器会自动使用 cmath/math.h 形式,而不是 stdlib)
晒黑
排除可能受此单行更改影响的任何文件。我确实有一个已经使用 cmath 的不同文件,但不影响测试。
一个测试用例
#include <iostream>
#include <cmath>
int main() {
double x = -0.546;
double y = abs( x / sqrt(x*x + 1.0));
double z = std::abs( x / sqrt(x*x + 1.0));
std::cout << "Variable value is " << x << std::endl;
std::cout << "Value of function using cmath " << z << std::endl;
std::cout << "Value of function using …Run Code Online (Sandbox Code Playgroud) 下面的代码片段有什么问题,VS2010无法编译它?
int m = sqrt( n );
Run Code Online (Sandbox Code Playgroud)
(我试图确定整数是否为素数......)
我正在使用OpenGL在c ++中进行一个项目,并且继续受到来自cmath.h的数百个语法错误的攻击.主要难题是我甚至根本没有提到或包括cmath.我在一两件事情中使用math.h,但在我做最近的更改之前,代码工作正常.回到以前工作的代码现在显示相同的错误,所以我有点难以从哪里开始寻找.
我错过了一些非常明显的东西,或者这可能是Visual Studio 10的问题吗?
cmath exp()和log()函数总是对称的吗?
如果我这样做的话
double x;
double y = exp(log(x));
assert(x == y);
Run Code Online (Sandbox Code Playgroud)
断言是否会失败,在这种情况下:在什么情况下?我们可以假设这x是一个有理数> 0.
我知道这是一个非常古老的话题,但我不明白为什么它在这里表现得如此。
该程序:
#include <vector>
#include <algorithm>
#include <cmath>
int main()
{
std::vector<double> a = {1,4,9,16};
std::transform(a.begin(), a.end(), a.begin(), sqrt); // WORKS
std::transform(a.begin(), a.end(), a.begin(), std::sqrt); // compilation error
}
Run Code Online (Sandbox Code Playgroud)
编译错误是
g++ -std=c++17 -O3 main.cpp -Wall -Wextra -pedantic
main.cpp: In function ‘int main()’:
main.cpp:10:57: error: no matching function for call to ‘transform(std::vector<double>::iterator, std::vector<double>::iterator, std::vector<double>::iterator, <unresolved overloaded function type>)’
std::transform(a.begin(), a.end(), a.begin(), std::sqrt); // compilation error
^
........
Run Code Online (Sandbox Code Playgroud)
所以,万一
sqrt 编译器可以推导出参数/返回类型并使用此函数。std::sqrt编译器无法在std::sqrt?.. 的不同重载之间进行选择。为什么会这样?编译时所有类型都是已知的,它到底不能推断出什么?为什么只是简单的sqrt作品(我找不到定义)?
以下可能表明正在发生某些事情:
g++ -std=c++17 …Run Code Online (Sandbox Code Playgroud)