本文介绍了STRTOF的精确问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的小组


我在使用STRTOF将文本字符串转换为
浮点数时遇到精度问题。例如,有时将文本串736.00000

终止字符串。有谁知道为什么会发生这种情况以及

专门用来避免这种情况。这对我正在写的申请表非常关键。

谢谢


Marcus D. Jacobs

Dear Group

I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and how
specifically to avoid this. This is quite critical to an applicaiton
that I am writing.
Thanks

Marcus D. Jacobs

推荐答案




您应该阅读常见问题解答。

-

有些编程实践会产生错误;

这个就像拨打一个800号码

并且错误传递给你的门。 br />
--Steve McConnell



You should read the FAQ.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell





您是否尝试过编写这样的小测试程序(未经测试)?


#include< stdio.h>

main(){

float x = 736.0;

printf("%f\ n",(double)x);

}


你可能得到类似的结果。 Float是低精度的

类型。 Double是高精度型。使用双倍。


-

Tom Zych

这个电子邮件地址将在某个时候到期以阻止垃圾邮件发送者。

永久地址:echo''g******@cbobk.pbz''| rot13



Did you try writing a little test program like this (not tested)?

#include <stdio.h>
main() {
float x = 736.0;
printf("%f\n", (double) x);
}

You''ll probably get similar results. Float is the low-precision
type. Double is the high-precision type. Use double.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo ''g******@cbobk.pbz'' | rot13





请注意,C区分大小写。函数的名称是''strtof'',而不是''STRTOF''。

strtof()返回一个浮点数。一个浮点数不需要超过6位数的

精度。您报告的值736.00001除了缩放(尾数)之外,还有大约27位信息

。你怎么知道这个值有那些精确的8位十进制数字?如果你只限于打印

只有数据中的信息,你会做得更好。


#include< stdio.h>

#include< math.h>

void showfloat(x)

{

printf("%。* g",FLT_DIG,x);

}


-

Martin Ambuhl



Note that C is case-sensitive. The function''s name is ''strtof'', not ''STRTOF''.
strtof() returns a float. A float need not have more than 6 digits of
precision. The value you report "736.00001" has some 27 bits of information
apart from scaling ("mantissa"). How do you know that the value has those
8 decimal digits of precision? If you would restrict yourself to printing
only the information in the data, you would do better.

#include <stdio.h>
#include <math.h>
void showfloat(x)
{
printf("%.*g", FLT_DIG, x);
}


--
Martin Ambuhl


这篇关于STRTOF的精确问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 02:51