Floating point correction

I am wondering if there is a way that you can easily and safely fix floating point numbers.

For instance,

When entering: "32 + 32.1" Result: "64.0999999999999"

I must also mention that this happens quite often when using scientific notation. "(2.3 * 10 ^ 23) * (1.452 * 10 ^ 23)" Returns: "3.339599999999999999e + 46"

And finally, sometimes a return number: ex. +123.0000000000001

Thanks for the help!

EDIT

The answer that has been approved is great. But what I found worked for me, used% g with double in NSString stringWithFormat. It seems that% g rounds everything off quite appropriately. e.g.

answer.text = [NSString stringWithFormat@ " %g ", doubleAnswer]; 

Using doubles through your calculations and then using this method seemed to work for me, and I hope this helps others too. If this is not the answer you are looking for, check out the approved answer!

+6
source share
2 answers

Floating point numbers do not very well indicate specific numbers. Using double will make this happen less often, but you will still have a 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 a value and have only so many bits, so they quickly end and round to the nearest value that they can represent.)

Usually you donโ€™t worry about +/-. 0000001 and just format them when you want to display them to the user with the corresponding number of decimal points, and he will be rounded. Inside, it does not matter how it is stored.

For example, if you want to display "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 floating point to 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 this for you, but it NSDecimalNumber little more involved and includes more overhead, but here is 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 
+8
source

You cannot โ€œfixโ€ a floating point number because floating point numbers simply cannot represent the number you need.

Floating-point numbers are remarkably inaccurate and, as a rule, should not be used for anything that involves a lot of calculations, because the cumulative error is often disastrously bad.

Use double instead. They can represent a much wider range of values โ€‹โ€‹and, as a result, can represent much more values โ€‹โ€‹exactly. If this is not enough, you need to move on to something with greater accuracy. Maybe NSDecimalNumber .

+2
source

Source: https://habr.com/ru/post/912638/


All Articles