471,348 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

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.
Jul 21 '05 #1
5 1828
Michael A. Covington <lo**@ai.uga.edu.for.address> wrote:
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)?
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)).
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Many thanks. But if it's binary, why did they call it Decimal? :)
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael A. Covington <lo**@ai.uga.edu.for.address> wrote:
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)?


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


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael A. Covington <lo**@ai.uga.edu.for.address> wrote:
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)?


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.


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!

Jul 21 '05 #4
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!
Jul 21 '05 #5
Michael A. Covington <lo**@ai.uga.edu.for.address> wrote:
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!


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Batista, Facundo | last post: by
7 posts views Thread by Blake T. Garretson | last post: by
5 posts views Thread by Michael A. Covington | last post: by
25 posts views Thread by Lennart Benschop | last post: by
3 posts views Thread by =?Utf-8?B?UGFvbG8=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.