473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 1955
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
7
by: Blake T. Garretson | last post by:
I'm having some issues with decimal.Decimal objects playing nice with custom data types. I have my own matrix and rational classes which implement __add__ and __radd__. They know what to do with...
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
5
by: Michael A. Covington | last post by:
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...
25
by: Lennart Benschop | last post by:
Python has had the Decimal data type for some time now. The Decimal data type is ideal for financial calculations. Using this data type would be more intuitive to computer novices than float as its...
3
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I have a table column of SQL smallmoney type which I am updating via a form input field defined as decimal. If I enter, say, 51.09 via the decimal input field (representing $51.09), this is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.