Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimal in c# misunderstanding?

Tags:

c#

.net

decimal

clr

(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 here28-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

like image 579
Royi Namir Avatar asked May 16 '12 08:05

Royi Namir


2 Answers

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).

like image 185
Jon Skeet Avatar answered Oct 29 '22 01:10

Jon Skeet


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).

like image 25
Danny Varod Avatar answered Oct 28 '22 23:10

Danny Varod