浮点数发行 [英] String To Float Issues

查看:111
本文介绍了浮点数发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个允许用户输入浮点值的应用程序.将这些值读入CString,然后转换为浮点整数以存储到文件中.一切顺利,直到我开始测试输入数据.具有.1,.4,.6,.9的任何数字均未正确转换.

我首先使用atof().然后我尝试了scanf().和sscanf()并得到相同的问题.

我什至编写了一个例程来解析字符串,提取了整数和小数.我注意到在分割和转换浮点数时遇到了同样的问题!例如,如果您有一个数字,说15确实是.15,然后将其除以100,则得到.1500001.

我看到了一些使用mod()和pow()舍入数字的示例.但是,我不知道舍入是什么,因为输入的范围可以是(99到.000013).

我想我的问题是是否有一种方法可以为atof()或scanf()添加类似于printf(str,%3.5f",data);的精确参数;"?

最好的问候,

Hello All,

I have an application that allows the user to input floating point values. These values are read into a CString and then converted to a floating point integer to be stored into a file. Everything went great until I started tested the input data. Any number that had a .1, .4, .6, .9 was converted incorrectly.

I first used atof(). Then I tried scanf(). And sscanf() and got the same issues.

I even wrote a little routine to parse through the string, pluck the whole number and fraction. I noticed when dividing and casting floats I got the same issue! For example if you have a number say 15 that is really .15 and you divide it by 100 you get .1500001.

I saw some example where mod() and pow() were used to round the number. However, I don’t know what the rounding will be since the inputs can range from (99 to .000013).

I guess my question at hand is "is there a way to add a precision argument for an atof() or scanf() similar to printf(str,"%3.5f",data);"?

Best regards,

推荐答案

你好,

就像其他答案所说的,这是如何存储浮点值的问题.

atof()是更好的选择.应该声明为 double atof ( const char * string); .使用双精度变量而不是浮点数,您将拥有足够的精度,范围在99到0.000013之间.然后,当对printf()使用正确的格式字符串时,将正确打印值,并四舍五入为在格式字符串中定义的位数.

请注意格式字符串,第一个数字是输出的整个长度,在所有输出上使用%9.6f"表示相同的长度.

另一点是,请注意比较浮点值.

此比较可能会失败,因为atof()的结果是.1500001,这与.15不一样!
Hello,

as the other answers say, it''s a matter of the kind how floating point values are stored.

atof() is the better choice. It should be declared as double atof( const char *string );. Use a double variable instead of a float and you will have a precision that is enough for your range of 99 to 0.000013. Then, when you use the correct format string for printf() the values are printed correct, rounded to the number of digits you defined in the format string.

Take care on the format string, the first number is the over all length of the output, use "%9.6f" for the same length on all outputs.

Another point is, take care on comparing floating point values.

This comparison may fail, because the result of atof() is .1500001 what is not the same as .15!
double dValue = atof( strInput);
if( .15 == dValue)
{
}



而是使用这样的东西:



Instead use somthing like this:

double dValue = atof( strInput);
if( fabs( .15 - dValue) < 0.01)
{
}


比较值(0.01)取决于您可以接受的公差.

最好的问候


The comparison value (0.01) depends on the tolerance you can accept.

Best regards


我所熟悉的所有数字计算机都是二进制的.您得到的结果不是您期望或想要的,但是是正确的.使用浮点数和双精度数时,通常应考虑使用近似值.数字和机器算术的机器表示与真实的东西不太一样.

在基数10中,您不能精确地以终止小数部分的形式写1/3.同样,您不能以终止二进制点分数的形式确切地写一个分母不是2的幂的分数.使用二进制机器浮点数时还涉及其他工件.

如果您的应用程序中不接受机器浮点算术的工件,那么您将不得不使用浮点数或双精度数之外的其他方法.

一种替代方法是使用缩放的整数.另一个是使用某种类型的任意精度数学程序包.

使用哪种方法取决于您要处理的情况,以及哪种用机器表示数字的不同方式最匹配.
All digital computers that I am familiar with are binary. The results you got are not what you expected or wanted, but are correct. When you are working with floats and doubles, you should generally consider that you are working with approximations. Machine representations of numbers and machine arithmetic aren''t quite the same as the real thing.

In base 10 you cannot write 1/3 exactly in the form of a terminating decimal fraction. In the same way, you cannot write any fraction with a denominator that is not a power of 2 exactly in the form of a terminating binary point fraction. There are also other artifacts involved in working with binary machine floating point numbers.

If the artifacts of machine floating point arithmetic are not acceptable in your application, then you will have to use something other than floats or doubles.

One alternative might be to use scaled integers. Another is to use an arbitrary precision math package of some type.

What to use depends on the situation you are dealing with and what matches up best with the different ways of representing numbers with a machine.


浮点数的精度约为6位.但是,它们的存储方式类似于科学计数法.它存储一个数字以及一个大小,以设置该数字中小数点应出现的位置.例如,您可以使用数字654321,然后通过将小数点放在中间(654.321),在开始(0.000000654321)之前或在结束(654321000000)之后将小数点进行调整.但是,您只能得到大约6位数字来存储该数字的精度.如果要提高精度,可以使用双精度而不是浮点型.双打的精度约为15位.有关更多信息,请参见.

就像另一个答案所说的,您可以使用一个将数字存储为分数的库.这样,您几乎可以拥有无​​限的精度.请记住,这些数字占用更多内存并且使用缓慢.您将必须进行一些搜索,但是这里有一些起点,因此您知道要查找的内容:扩展十进制类别,以及分数数字类别.
Floats have about 6 digits of precision. However, they are stored similar to scientific notation. It stores a number along with a magnitude to set where in that number the decimal point should appear. For example, you could have the number 654321 and then adjust it by placing the decimal point in the middle (654.321), before the beginning (0.000000654321), or after the end (654321000000). However, you only get about 6 digits to store the precision of that number. If you want more precision, you can use a double instead of a float. Doubles have about 15 digits of precision. For more information, see this.

As the other answer said, you can use a library that stores numbers as fractions. That way, you can have virtually unlimited precision. Just keep in mind that these numbers take up more memory and are slow to use. You''ll have to do some searching, but here are some starting points so you know what to look for: an extended decimal class, and a fractional numbers class.


这篇关于浮点数发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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