Internals of Decimal data type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael A. Covington

    Internals of Decimal data type

    Microsoft's documentation is a bit unclear, but is Decimal in fact a
    radix-100 (base-100) arithmetic data type (in which each byte ranges only
    from 0 to 99)?

    It should be straightforward to dump some Decimal values onto a binary file
    (or into a stream that writes into an array of bytes) and examine them.


  • Jon Skeet [C# MVP]

    #2
    Re: Internals of Decimal data type

    Michael A. Covington <look@ai.uga.ed u.for.address> wrote:[color=blue]
    > Microsoft's documentation is a bit unclear, but is Decimal in fact a
    > radix-100 (base-100) arithmetic data type (in which each byte ranges only
    > from 0 to 99)?[/color]

    No. It's a 96-bit integer, a sign bit, and an exponent in the range
    0-28. (The exponent is used to divide, not multiply the mantissa - in
    other words, an exponent of 28 shows a much smaller number than an
    exponent of 0 (for the same mantissa)).
    [color=blue]
    > It should be straightforward to dump some Decimal values onto a binary file
    > (or into a stream that writes into an array of bytes) and examine them.[/color]

    You don't even need to do that - just use Decimal.GetBits .

    See http://www.pobox.com/~skeet/csharp/decimal.html for a bit more
    information.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Michael A. Covington

      #3
      Re: Internals of Decimal data type

      Many thanks. But if it's binary, why did they call it Decimal? :)


      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1ca1ed c3b572ecee98be7 3@msnews.micros oft.com...[color=blue]
      > Michael A. Covington <look@ai.uga.ed u.for.address> wrote:[color=green]
      >> Microsoft's documentation is a bit unclear, but is Decimal in fact a
      >> radix-100 (base-100) arithmetic data type (in which each byte ranges only
      >> from 0 to 99)?[/color]
      >
      > No. It's a 96-bit integer, a sign bit, and an exponent in the range
      > 0-28. (The exponent is used to divide, not multiply the mantissa - in
      > other words, an exponent of 28 shows a much smaller number than an
      > exponent of 0 (for the same mantissa)).
      >[color=green]
      >> It should be straightforward to dump some Decimal values onto a binary
      >> file
      >> (or into a stream that writes into an array of bytes) and examine them.[/color]
      >
      > You don't even need to do that - just use Decimal.GetBits .
      >
      > See http://www.pobox.com/~skeet/csharp/decimal.html for a bit more
      > information.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Michael A. Covington

        #4
        Re: Internals of Decimal data type


        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        news:MPG.1ca1ed c3b572ecee98be7 3@msnews.micros oft.com...[color=blue]
        > Michael A. Covington <look@ai.uga.ed u.for.address> wrote:[color=green]
        >> Microsoft's documentation is a bit unclear, but is Decimal in fact a
        >> radix-100 (base-100) arithmetic data type (in which each byte ranges only
        >> from 0 to 99)?[/color]
        >
        > No. It's a 96-bit integer, a sign bit, and an exponent in the range
        > 0-28. (The exponent is used to divide, not multiply the mantissa - in
        > other words, an exponent of 28 shows a much smaller number than an
        > exponent of 0 (for the same mantissa)).
        >
        > See http://www.pobox.com/~skeet/csharp/decimal.html for a bit more
        > information.[/color]

        Actually, that page says it's radix-10 but then doesn't go into the details
        of it. I plan to explore a little. More news soon.

        Thanks!



        Comment

        • Michael A. Covington

          #5
          Re: Internals of Decimal data type

          You're right, and to put it even more clearly:

          The Decimal type is a binary integer stored with an offset that represents a
          power of 10, and not normalized.

          Thus 1 and 1.0 are different numbers. The latter is stored as 10 offset 1
          decimal place to the right.

          This combines the speed of binary arithmetic with the ability to represent
          decimal numbers exactly, and, indeed, to remember how many decimal places
          were given (so if you give a price as $1.00 it remains 1.00, not 1 or 1.0).

          Clever... but poorly documented!


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Internals of Decimal data type

            Michael A. Covington <look@ai.uga.ed u.for.address> wrote:[color=blue]
            > You're right, and to put it even more clearly:
            >
            > The Decimal type is a binary integer stored with an offset that represents a
            > power of 10, and not normalized.
            >
            > Thus 1 and 1.0 are different numbers. The latter is stored as 10 offset 1
            > decimal place to the right.
            >
            > This combines the speed of binary arithmetic with the ability to represent
            > decimal numbers exactly, and, indeed, to remember how many decimal places
            > were given (so if you give a price as $1.00 it remains 1.00, not 1 or 1.0).
            >
            > Clever... but poorly documented![/color]

            Not particularly poorly documented, really - the docs for
            System.Decimal are pretty clear about all but the normalisation:

            <quote>
            The binary representation of an instance of Decimal consists of a 1-bit
            sign, a 96-bit integer number, and a scaling factor used to divide the
            96-bit integer and specify what portion of it is a decimal fraction.
            The scaling factor is implicitly the number 10, raised to an exponent
            ranging from 0 to 28.
            </quote>

            That, to me, can't easily be misinterpreted in the way that your first
            post suggests where each byte is between 0 and 99.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...