473,545 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1972
Michael A. Covington <lo**@ai.uga.ed u.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Michael A. Covington <lo**@ai.uga.ed u.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Michael A. Covington <lo**@ai.uga.ed u.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.ed u.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.co m>
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
4497
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
2017
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 Decimal objects and react appropriately. The problem is that they only work with Decimals if the custom type is on the left (and therefore...
17
6113
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to...
5
308
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 some Decimal values onto a binary file (or into a stream that writes into an array of bytes) and examine them.
25
3005
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 rounding behaviour matches more closely what humans expect. More to the point: 0.1 and 0.01 are exact in Decimal and not exact in float. ...
3
3954
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 displayed as 5109.0000 when I view the table data. I'm not sure why 4 decimal places are being shown, and how would I get the table data to...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.