6

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!

Neil
  • 2,004
  • 3
  • 23
  • 48

2 Answers2

8

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
lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • 1
    ahh true true, but what if I wanted to display it as 62.1, but still leave the possibility to display 64.1065 for example (this is what I'm looking for btw, thanks.) – Neil Apr 06 '12 at 21:46
  • 1
    Then you either need to track the decimals that you want to display (messy) or not use float and move to anther type of variable (probably `NSDecimalNumber`). – lnafziger Apr 06 '12 at 21:48
  • 1
    It's a little more complicated but will definitely maintain the numbers as you expect them to. I'll add a quick example to get you started. – lnafziger Apr 06 '12 at 21:53
  • 1
    I added one more example for you that might help! – lnafziger Apr 06 '12 at 22:10
2

You can't "correct" a floating point number because floating point numbers simply cannot represent the number you want.

Floating point numbers are remarkably imprecise and, generally, shouldn't be used for anything that involves lots of calculations because the cumulative error is often catastrophically bad.

Use a double instead. They can represent a much greater range of values and, as a result, can represent many more values quite precisely. If that isn't good enough, you'll need to move to something with more precision. Possibly NSDecimalNumber.

bbum
  • 162,346
  • 23
  • 271
  • 359