當我運行我的代碼時,對於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