Question

I'm doing a simple bit of maths on a PIC microcontroller, running code in C and using MPLABX and the xc16 compiler. This is the code:

double mydouble = 0.019440;
long long int mypower = 281474976710656;

long long int result = mypower*mydouble;

Printing out 'result' gives me 5,471,873,794,048; while it should give 5,471,873,547,255. Any idea what is causing this problem, and how I can rectify it?

Thanks

Was it helpful?

Solution

xc16 handles both double and float as 32-bit data types by default. You need to give the compilation option -fno-short-double to use 64-bit doubles.

You may also be able to just use long double as a data type, but I can't compile at the moment to verify that.

(As a test, 5,471,873,794,048 is also exactly the result you get compiling your sample code on x86 using float instead of double)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top