在PIC上使用long long时的舍入问题 [英] Rounding issue when using long long on PIC
问题描述
我正在PIC单片机上做一些简单的数学运算,用C语言运行代码,并使用MPLABX和xc16编译器.这是代码:
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;
打印出结果"后,我得到了5,471,873,794,048;而它应该给5,471,873,547,255.知道导致此问题的原因是什么,如何纠正该问题?
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?
谢谢
推荐答案
xc16默认将double和float都作为32位数据类型处理.您需要提供编译选项-fno-short-double
才能使用64位double.
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.
您也许还可以只将long double
用作数据类型,但是我目前无法进行编译以验证这一点.
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.
(作为测试,5,471,873,794,048也正是您使用float
而不是double
在x86上编译示例代码的结果)
(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
)
这篇关于在PIC上使用long long时的舍入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!