472,805 Members | 1,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 1925
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.