Question

long long int A = 3289168178315264;
long long int B = 1470960727228416;
double D = sqrt(5);

long long int out = A + B*D;

printf("%lld",out);

This gives result : -2147483648

I am not able to figure out why (it should be a positive result). Can somebody help?

Was it helpful?

Solution

My guess is that the compiler needs to round the result from "A+B*D" to an integer first, because you're storing the result inside an int field. So basically, you're having a datatype conflict.

Both A and B are still valid numbers for a long long int, which is 8 bytes long. You could even multiply them by 1.000 and still have valid long long int values. In some other languages it's also known as the int64.

A double, however, is also 64-bits but part of those are used as exponent. When you multiply a double with an int64, the result would be another double. Adding another int64 to it still keeps it a double. Then you're assigning the result to an int64 again without using a specific rounding method. It would not surprise me if the compiler would use a 4-bit rounding function for this. Am even amazed that the compiler doesn't puke and break on that statement!

Anyways, when using large numbers like these, you have to be extra careful when mixing different types.

OTHER TIPS

maybe you have to specify those constants as "long long" literals? e.g. 3289168178315264LL

What compiler/operating system are you using? I ran your code using Visual C++ 2008 Express Edition on Windows XP and IT WORKS - answer: 6578336356630528 (this is a 53-bit number, so it just fits inside a double).

I also tried two variations to see if the order of operations mattered:

long long int out = A; out+=B*D;

long long int out = B*D; out+=A;

These both work as well!

Curious.

Your answer (have to verify) calcuates successfully, however, it causes an overflow into the sign bit, which makes the answer negative. Solution : make all your variables unsigned.

Why:

Numbers are stored as series of bits in you computer's memory. The first bit in such a series, when set means that you number is negative. So the calculation works, but overflows into the sign bit.

Recommendation:

If you're working with numbers this big, I recommend you to get a multiprecision arithmetic library. 'T will save you a lot of time and trouble.

The parameter to sqrt should be double.

#include <math.h>
double sqrt( double num );

And also we should explict cast the result from B * D to long long.

    long long int A = 3289168178315264;
    long long int B = 1470960727228416;
    double D = sqrt(5.0);
    printf("%f\n",D);
    long long int out = A + (long long) (B * D);
    printf("%lld",out);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top