2

Is Decimal type in C# follow the same rules (formula,normalized/denormalized,implied 1,Exponent bias) of classic double representation (IEEE-754 standard) except the use of base 10 instead of base 2.

What does implied the use of base 10 instead of base 2 ?

Is there, like "IEEE-754 double", the same behavior namely some gaps between adjacent values even for a finite precision (like 28/29 digits)?

Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
  • Any representation of real numbers that uses finite memory has to contain gaps between adjacent values. – svick Jan 31 '12 at 12:16
  • AFAIK decimal is not compatible with anything - it's a Microsoft only invention - so you can't think of it in the same terms as normal floating point stuff. And it doesn't have subnormal values. – Mr Lister Jan 31 '12 at 12:17

1 Answers1

4

No, C# decimal doesn't follow the same rules as IEEE 754 floating point numbers. The C# 4 specification is quite clear on how it should behave (§4.1.7):

The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 × 10−28 to approximately 7.9 × 1028 with 28-29 significant digits.

The finite set of values of type decimal are of the form (–1)s × c × 10-e, where the sign s is 0 or 1, the coefficient c is given by 0 ≤ c < 296, and the scale e is such that 0 ≤ e ≤ 28. The decimal type does not support signed zeros, infinities, or NaN's. A decimal is represented as a 96-bit integer scaled by a power of ten. For decimals with an absolute value less than 1.0m, the value is exact to the 28th decimal place, but no further. For decimals with an absolute value greater than or equal to 1.0m, the value is exact to 28 or 29 digits. Contrary to the float and double data types, decimal fractional numbers such as 0.1 can be represented exactly in the decimal representation. In the float and double representations, such numbers are often infinite fractions, making those representations more prone to round-off errors.

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514
  • There's a further discussion of the guarantee(s) given by the C# specification(s) in the answers and comments here: http://stackoverflow.com/questions/5018187/is-net-decimal-arithmetic-independent-of-platform-architecture – LukeH Jan 31 '12 at 12:39