views:

79

answers:

1

I have a some doubles that I want to port into NSDecimalNumber, because I'm getting too bad floating-point arithmetic errors with them.

They are:

double one = 0.0000001;
double two = 1000000000.0000001;

They are very problematic, but I hope that NSDecimalNumber can help out to get calculations right. I'm not that big math genius, so I wonder how to provide the correct input to this method of NSDecimalNumber.

Let me try:

NSDecimalNumber *one = [NSDecimalNumber decimalNumberWithMantissa:1 exponent:-7 isNegative:NO];

NSDecimalNumber *two = [NSDecimalNumber decimalNumberWithMantissa:10000000000000001 exponent:-7 isNegative:NO];

I feel that's wrong. I could only guess. The documentation does not provide much information on that. But as far as I get it, the "mantissa" is an integer and the exponent tells where the floating point should be, by adding zeros. Probably this is also wrong ;-)

I have seen some code snippets where people just feeded a CGFloat as mantissa and provided a 0 as exponent, but I can only guess what their intention was, so I can't just do it the same way without understanding it.

Any idea?

+1  A: 

As of the reference of the class, numbers are represented as mantissa x 10^exponent.

So, you are pretty right with your assumptions.

Your number is first of all the mantissa. The decimal point is after your number and gets pushed to the right exponent times (if the exponent is positive, zeroes will be added as the points stays on the right side). In the end, a minus sign will be put before your number according to the isNegative flag.

So for example, if you have 123 * 10 ^ (-3), it starts with 123., goes over 12.3 and 1.23 to 0.123, where the decimal point is three digits left as before

Note that this type also has restrictions. The reference shows them:

[...]where mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127

Maybe, Wikipedia is able to explain the scientific notation of numbers better, which is exactly what you have to use here :-)

Etan
thanks Etan, I think my old school memories come back now. Only that negative exponent thing is not too clear to me. i.e. if my mantissa is 123, and my exp is -3, would I have a number like 0.000123 then?
HelloMoon
10 ^ -3 is 0.001, not 0.0001, so if your mantissa is 1.23 and your exp is -3 you'd have 0.00123.
David Maymudes
if mantissa was 123 and exp -3 that would be 0.123
David Maymudes
sorry there was indeed some error in my explanation. the decimal point is moved exponent digits to the _right_, not as stated in my original post to the _left_. I have included your 123*10^-3 example into my post., 0.000123 would be 123 * 10^-6 = 12.3 * 10^-5 = 1.23 * 10^-4 = 0.123 * 10^-3 = 0.0123 * 10^-2 = 0.00123 * 10^-1 = 0.000123. Hope that this helped you with your problem.
Etan
Thanks guys, now all this mantissa and exponent stuff seems clear to me. 38 digits for NSDecimalNumber also includes those "fractional" digits, right? So the "worst balanced" number I could have is something like 9999999999999999999.9999999999999999999 * 10^-128 or *10^127? should be enough for my stuff ;)
HelloMoon