修正浮点数 [英] Correcting floating point numbers
问题描述
我想知道是否有一种方法可以方便而安全地更正浮点数。
例如,
输入时:
32 + 32.1
结果:
64.0999999999999
这在使用科学记数法时频繁发生。
(2.3 * 10 ^ 23)*(1.452 * 10 ^ 23)
退货:
3.339599999999999999e + 46
最后,有时返回的数字是:
ex。
123.0000000000001
感谢您的帮助!
EDIT p>
被批准的答案是伟大的。但我发现为我工作是使用%g与NSString stringWithFormat中的双。 %g似乎圆了一切都很恰当。
ex。
answer.text = [NSString stringWithFormat @%g,doubleAnswer];
通过计算使用双精度,然后使用该方法似乎对我有用,我希望这有助于其他人。如果这不是您寻找的答案,请查看已批准的答案!
浮点数字不代表特定数字很好。使用double会使这种情况发生较少,但你仍然会有问题。有关如何存储浮点数的技术描述(以及为什么我们首先遇到这个问题),请参阅这篇维基百科文章: IEEE 754-1985 。 (基本上,浮点存储为值的二进制表示,并且只有很多位可供使用,因此它们很快用尽,必须舍入到它们能够表示的最接近的值。)
通常你不用担心+/- .0000001,只需要格式化它们,当你想用适当的小数点数显示给用户,它会被四舍五入。
例如,如果你想显示结果,你可以这样做:
float myFloat = 32 + 32.1;
NSString * result = [NSString stringWithFormat:%@%。2f,myFloat];
//结果将包含64.10
如果您不想要固定的十进制数点,你也可以使用 NSNumber
的浮点数到字符串转换能力:
float myFloat = 32 + 32.1;
NSNumber * myNumber = [NSNumber numberWithFloat:myFloat];
NSString * result = [myNumber stringValue];
//结果将包含64.1
NSDecimalNumber
会为你照顾所有这一切,但它涉及更多,涉及更多的开销,但这里是一个例子,让你开始:
NSDecimalNumber * num1 = [NSDecimalNumber decimalNumberWithString:@32];
NSDecimalNumber * num2 = [NSDecimalNumber decimalNumberWithString:@32.1];
//或者因为你使用指数:
// NSDecimalNumber * num2 = [NSDecimalNumber decimalNumberWithMantissa:321 exponent:-1 isNegative:NO];
NSDecimalNumber * myNumber = [num1 decimalNumberByAdding:num2];
NSString * result = [myNumber stringValue];
//结果将包含64.1
I am wondering if there is a way that you can easily and safely correct floating point numbers.
For example,
When entered: " 32 + 32.1 " Result: "64.0999999999999"
Also I must mention that this occurs quite frequently when using scientific notation. " ( 2.3 * 10^23)*(1.452 * 10^23) " Returns: " 3.339599999999999999e+46"
And finally, sometimes the number that is returned is: ex. 123.0000000000001
Thanks for the help!
EDIT
The answer that was approved is great. But what I found worked for me was using %g with a double in NSString stringWithFormat. %g seems to round everything quite appropriately. ex.
answer.text = [NSString stringWithFormat@" %g ", doubleAnswer];
Using doubles through your calculations and then using that method seemed to work for me, and I hope this helps others as well. If this isn't the answer your looking for, check out the approved answer!
Floating point numbers do not represent specific numbers very well. Using double's will make this happen less often but you will still have the problem. For a technical description of how floating point numbers are stored (and why we have this problem in the first place) see this wikipedia article: IEEE 754-1985. (Basically, floats are stored as a binary representation of the value and only have so many bits to use, so they quickly run out and have to round to the nearest value that they are capable of representing.)
Typically you don't worry about the +/- .0000001 and just format them when you want to display them to the user with the appropriate number of decimal points and it will get rounded. Internally it doesn't really matter how it is stored though.
For instance, if you want to display the "result" you can do something like this:
float myFloat = 32 + 32.1;
NSString *result = [NSString stringWithFormat:%@"%.2f", myFloat];
// result will contain 64.10
If you don't want a fixed number of decimal points, you can also use the float to string conversion capability of NSNumber
:
float myFloat = 32 + 32.1;
NSNumber *myNumber = [NSNumber numberWithFloat:myFloat];
NSString *result = [myNumber stringValue];
// result will contain 64.1
NSDecimalNumber
will take care of all of this for you, but it a little more involved and involves more overhead but here's an example to get you started:
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:@"32"];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:@"32.1"];
// Or since you use exponents:
// NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithMantissa:321 exponent:-1 isNegative:NO];
NSDecimalNumber *myNumber = [num1 decimalNumberByAdding:num2];
NSString *result = [myNumber stringValue];
// result will contain 64.1
这篇关于修正浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!