You probably added the two longs together and then converted the result to a double. This will give you the incorrect value because the result of adding two longs is a long, thus you get overflow. According to C's type conversion rules, the return of an addition is the type of the longest floatiest operand. If you cast one of the operands to double then the result will be correct:
Code:
#include <stdio.h>
#include <limits.h>
int main ( void )
{
long a, b;
double result;
a = b = LONG_MAX;
/*
** This works.
*/
result = (double)a + b;
printf ( "%ld + %ld = %.0f\n", a, b, result );
return 0;
}
Code:
#include <stdio.h>
#include <limits.h>
int main ( void )
{
long a, b;
double result;
a = b = LONG_MAX;
/*
** This does not.
*/
result = (double)(a + b);
printf ( "%ld + %ld = %.0f\n", a, b, result );
return 0;
}
-Prelude