为什么不工作? [英] Why doesn't atof work?

查看:71
本文介绍了为什么不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:


我很难得到atof()以准确转换3.1

任何想法?


谢谢,


画了


#include< iostream>

#include< math.h>


使用命名空间std;


int main(int argc,char * argv [])

{


float fNum;

char cNumBuffer [] =" 3.1";

fNum =(float)atof(cNumBuffer);

//为什么这个存储在fNum中的3.099999905

返回0;

}

Hi:

I''m having trouble getting atof() to accurately convert "3.1"
Any ideas?

Thank you,

Drew

#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char* argv[])
{

float fNum;
char cNumBuffer[]="3.1";
fNum =(float)atof(cNumBuffer);
//Why does this store 3.099999905 in fNum

return 0;
}

推荐答案



" Drew" < PE ***** @ bellsouth.net>在消息新闻中写道:JS ******************* @ bignews5.bellsouth.net。 ..

"Drew" <pe*****@bellsouth.net> wrote in message news:JS*******************@bignews5.bellsouth.net. ..
//为什么这个存储在fNum中的3.099999905
//Why does this store 3.099999905 in fNum



因为这是最接近的可表示数字(3.1几乎肯定不完全是
$将b $ b表示为二进制后,将其转换回十进制进行打印。


Because that is the closest representable number (3.1 is almost surely not exactly
representable in binary) after it''s been converted back to decimal for printing.


Drew写道:
嗨:

我无法通过atof()来准确转换3.1
任何想法?

谢谢,

Drew


#include< iostream>
#include< math.h>

使用命名空间std;

int main(int argc,char * argv [])
{/>
浮点数fNum;
char cNumBuffer [] =" 3.1" ;;
fNum = (float)atof(cNumBuffer);
//为什么这个存储在fNum中的3.099999905

返回0;
}
Hi:

I''m having trouble getting atof() to accurately convert "3.1"
Any ideas?

Thank you,

Drew

#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char* argv[])
{

float fNum;
char cNumBuffer[]="3.1";
fNum =(float)atof(cNumBuffer);
//Why does this store 3.099999905 in fNum

return 0;
}



罗恩说的话。此外,3.1可证明是无法代表的,因为.1

= 10 ^ -1,而且10有一个素数,因此

是2的整数幂有限序列的传统和。不是两个因素。


GCC维护人员将此列为最常报告的非bug。

http://gcc.gnu.org/bugs.html#nonbugs


您可以使用

sprintf或更安全的替代品获得指定数量的重要或可靠数字。如果您需要绝对准确地表示

有理数,请获得支持

常用分数的库。如果你需要完美地表示非理性,那么你可以通过搜索连续分数来开始
。这些将有助于

一些非超验证。


就个人而言,我尽可能避免使用浮点数。 Von

Neumann还认为任何值得他们的盐的程序员都应该能够跟踪小数点。如果你必须像使用整数一样使用浮点数

数字(例如,你想要比较它们的b
$),至少要小心圆它们首先是实际的有效位数。


What Ron said. Furthermore, 3.1 is provably unrepresentable as the
traditional sum of a finite sequence of integer powers of two, since .1
= 10^-1, and 10 has a prime factor other than two.

The GCC maintainers list this as their most often reported non-bug.

http://gcc.gnu.org/bugs.html#nonbugs

You can get a specified number of significant or reliable digits using
sprintf, or a more type-safe alternative. If you need to represent
rational numbers with absolute accuracy, get a library that supports
"common fractions." If you need to represent irrationals perfectly, you
might start by searching for "continued fractions;" these will help with
some of the non-transcendentals.

Personally, I avoid floating-point numbers whenever possible. Von
Neumann also thought any programmers worth their salt should be able to
keep track of a radix point. If you must work with floating point
numbers in the same way you work with integers (e.g., you want to
compare them for equality), at least be careful to round them to a
realistic number of significant digits first.


感谢您的回复。

这是我第一次去过这个团体。

很多聪明人在这里!


还有一个问题:

如何将一些花车保存到文本文件并仔细阅读?


谢谢。

祝你好运,


Drew


" Jeff Schwab" < JE ****** @ comcast.net>在消息中写道

news:nd ******************** @ comcast.com ...
Thank you for the great responces.
This was the first time I had ever been to this group.
Lots of smart people here!

One more question:
How can you save some floats to a text file and read them back acurately?

Thank you.
Best regards,

Drew

"Jeff Schwab" <je******@comcast.net> wrote in message
news:nd********************@comcast.com...
Drew写道:
嗨:

我很难得到atof()来准确转换3.1
任何想法?
<谢谢,

画了


#include< iostream>
#include< math.h>

使用命名空间std;
int main(int argc,char * argv [])
{/>
浮点数fNum;
char cNumBuffer [ ] =" 3.1";
fNum =(float)atof(cNumBuffer);
//为什么这个存储在fNum中的3.099999905

返回0;
}
Hi:

I''m having trouble getting atof() to accurately convert "3.1"
Any ideas?

Thank you,

Drew

#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char* argv[])
{

float fNum;
char cNumBuffer[]="3.1";
fNum =(float)atof(cNumBuffer);
//Why does this store 3.099999905 in fNum

return 0;
}



罗恩说的话。此外,3.1可证明是无法代表的,因为.1
= 10 ^ -1的整数幂的有限序列的传统和,并且10具有除2之外的素因子。
GCC维护人员将此列为最常报告的非bug。

http://gcc.gnu.org/bugs.html#nonbugs

您可以使用常用分数的库。如果你需要完美地表示非理性,你可以从搜索连续分数开始。这些将有助于
一些非超验证。

就个人而言,我尽可能避免使用浮点数。 Von
Neumann还认为任何值得他们盐的程序员都应该能够跟踪小数点。如果你必须像处理整数一样使用浮点数(例如,你想比较它们的相等性),至少要小心将它们四舍五入到一个逼真的有效位数首先。


What Ron said. Furthermore, 3.1 is provably unrepresentable as the
traditional sum of a finite sequence of integer powers of two, since .1
= 10^-1, and 10 has a prime factor other than two.

The GCC maintainers list this as their most often reported non-bug.

http://gcc.gnu.org/bugs.html#nonbugs

You can get a specified number of significant or reliable digits using
sprintf, or a more type-safe alternative. If you need to represent
rational numbers with absolute accuracy, get a library that supports
"common fractions." If you need to represent irrationals perfectly, you
might start by searching for "continued fractions;" these will help with
some of the non-transcendentals.

Personally, I avoid floating-point numbers whenever possible. Von
Neumann also thought any programmers worth their salt should be able to
keep track of a radix point. If you must work with floating point
numbers in the same way you work with integers (e.g., you want to
compare them for equality), at least be careful to round them to a
realistic number of significant digits first.



这篇关于为什么不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆