Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

iphone - Correcting floating point numbers

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...