完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
微芯片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 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个回答
|
|
|
这些格式说明符对单精度(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. |
|
|
|
|
|
因为他们已经被打破了很长一段时间?然而,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. |
|
|
|
|
|
我建议你对如何正确地写浮点数字做一些研究。请让我们知道,如果你找到一个方法,不要求那么多位的精确度。我需要打印一个数值表完全的精确度,所以我可以比较我的结果与其他人的结果。十位数不行。我需要将它们和最后一个有效数字进行比较,因为一个错误的数字会完全放弃我的计算,因为错误会相继增加。我不能比较二进制表示,因为另一个函数只输出十进制。
以上来自于百度翻译 以下为原文 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. |
|
|
|
|
|
你看了吗?打破曼斯塔和指数,然后像任何科学符号编号一样对待它。如果你想要的话,你甚至可以放置十进制。
以上来自于百度翻译 以下为原文 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. |
|
|
|
|
|
除了IEEE74是二进制的,我们需要十进制表示。我们需要多重精度。
以上来自于百度翻译 以下为原文 Except that IEEE754 is in binary and we want a decimal representation. We need multiple precision for that. |
|
|
|
|
|
正如NK所建议的那样,把它分解出来,并使用像Excel之类的PC程序来计算十进制表示。
以上来自于百度翻译 以下为原文 Break it out as NK has suggested and use a PC program like Excel or whatnot to compute the decimal representation. |
|
|
|
|
|
谢谢,但是我对我现在的功能很满意。
以上来自于百度翻译 以下为原文 Thanks, but I'm quite satisfied with the function I have now. |
|
|
|
|
|
您的要求非常特殊,并且用于将IEEE双精度表示转换为十进制表示((s)printf、ecvt等)的标准c函数都不能满足您的需要,因为它们都使用重复的除法/乘以10,因此在最后几个数字中引入舍入错误。因此,您的实现,虽然是wellcome,但不能替代这些库函数的基于Microchips的实现。另一个确切的转换函数是http://www.ampl.com/netlib/fp/dtoa.c,可能需要在移植方面做一些工作;-)对于您的应用程序类型,您很可能是b。用二进制或IEEE十六进制符号从PIC检索结果,然后转换为PIC外部的十进制以供比较。
以上来自于百度翻译 以下为原文 Your requirement is extremely specialised, and none of the standard c functions for converting IEEE double to decimal representation ((s)printf, ecvt etc.) would satisfy your need, as they all use repeated divide/multiply by 10, and as such introduce rounding errors in the last few digits. As such, your implementation, whilst wellcome, is not a replacement for Microchips borked implementation of these library functions. An alternative exact conversion function is at http://www.ampl.com/netlib/fp/dtoa.c, may need some work in porting ;-) For your type of application, you might well be better off retrieving the results from the PIC in binary, or IEEE hex notation, then converting to decimal external to the PIC for comparison. |
|
|
|
|
|
@bsgamaIEEE 745在十进制中加倍,其最大值为16位数加上4位数的幂-10(包括符号)。这比你上面提到的近千比特少。除非你想把它打印成000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
以上来自于百度翻译 以下为原文 @bsgama IEEE 745 double in decimal ends up with a max. of 16 digits plus 4 digits power-of-ten (including the sign). 20 digits - resulting in 160 Bits when coded in ASCII. This is way less than the near-to-thousand bits you stated above. Unless you want to have it printed as 0.00000000000000000000000000000000000000000000something In this case: good luck with comparing. (OK - I forgot; nowadays there are tools that are very good in comparing scrap.) |
|
|
|
|
|
所有东西都是CPU.http://steve.hollasch.net/cgindex/coding/ieeefloat.htmlhttp://www.oxfordmathcenter.com/drupal7/node/43的二进制。所以低52位就是分数。如果将其分配给Long则将得到数字。
以上来自于百度翻译 以下为原文 Everything is binary to a CPU. http://steve.hollasch.net/cgindex/coding/ieeefloat.html http://www.oxfordmathcenter.com/drupal7/node/43 So the Low 52 bit is the fraction. if you assign it to a Long Long you will get your digits. |
|
|
|
|
|
西蒙,我必须仔细检查你给我的实施方式,但乍一看,它看起来比我复杂得多。也许我可以优化我的内存,减少内存占用,以适应更小的Pic,但是正如我在上次回复中说过的,我很满意我的实现。它确实为我修复了破损的实现,我希望它对其他人也有用。谢谢你花时间查找,伯尼。
以上来自于百度翻译 以下为原文 Simon, I have to check the implementation you sent me carefully, but at first glance it looks a lot more complex than mine. Maybe I can optimize mine and reduce the footprint to fit smaller Pics, but as I said before in my last reply I'm happy with my implementation. It does fix the broken implementation for me, and I hope it can be useful to others too. Thanks for taking your time to look it up, Bernie |
|
|
|
|
|
我想知道有没有人看到或有机会测试我昨天提交的源代码。我刚才检查了我的帖子,文件已经消失了,没有任何痕迹或解释。
以上来自于百度翻译 以下为原文 I was wondering if anyone saw or had a chance to test the source code I submitted yesterday. I just checked my post now and the files are gone without any trace or explanation. |
|
|
|
|
|
不要介意。今天早上我又检查过了,他们回来了。
以上来自于百度翻译 以下为原文 Never mind. I checked it again this morning and they're back. |
|
|
|
|
|
实际上,要精确地表示所有可能的IEEE754双精度值,需要17个十进制数字(+4表示指数)。(来源:http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)。整数表示首先,由于舍入误差除以10。查看文献,每个人都使用某种形式的BigIt表示,用于范围之外的值。+/- 2 ^ 30。在我的任何搜索中,算法需要的BigInt位的最小数目是~830。我也感到惊讶:-)但是,我仍然认为,除了极少数的专业用途之外(如OP),10位数字对于任何人来说都足够,并且传统的重复除法/乘法是1。0已经足够好了。
以上来自于百度翻译 以下为原文 It actually takes 17 decimal digits (+4 for exponent) to exactly represent all possible IEEE754 double precision values. (source:http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). To do the conversion *exactly* requires either an extended precision floating point format, or conversion to integer representation first, due to the rounding errors when dividing by 10. Looking at the literature, everyone uses some form of BigInt representation for values outside the range approx. +/- 2^30. The smallest number of BigInt bits an algorithem needs in any of my searches was ~830. I was surprised as well :-) However, I am still of the opinion that outside of a very small number of specialist uses (like to OP's), 10 digits is enough for anyone and the traditional repeated divide/multiply by 10 is good enough. |
|
|
|
|
|
所有的东西都是二进制的,CPU.http://steve.hollasch.net/cgindex/coding/ieeefloat.htmlhttp://www.oxfordmathcenter.com/drupal7/node/43所以低52位就是分数。这不能直接转换成十进制,例如,看下面的尾数值3,1e0、1e 1和1e2中1的完全不同的表示。
以上来自于百度翻译 以下为原文 Everything is binary to a CPU. http://steve.hollasch.net/cgindex/coding/ieeefloat.html http://www.oxfordmathcenter.com/drupal7/node/43 So the Low 52 bit is the fraction. if you assign it to a Long Long you will get your digits. No. You are forgetting that the low bits are the *binary* fraction. This cannot be converted directly to decimal. e.g. look at the following mantissa values decimal mantissa (first 12 bits) 1 .000000000000... 10 .010000000000... 100 .100100000000... 3 Totally different representations for the 1 in 1e0,1e1 and 1e2. |
|
|
|
|
|
你真的需要研究一下浮点数到底代表什么。当你的“完全精确”还不够时,你会怎么做?如何知道另一个实现在将浮点转换为ASCII字符串的方式上是正确的?
以上来自于百度翻译 以下为原文 You really need to do some research into what floating point numbers actually represent. What are you going to do when your "full precision" still isn't enough? How do you know the other implementation is correct in the way it converts floats to ascii strings? |
|
|
|
|
|
它有很长的时间,因为我有Delt将十六进制格式转换为文本。它应该是标准符号。数字在那里。这是留给学生的一项练习。
以上来自于百度翻译 以下为原文 It has a long time since I have delt with converting the hex format to text. It should be in standard notation. The digits are there. It is left as an exercise for the student to dig them out. |
|
|
|
|
|
Update:-fixed LibTomMaTh超链接将字符串大小要求从无符号int(32位)数组更改为26个字符或更多,将p10_idx[]和p5_idx[]更改为无符号.(16_位)
C(240.84 KB)下载11次 以上来自于百度翻译 以下为原文 Update: - fixed LibTomMaTh hyperlink - changed string size requirement to 26 characters or more - changed p10_idx[] and p5_idx[] from arrays of unsigned int (32-bits) to unsigned short (16_bit) Attachment(s) dbl2str.c (9.29 KB) - downloaded 14 times dbl2str_tables.c (240.84 KB) - downloaded 11 times |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
495 浏览 0 评论
5808 浏览 9 评论
2347 浏览 8 评论
2237 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3542 浏览 3 评论
1152浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1118浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
886浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
499浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-11 20:09 , Processed in 1.229195 second(s), Total 76, Slave 69 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
3599