C#: Not a careful standard


I don’t know why, but I did. I guess I was just curious. I looked at the C# standard, ECMA 334 (now an ISO standard).

A few seconds in I notice the following:

Section 11.1.6 describes the set of values in the double type:

“The finite set of non-zero values of the form s × m × 2e, where s is 1 or −1, and for double, 0 < m < 253 and −1075 ≤ e ≤ 970.”

It also says that double is represented using the 64-bit double precision IEC 60559. Now of course I can’t actually get a copy of that standard, but it’s well known that the exponent for double precision is 11-bits wide with a bias of 1023. That means that the smallest (positive) normalised number is 2-1022; the smallest denormal is 2-52 times that value: 2-1074. Still, the C# boys nearly got it right. They’re off-by-one on the top end too, the largest finite double is (253-1) × 2971.

Like a boy with a new toy (I discovered Python’s struct module a few days ago), I can check the math with Python:

>>> struct.unpack(‘d’, struct.pack(‘Q’, 1))[0]
>>> 2**-1074

Further on in the same section the standard says: “The double type can represent values ranging from approximately 5.0 × 10−324 to 1.7 × 10308 with a precision of 15–16 digits.” At least they get the range right in decimal. I still find this slightly misleading because down in the denormal range you don’t get 15-16 digits of precision (consider, 3e-324 == 7e-324). It would be more accurate to say “The double type can represent values ranging from approximately 5.0 × 10−324 to 1.7 × 10308, with a precision of 15–16 digits when the magnituitude is greater than approximately 2.2 × 10-308.”

These are tedious details; some might think them a little bit dull, but they’re details that a standard should get right. After all, if the guy writing the standard isn’t going to get them right, who is? Your vendor? (mwahaha) It’s sloppy and makes me wonder what else might they have got wrong. Am I encouraged to continue reading? No.

Sorry, wrong category because wordpress thinks C, C++, and C# are all the same. Meh.

4 Responses to “C#: Not a careful standard”

  1. glorkspangle Says:

    From what you say, 0.0 is not a member of the double type.

  2. glorkspangle Says:

    Oh, and I think we have a paper copy of the IEEE float standard somewhere in the office.

  3. drj11 Says:

    I trimmed too harshly. The section first goes to mention the zeroes, the infinities, and the NaNs, then goes on to discuss the finite non-zero values.

  4. drj11 Says:

    “4th edition”. *snigger*

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: