0

When I run my code, for Y I am consistently getting the value -2147483648, regardless of what value y was fed into my equation.

Here is my code.

#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;
    }
}

After reviewing my code, I realized my problem was with my "int x = sqrt(value)". Aside from the problem with the "value" variable being an int, of course, a bogus value was still being returned due to the fact that the purpose of check is to evaluate whether or not (2*(pow(y, 4)))+1) returned a perfect whole square for any given value of y, and this was not possible due to variable x in check(double value) being datatype integer.

UPDATE: I rewrote my code as follows. I still don't get any correct returns

#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;
}

Read the comments are the top of my code, and the purpose for this selection should be pretty obvious.

h3xc0ntr0l
  • 399
  • 1
  • 3
  • 14
  • If you need a `pow()` function that works with integers, [use this](http://stackoverflow.com/a/101613/1679849). – r3mainer Jan 15 '15 at 15:20
  • `2*pow(1000, 4)` doesn't fit in an `int`; it has 41 binary digits. And you're using the wrong format specifiers. – molbdnilo Jan 15 '15 at 15:25

4 Answers4

2

pow() returns double and you are using integer i to store the return value.

Due to type promotion during expression evaluation the expression:

((2*(pow(y, 4)))+1)

will give a double value and you are storing this in integer type which will give unexpected results.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • It's not unexpected results, it's expected to eventually **overflow** the integer. – Iharob Al Asimi Jan 15 '15 at 15:16
  • @iharob; and that will lead to unexpected result. – haccks Jan 15 '15 at 15:17
  • 1
    This doesn't explain why the answer is always -2147483648. – John Kugelman Jan 15 '15 at 15:20
  • I have no Idea, why do you assume it was me, I was the first to upvote, but since you never explain anything I removed the upvote, but I didn't downvote. – Iharob Al Asimi Jan 15 '15 at 15:21
  • @iharob Sorry I went by the comment – Gopi Jan 15 '15 at 15:21
  • @JohnKugelman There is a issue if an answer makes sense to give some knowledge which is helpful then it is not a answer which is of no use and it doesn't make sense to down vote. This is just my opinion you are free to express your views – Gopi Jan 15 '15 at 15:24
  • Your answer implies that the solution is to change `int i` to `double i`, but that's not the problem. It's *a* problem, but it's not *the* problem. As a general suggestion, I'd encourage you to spell out exactly what you're saying. If you elaborate on what the "unexpected results" are exactly (i.e. truncation and/or overflow), it should be more clear that that's a side issue. – John Kugelman Jan 15 '15 at 15:26
  • @JohnKugelman But that also needs to be done which your answer doesn't mention – Gopi Jan 15 '15 at 15:27
2

You don't have a prototype for double pow(double, double); so the compiler implicitly assumes its signature is int pow(int, int);. Not good!

The solution is to #include the appropriate header at the top of your .c file.

#include <math.h>

Make sure you enable warnings, and if they're already enabled, pay attention to them! Your compiler should warn you about the missing prototype. (It should also spit out a similar warning for printf.)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I see this problem so often, I think people should learn how to invoke their compilers to prevent this silly mistakes. And it acutally never occurs to me that someone forgot to include any header because my programs wouldn't compile in that case. – Iharob Al Asimi Jan 15 '15 at 15:20
  • This is a good answer, but I didn't forget the header. I only included the code that is producing the bugs; there is a lot more that runs smoothly. I include math.h and have a prototype for it, and also link it using -lm in GCC. – h3xc0ntr0l Jan 15 '15 at 17:14
0

This is the declaration of pow:

double pow(double x, double y)

Which means it operates in double. By using int instead, variable y is overflowing.

FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46
0

In reference to your updated question, this line:

if(x = check(i) > 0)

needs to be parenthesized:

if((x = check(i)) > 0)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578