当我运行我的代码时,对于Y,我总是得到值-2147483648,而不管我的公式中输入了什么值。宏观方程给假价值?
这是我的代码。
#define MAX 1000
#define EQ(y) ((2*(pow(y, 4)))+1)
int check(int value);
int main()
{
int i, y, x;
for(y = 1; y < MAX; y++)
{
i = EQ(y);
if(check(i))
printf("combination found: x = %d, y = %d", sqrt(i), y);
}
}
int check(int value)
{
int x = sqrt(value);
if ((x*x) == value)
return 1;
else
{
return 0;
}
}
审查我的代码后,我意识到我的问题是我的 “INT X =开方(值)”。除了“值”变量是一个整数的问题,当然由于检查的目的是评估是否(2 *(pow(y,4))),所以仍然返回一个假的值)+1)为任何给定的y值返回了一个完美的整数正方形,并且由于变量x(checkval(double value))是数据类型integer,所以这是不可能的。
更新:我重写了我的代码,如下所示。我还没有得到任何正确的报税表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*
* the solution I implemented basically involved dropping x from the equation, solving for y, checking to see if it has a
* perfect square root. if it does, then x = the squareroot of y in the function EQ.
* original problem: for equation x^2 - 2y^4 + 1 = 0, find all possible solutions up to arbitrary maximum
*/
#define MAX 100000
#define EQ(g) (((pow(g, 4.0)))+1)
int check(double value);
int main()
{
int y, x;
double i;
for(y = 1; y < MAX; y++)
{
i = EQ(y);
if(x = check(i) > 0)
printf("combination found: x = %d, y = %d\n", y, x);
}
}
int check(double value)
{
double x = sqrt(value);
int n = (int) x;
printf("%d\n%f\n%f\n", n*n, value, x);
if (n*n == value)
return n*n;
else
return 0;
}
阅读评论是我的代码的顶部,并选择用的目的应该是很明显的。
如果你需要一个'POW()'函数与整数作品,[使用](http://stackoverflow.com/a/101613/1679849)。 – 2015-01-15 15:20:51
'2 * pow(1000,4)'不适合'int';它有41个二进制数字。而你正在使用错误的格式说明符。 – molbdnilo 2015-01-15 15:25:43