Microchip
直播中

彭瑾

7年用户 179经验值
私信 关注
[问答]

如何将IEEE754双字符串转换成十进制字符串

微芯片XC32包括几个数学函数,这些函数在IEEE754双精度(微芯片称它们为长双精度的)上运行,但是在标准工作室中不提供打印它们的支持。将双精度数字转换为十进制字符串表示的精确功能迄今为止还没有。在Microschip的XC32william hill官网 页面中,一些用户已经与社区共享了自己的功能,但没有一个满足精确打印的要求。问题的根源是要将它们转换成十进制字符串,需要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 functions 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

回帖(18)

徐磊

2019-7-23 12:05:27
这些格式说明符对单精度(32位)浮点数字工作良好。我知道,使用32位语言工具库(DS51685E)可以打印长双精度文件的惟一方法是将长双精度文件转换为浮点数,从而丢失几个重要数字。ND SCANF不支持64位长双类型的大小说明符。

以上来自于百度翻译


      以下为原文

    These format specifiers work fine for single precision (32-bit) floating-point numbers. The only way, I'm aware of, that you can print long doubles using the 32-Bit Language Tools Libraries (DS51685E) is by casting a long double to a float, thus loosing several significant digits.
 
The issue is documented in the release notes for XC32 v1.42:
XC32-307 :
The stdio functions such as printf and scanf do not support the size specifiers for the 64-bit long double type.
举报

李子跃

2019-7-23 12:24:35
因为他们已经被打破了很长一段时间?然而,OP的解决方案无疑是过度的。你不需要一个“千位”或一个任意的精确库来精确地转换64位浮点值以便显示。准确度,或显著数字。我想不出任何现实世界的情况,除了结果中的第十位数以外,任何东西都是需要的,甚至是有意义的。

以上来自于百度翻译


      以下为原文

   
Because they have been broken for a long time?
However, the OP's solution is definately overkill. You don't need a 'thousand bits' or an arbitrary precision library to accurately convert a 64bit floating point value for display.
Hell, if you think you need to display all 19 decimal significant figures from a 64bit float you are doing it wrong, or just don't understand the difference between precision and accuracy, or significant figures. I cant think of any real world situation where anything beyond the 10th digit in the result is required, or even meaningful.
举报

徐磊

2019-7-23 12:43:34
我建议你对如何正确地写浮点数字做一些研究。请让我们知道,如果你找到一个方法,不要求那么多位的精确度。我需要打印一个数值表完全的精确度,所以我可以比较我的结果与其他人的结果。十位数不行。我需要将它们和最后一个有效数字进行比较,因为一个错误的数字会完全放弃我的计算,因为错误会相继增加。我不能比较二进制表示,因为另一个函数只输出十进制。

以上来自于百度翻译


      以下为原文

    I suggest you do some research on how to write floating-point numbers accurately. Please let us know if you find a way that doesn't require that many bits of precision.
 
I need to print a table of values in full precision so I can compare my results with somebody else results. Ten digits won't do. I need to compare them to the last significant digit as one wrong digit will throw my computation completely off, since errors add up in succession. I can't compare the binary representations because the other function only outputs decimal.
 
举报

陈晨

2019-7-23 13:00:18
你看了吗?打破曼斯塔和指数,然后像任何科学符号编号一样对待它。如果你想要的话,你甚至可以放置十进制。

以上来自于百度翻译


      以下为原文

    Have you looked at  IEEE754?
break out the manista and exponent and sign.
Then treat it like any scientific notation number.  
You could even place the decimal if you want.
举报

更多回帖

发帖
×
20
完善资料,
赚取积分