10

(while trying to analyze how decimal works ) && after reading @jonskeet article and seeing msdn , and thinking for the last 4 hours , I have some questions :

in this link they say something very simple :

1.5 x 10^2 has 2 significant figures

1.50 x 10^2 has 3 significant figures.

1.500 x 10^2 has 4 significant figures etc...

ok...we get the idea.

from jon's article :

 sign * mantissa / 10^exponent

As usual, the sign is just a single bit, but there are 96 bits of mantissa and 5 bits of exponent

 ^ _ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^___ ^^^^^

 1 _                     96                         5

ok

so max mantiss val = 2^96-1 = 79228162514264337593543950335 which is : 7.9228162514264*10^28 (according to my iphone... could'nt see exponent representation in windows calc.)

notice :

7.9228162514264*10^28 has 14 significant figures (according to examples above)

now the part with the 5 bit in exponent is irrelevant because its in the denominator - so i need the min val which is 2^0

question #1 :

msdn say : enter image description here 28-29 significant digits

but according to my sample (1.500 x 10^2 has 4 significant figures) they have 2 significant figures which is 7.9 ( 7 and 9).

if msdn would have written :

±79228162514264337593543950335 × 10^0

i would understand this , since all significant digits are in the expression.

why do they write 28-29 but display 2 ?

question #2 :

how will decimal representation ( mantiss && exponent) will be displayed for the value 0.5 ?

the max denominator can be 2^32-1 --> 31

thanks guys.

question #3 :

1+96+5 = 102 bits.

msdn says :

The decimal keyword denotes a 128-bit data type.

128-102 = 26

could understnad from article why there isnt a usage to those 26 bits

Royi Namir
  • 144,742
  • 138
  • 468
  • 792

2 Answers2

7

They've given the range to just two significant digits, but specified the precision separately. That's why the range is listed as "approximate range".

The decimal representation of 0.5 would be a mantissa of 5 and an exponent of 1 (which is treated in the inverse sense to normal, i.e. it's effectively -1).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @RoyiNamir: No, the mantissa is 5. What made you think it's got anything to do with 2^5? I suspect you've misunderstood what a mantissa is. – Jon Skeet May 16 '12 at 08:35
  • oh sorry...:) after 4 hours of searching im thinking in base 2 :) – Royi Namir May 16 '12 at 08:35
  • @RoyiNamir: The base is irrelevant though - you never raise a base to the power of a mantissa; that's what an *exponent* is for. – Jon Skeet May 16 '12 at 08:38
  • can i say that the precision ( decimal digits at the right to the point) is accurate within 28 digits ? so if i have 2 fractions of 28 decimals being added - the result will be accurate ? is it true to say ? – Royi Namir May 16 '12 at 08:38
  • @RoyiNamir The decimal fraction's precision is accurate within a range of 2^96/10 providing the number is between 0 and 1. – Danny Varod May 16 '12 at 08:44
  • @DannyVarod sorry i dont understand this. my question is simple. `i can work safely accurate with X digits to the right of the decimal point`. what is X ? – Royi Namir May 16 '12 at 08:48
  • @RoyiNamir You can work safely with a total of N digits including those to the left of the decimal point. In other words, depends on the rest of the number. – Danny Varod May 16 '12 at 08:54
3

why do they write 28-29 but display 2?

For readability. It says "Approximate range" and 7.9E28 is more readable than 79228162514264337593543950335 (E0 here, not E1).

how will decimal representation ( mantiss && exponent) will be displayed for the value 0.5 ?

The exponent's range is -28...0, however, it is stored (and received via constructor parameters) as an absolute value of 0...28.

So 0.5 Would have the same mantissa representation as 5 with an exponent of -1 (stored as 1).

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • You're getting confused between exponent and mantissa in the "if the exponent is 5 bits" part. Which exact bit of MSDN do you believe to be inaccurate? – Jon Skeet May 16 '12 at 08:46
  • a. The mantissa is 96bits, the exponent is 5 - where exactly did I confuse the two. b. "The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28." - There is no mention of negative exponents. – Danny Varod May 16 '12 at 08:52
  • So the exponent doesn't have its own sign? - How do you know if you have to use E+n or E-n then? – Danny Varod May 16 '12 at 10:29
  • (I was just in the middle of adding another comment, btw.) The exponent for decimal is always in the range 0-28, as specified in the documentation. It's then *used* in the inverse sense of a tradtional exponent, as documented (value = sign * mantissa / 10^exponent) – Jon Skeet May 16 '12 at 10:30
  • I still think your statement of "If the exponent is 5bits and is signed, then the full range is -2^31 .. 2^31 - 1" is extremely confused and/or confusing. I certainly don't know what you mean by it, despite having a pretty good understanding of `decimal`. Note that the range depends on the size of the mantissa as well as the exponent. – Jon Skeet May 16 '12 at 10:31
  • Ahh so it is actually -28 to 0. – Danny Varod May 16 '12 at 10:36
  • It depends on how you want to view it. It's *stored* as if it were an unsigned integer in the range 0-28, and that non-negative value is used in a sort of "inverse" way, or as the normal exponent of a divisor instead of a multiplier. I think that's the clearest way of looking at it, personally, but YMMV. I think it's confusing to claim in this answer that the range is -28 to 0 (with no usage or storage explanation) when the *documented* range is 0-28 with a complete explanation of how it's stored and used. – Jon Skeet May 16 '12 at 10:44