Integer constants

J

Jon Shemitz

I was experimenting with implicit typing of integer constants, and
found to my surprise that in MS C# 1.1 (VS.2003) you can write code
like

double Large = 18446744073709551615;

This is quite a bit bigger than ulong.MaxValue - in fact, it's
ulong.MaxValue * 2 + 1. I'm curious - could someone who has Mono
installed check to see if the Mono C# compiler can handle this line?
 
K

Klaus H. Probst

Well... you're using a double there, not an int or a long. If you try a long
you'll probably get a compiler error along the lines of "can't do implicit
cast of 'ulong to long'. And if you cast it you'll still get an error and a
recommendation that you use unchecked code... which is not a good idea to
say the least =)
 
J

Jon Shemitz

Klaus H. Probst said:
Well... you're using a double there, not an int or a long.

Well, yes. Actually, I was expecting that the compiler wouldn't handle
any "double Large = <int constant>;" where <int constant> was any
greater than ulong.MaxValue. I was surprised to fin that that's not
true, that it can go all the way to "double Large =
18446744073709551615;" before demanding code like "double Large =
18446744073709551616d;".
If you try a long
you'll probably get a compiler error along the lines of "can't do implicit
cast of 'ulong to long'. And if you cast it you'll still get an error and a
recommendation that you use unchecked code... which is not a good idea to
say the least =)

Thanks, but not news, and not what I'm asking about ....
 
J

Jon Skeet [C# MVP]

Jon Shemitz said:
Well, yes. Actually, I was expecting that the compiler wouldn't handle
any "double Large = <int constant>;" where <int constant> was any
greater than ulong.MaxValue. I was surprised to fin that that's not
true, that it can go all the way to "double Large =
18446744073709551615;" before demanding code like "double Large =
18446744073709551616d;".

Indeed - according to the C# spec, I that it shouldn't compile.
 
D

Daniel O'Connell [C# MVP]

Jon Shemitz said:
I was experimenting with implicit typing of integer constants, and
found to my surprise that in MS C# 1.1 (VS.2003) you can write code
like

double Large = 18446744073709551615;

This is quite a bit bigger than ulong.MaxValue - in fact, it's
ulong.MaxValue * 2 + 1. I'm curious - could someone who has Mono
installed check to see if the Mono C# compiler can handle this line?

Mono's mcs 0.29 accepts this as well, didn't check to ensure it was correct,
but it does accept it.
 
J

Jon Shemitz

Daniel O'Connell said:
Mono's mcs 0.29 accepts this as well, didn't check to ensure it was correct,
but it does accept it.

Thanks! Didn't really want to install mono on my ancient Linux machine
just for a footnote (that might get canned anyway).
 
D

Daniel O'Connell [C# MVP]

Jon Shemitz said:
Thanks! Didn't really want to install mono on my ancient Linux machine
just for a footnote (that might get canned anyway).
Hehe, ya, I run it on my windows machien actually; rotor, mono, and the
framework. Sometimes I have to be careful what compiler I'm actually
using, -_-.
 
J

Jon Shemitz

I was experimenting with implicit typing of integer constants, and
found to my surprise that in MS C# 1.1 (VS.2003) you can write code
like

double Large = 18446744073709551615;

This is quite a bit bigger than ulong.MaxValue - in fact, it's
ulong.MaxValue * 2 + 1. I'm curious - could someone who has Mono
installed check to see if the Mono C# compiler can handle this line?

I feel so very silly - this IS ulong.MaxValue.

I must have done a Console.WriteLine() of long.MaxValue, not
ulong.MaxValue, and copy/pasted THAT value to experiment with. Of
COURSE the ulong.MaxValue would be long.MaxValue * 2 + 1, and it makes
perfect sense that that would be the largest "integral constant".

So sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top