Rounding issue when using long long on PIC

一个人想着一个人 提交于 2019-12-02 04:56:15

问题


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


回答1:


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)



来源:https://stackoverflow.com/questions/22412584/rounding-issue-when-using-long-long-on-pic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!