decimal in c# misunderstanding?

霸气de小男生 提交于 2020-01-01 04:38:06

问题


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

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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/10614592/decimal-in-c-sharp-misunderstanding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!