微芯片XC32包括几个数学函数,这些函数在IEEE754双精度(微芯片称它们为长双精度的)上运行,但是在标准工作室中不提供打印它们的支持。将双精度数字转换为十进制字符串表示的精确功能迄今为止还没有。在Microschip的XC32
论坛页面中,一些用户已经与社区共享了自己的功能,但没有一个满足精确打印的要求。问题的根源是要将它们转换成十进制字符串,需要64位以上的精度。(实际上,对于一些数字,数字接近一千位)。通过使用Tom St Denis编写的一个免费提供的多精度整数库,我编写了对该库的扩展,以将64位浮点数转换成十进制字符串。该函数使用查找表(快速但冗长)将浮点数转换为任意精度的整数,并使用任意精度的整数算法计算基数10中的尾数和指数的值。从他的网站上构建整数库(LibTomMath),并在MPLABX中添加我在这里提供的两个源文件到一个库项目中。此外,tommath.h应该被修改为包括以下函数原型:/*->IEEE754 double(Micro.long double)到字符串转换<-*/chAr*DBL2STR(long double f,char *STR);这就是构建库所需的全部内容。我建议在项目属性下打开XC32-gcc中的优化,因为dbl2str使用的查找表非常大。g doubles。注意,我还没有添加对将字符串转换为长doubles的支持,也没有添加格式说明符——除了零、无穷大和指数等于零的数字之外,所有这些都以科学的表示法打印,其中指数大于基数10。大,所以它可能不适合所有的PIC32。请欣赏并删除你的评论!伯尼
C(240.84 KB)下载12次
以上来自于百度翻译
以下为原文
Microchip XC32 includes several math func
tions that operate on IEEE754 double precision (Microchip calls them long doubles), but does not provide support in stdio for printing them.
With the introduction of PIC32 microcontrollers with larger flash memories and faster clocks there's no reason that an accurate function to convert double precision numbers to a decimal string representation hasn't been available to date.
In Microchip's XC32 forum pages some users have shared their own functions with the community, but none satisfies the requirement to print them accurately. The root of the problem is that to convert them to a decimal string more than 64 bits of precision are required. (Actually the number is close to a thousand bits for some numbers).
I present here my solution to the problem. By using a freely available multi-precision integer library written by Tom St Denis I wrote an extension to the same to convert 64-bit floating-point numbers to a decimal string.
The input of the
dbl2str function is a Microchip long double and a pointer to an allocated
string of 26 or more characters. The function converts the floating-point number to an arbitrary precision integer using look-up tables (fast, but lengthy) and using arbitrary precision integer arithmetic computes the value of the mantissa and exponent in base 10.
To use the function one must download Tom's free multi-precision integer library (LibTomMath) from his website and build it in MPLABX adding the two source files that I provide here to a library project.
Also tommath.h should be modified to include the following function prototype:
/* ---> IEEE754 double (Microchip long double) to string conversion <--- */
char* dbl2str(long double f, char* str);
That's all that's needed to build the library. I recommend to turn on optimizations in XC32-gcc under project properties, as the look-up tables that dbl2str uses are quite large.
To use the function all you need to do is to add the library and tommath.h and set a
heap of 4096 bytes in your project and you should now have support for printing long doubles.
Note that I haven't added support for converting strings to long doubles yet, nor I have added format specifiers – all is printed in scientific notation with an exponent over base 10, except for zero, infinity and numbers with exponents equal to zero.
Also, note that the library is quite large, so it might not fit in all PIC32s.
Please enjoy and drop your comments!
~Bernie
Attachment(s)
dbl2str.c (9.04 KB) - downloaded 17 times
dbl2str_tables.c (240.84 KB) - downloaded 12 times
0