完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
嗨,我在我的应用程序中使用PIC32 MX250F256H控制器。我面临着浮点运算的一些问题。浮点F=0in主体(){f=(浮点)(47.171143947.0);}我应该得到f=0.1711439,而我得到f= 0.1711425i,希望在十进制之后达到第六位的精度。我怎么做?
以上来自于百度翻译 以下为原文 Hi, I am using PIC32MX250F256H controller in my application.I am facing some issues with floating point operations. float f=0 int main() { f=(float)(47.1711439-47.0); } I should get f=0.1711439 instead I m getting f=0.1711425 I want accuracy upto 6th place after decimal.How do i do it? |
|
相关推荐
19个回答
|
|
|
你需要刷新一下浮点数字是如何工作的。尝试使用“double”来代替浮点数。
以上来自于百度翻译 以下为原文 You need to refresh your memory about how floating point numbers work. Try using "double" instead of float. |
|
|
|
|
|
XC32编译器指南包含了一个相当好的解释浮点,双,长双的差异。参见浮点数据类型章。[编辑:和照顾- FNO短双编译器optHTP://www. MyCHIP.COM/FUMMS/M68 39 40.ASPX ]
以上来自于百度翻译 以下为原文 The XC32 compiler guide contains a rather good explanation of the differences for float, double, long double. see FLOATING-POINT DATA TYPES chapter. [edit: and take care of -fno-short-double compiler option http://www.microchip.com/forums/m683940.aspx ] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
我使用了双偶数长双加法:最接近47.1711439的单精度表示为47.171142578125,而47具有精确浮点表示。根据XC32用户指南,浮点和双类型都是32位,长双则是64位。长双倍应该给出你想要的精度。
以上来自于百度翻译 以下为原文 I used Double even long double also Hint: the closest single-precision representation for 47.1711439 is 47.171142578125, and 47.0 has exact floating-point representation. According to the XC32 User's Guide, both float and double types are 32 bits while long double is 64 bits. Long double should give the accuracy you desired. |
|
|
|
|
|
如何刷新内存?你需要刷新你的记忆力。浮动不能像计算器那样工作。
以上来自于百度翻译 以下为原文 How do i refresh memory? You need to refresh YOUR memory. floats do not work like a calculator. |
|
|
|
|
|
你是怎么用的?我猜想你犯了一个常见的错误,认为目标变量的类型是计数的。它不是用最大类型的源变量来完成的,只有在所有的计算完成之后,才是复制到目的地的值。这是C的一个基本特征。做算术。
以上来自于百度翻译 以下为原文 HOW did you use it? I suspect you made the common mistake of thinking that the type of the destination variable counts. It does not. The calculation is done using the largest type of the SOURCE variables, and only after all calculations are complete is the value copied to the destination. This is a basic feature of how C does arithmetic. |
|
|
|
|
|
你是怎么用的?我猜想你犯了一个常见的错误,认为目标变量的类型是计数的。它不是用最大类型的源变量来完成的,只有在所有的计算完成之后,才是复制到目的地的值。这是C的一个基本特征。long double double f= 0in主体(){f=(long double)(47.171143947.0);}
以上来自于百度翻译 以下为原文 HOW did you use it? I suspect you made the common mistake of thinking that the type of the destination variable counts. It does not. The calculation is done using the largest type of the SOURCE variables, and only after all calculations are complete is the value copied to the destination. This is a basic feature of how C does arithmetic. long double f=0 int main() { f=(long double)(47.1711439-47.0); } |
|
|
|
|
|
太晚了。47.1711433-47.0的计算是用浮点运算完成的,只有在你已经丢失了额外的数字之后,才把结果转换成“long double”。为了工作,你必须将至少一个操作数投射到更大的大小。
以上来自于百度翻译 以下为原文 Too late. The calculation of 47.1711439-47.0 is done using float arithmetic, and only then do you cast the result to "long double", after you have already lost the extra digits. For this to work, you have to cast at least one of the operands to the larger size. |
|
|
|
|
|
你的意思是说f=(长双)(47.1711439)- 47;
以上来自于百度翻译 以下为原文 You meant to say f=(long double)(47.1711439)-47.0; |
|
|
|
|
|
|
|
|
|
|
|
我必须把它转换成String。我有“String to String”函数。但是在这种情况下,我需要使用我没有的“long double to string”函数。
以上来自于百度翻译 以下为原文 I have to convert it to string.I have "float to string"function.But in this case I need to use "long double to string" function that i don't have |
|
|
|
|
|
|
|
|
|
|
|
如果我们通过双/长双,为什么Spavtf给出错误的字符串,但是如果我们通过浮点,它会给出正确的值。
以上来自于百度翻译 以下为原文 why sprintf gives wrong string if we pass double/long double.However it gives correct value if we pass float |
|
|
|
|
|
为什么你不断提出问题而不提供相关信息?在这种情况下,您没有显示您如何调用SeaStFF函数。我们看不到您的屏幕,您必须告诉我们。我怀疑您使用了长格式的错误格式字符串。
以上来自于百度翻译 以下为原文 Why do you continually ask questions without supplying relevant information? In this case, you have not shown HOW you called the sprintf function. We can't see your screen, you have to tell us. I suspect you have used the wrong format string for a long double., |
|
|
|
|
|
SPLATF(STR),“%F”,(double)47.11378963;---这工作到第五个十进制Posies PrimTf(STR,“%LF”,(long double)47.11378963);---因为我想使用long double。我尝试使用但是不能得到预期的结果。
以上来自于百度翻译 以下为原文 sprintf(str,"%f",( double) 47.11378963);---This works upto 5th decimal place sprintf(str,"%lf",( long double) 47.11378963);---As i want to use long double.I tried using but couldnt get expected result. |
|
|
|
|
|
|
|
|
|
|
|
+1你得到了什么结果?你期待什么结果?对于XC32中的浮点常数,例如:47.11378963是双47.11378963F或47.11378963F是浮点47.11378963L或47.11378963L是Primff()使用的长双格式格式说明符:%LF将该值转换为双%LF,将该值转换为长双反常,您应该真正读取XC3。2用户指南和32位语言工具库手册!
以上来自于百度翻译 以下为原文 +1 What result did you get? What result did you expect? For floating-point constants in XC32, e.g.:
For format specifier used by printf():
Anyway, you should really read the XC32 User's Guide and the 32-Bit Language Tools Libraries Manual! |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
473 浏览 0 评论
5793 浏览 9 评论
2334 浏览 8 评论
2224 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3530 浏览 3 评论
1124浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1098浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
873浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
475浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-2 10:20 , Processed in 1.111531 second(s), Total 113, Slave 95 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
11622