20

I have come across a confusing pattern of the size and Max value of these data types in C#.

While comparing these size using Marshal.SizeOf(), I have found following result-

Float- 4 bytes,
Double - 8 bytes,
Decimal - 16 bytes

and when compared their MaxValues, i got the results like this,

 Float- 340282346638528986604286022844204804240,
 Double - 179769313486231680088648464220646842686668242844028646442228680066046004606080400844208228060084840044686866242482868202680268820402884062800406622428864666882406066422426822086680426404402040202424880224808280820888844286620802664406086660842040886824002682662666864246642840408646468824200860804260804068888,
 Decimal - 79228162514264337593543950335

The reason I am confused is, Decimal takes more unmanaged memory than Float and Double but is not able to contain larger value than float even. Can anyone explain this behavior of Compiler. Thanks.

Rohit Prakash
  • 1,975
  • 1
  • 14
  • 24
  • 15
    From [MSDN](http://msdn.microsoft.com/en-us/library/364x0z75.aspx): _Compared to floating-point types, the decimal type has **more precision** and a **smaller range**, which makes it appropriate **for financial and monetary calculations**._ Not sure if this should be an answer, too easy to find this information. – awesoon Feb 22 '14 at 08:52
  • 1
    @soon: it should be an `ANSWER` – Sudhakar Tillapudi Feb 22 '14 at 09:01
  • 1
    @SudhakarTillapudi, Really not sure. Looks like a duplicate for me. – awesoon Feb 22 '14 at 09:05
  • Thank soon for the quite well response. I had already read that post, but that doesn't solved a real question. Why decimal needs 16 bit memory to output more precise calculation ? – Rohit Prakash Feb 22 '14 at 09:35
  • @RohitPrakash, Do you want to know how does `decimal` work under the hood? – awesoon Feb 22 '14 at 09:46
  • @Soon, In fact my question is; Is there any relation between occupied memory and their max values, and if decimal is not having a larger values than float/double, why should it take 16 bit memory to perform its operation ? – Rohit Prakash Feb 22 '14 at 09:55
  • 2
    @Rohit: See this link for answeer from Jon Skeet, this will clear your doubts. the whole discussion is worth reading : http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c – Milan Raval Feb 22 '14 at 09:56
  • To see what is really going on you would need to look at the internal representations of the data types. However, there is a general principle that binary is the most efficient way to use inherently binary hardware to represent numbers. Mapping decimal floating point onto binary storage costs bits due to the mismatch between the number requirements and the hardware, but is worth it when you need exact representation of decimal fractions. – Patricia Shanahan Feb 22 '14 at 12:47
  • 3
    Here's a hint: If you calculate `60.0/7.0` with `double`s, you get the result `8.57142857142857` (will show as `8.5714285714285712` with roundtrip formatting, last figure `2` is clearly inaccurate). But if you do the `decimal` calculation `60m/7m` you get the result `8.571428571428571428571428571m`. You see the result is much "longer"? That requires more bytes. – Jeppe Stig Nielsen Feb 22 '14 at 21:36

0 Answers0