views:

180

answers:

1

Mathematically any number with an exponent of 0 is supposed to equal 1 (my remedial math research assures me).

But this code produces 5:

[NSDecimalNumber decimalNumberWithMantissa:5 exponent:0 isNegative:NO]

Is this something standard in computer programming -- that can be relied upon not being "corrected" in future Cocoa versions?

+3  A: 

Hi NewMath,

You probably haven't read the documentation for decimalNumberWithMantissa:exponent:isNegative:

The exponent is related to the multiple of 10 that the mantissa will have.

You have to understand that the mantissa is the value on the right hand of the point.

The normalization of the number turns a value like 18.23 = 0.1823x10^2 --> mantissa == 1823, exponent == 2

Take a look at it to see how to use.

Discussion

The arguments express a number in a kind of scientific notation that requires the mantissa to be an integer. So, for example, if the number to be represented is –12.345, it is expressed as 12345x10^–3—mantissa is 12345; exponent is –3; and isNegative is YES, as illustrated by the following example.

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa:12345

                                           exponent:-3

                                           isNegative:YES];

Cheers,
VFN

PS: Check out the article on Wikipedia for: Standard for Floating-Point Arithmetic (IEEE 754 - 2008).

vfn
To clarify: the exponent in that method refers to the exponent on *ten*, not on the number itself (so @NewMath, your mantissa 5 and exponent 0 is not 5^0, but instead 5x10^0 = 5x1 = 5).
Tim