Click to See Complete Forum and Search --> : Yet another stupid question from The Old Guy


ThermoSight
January 12th, 2010, 03:57 PM
Hi,

back again with another of my inane questions.

I am using VS 2008 (is that C# 3.5?) and discovered a result I can't explain in what I thought would be a simple statement.....

in a static, constant-definition class, I have the statement
"public static double test = (2e31 - 1.0);

It seems to compile as 2e31 .... it appears that the "-1.0" term was dropped.

I verified this by setting an int to that value, then asking the debugger to print the result in hex. The debugger reports that the int's value is indeed 0x80 00 00 00 rather than the value 0x7F FF FF FF I expected.

It's as if the compiler is ignoring the "- 1.0".

Does that seem right to you ? does it do it on your machine ?

thanks for helping an old man struggling with age-related issues come to grips once again with computer fundamentals.

bill

rliq
January 12th, 2010, 09:17 PM
Bill,

A few things...

Can you just show exactly how you verified (ie show the bit of code) as I tried the following:

public static double test = (2e31 - 1.0);

public Foo()
{
Int64 sf = (Int64)test;
Int32 tt = (Int32)test;

UInt64 usf = (UInt64)test;
UInt32 utt = (UInt32)test;
}

sf was 0x8000000000000000 (8 + 15 0's)
tt was 0x80000000 (8 + 7 zero's)
usf and utt where both 0x000... (all zeros's)

I tried using Convert.ToInt32(...) and Convert.ToInt64(...), the first gave a range error and the second an overflow error. Same when converting UInt32 or UInt64.

So yes, I experience the same problem.

Incidentally, look at my signature and swap 'dumb' with 'stupid' ;)
As there is no such thing as a stupid question, please use the title of the post to give a heading for the contents. We get too many titles that say "A quick question", "A C# question" "A newbie question etc"... People on here don't really care, they are more interested in the actual question!

Anyway, I'll be interested to see other people's follow up posts on this one.

rliq
January 12th, 2010, 09:25 PM
This programme displays "What the?" on the console, maybe we can use this as a starting example:

using System;

namespace DoubleTest
{
class Program
{
static void Main(string[] args)
{
Double a = (2e31);
Double b = a - 1.0;

if (a.Equals(b))
Console.WriteLine("What the?");
}
}
}

and this one displays "What the?" on the console for ever and ever...

using System;

namespace DoubleTest
{
class Program
{
static void Main(string[] args)
{
Double a = (2e31);

for (Double b = a; b > 0; b--)
if (a.Equals(b))
Console.WriteLine("What the?");
}
}
}

ThermoSight
January 13th, 2010, 06:45 PM
Hi Rliq.

My apologies Sir, or Madam, as the case may be. My delay in responding was not a result of inattention but rather, another minor emergency. I finally got a few minutes to revisit this question (and try your code), and while I was doing that, it suddenly hit me ... I had one of those all-too-frequent, head-slapping revelations. I had screwed up yet again!

I regret to inform one and all of my ignorance. Certainly, I knew what I MEANT when I said "2e31", but my intent, and the meaning of "2e31", appear to be two completely different things ......

What I had intended was 2 raised to the 31st power ... 0x80 00 00 00, but I believe the meaning of the term is 2 * 10 raised to the 31st power.


When that thought occurred to me, I modified the code as follows
Double a = (2e4);
Double b = 2e4 - 1;

MessageBox.Show("a = " + a.ToString() + ", b = " + b.ToString());


and, YUP, it's been confirmed once again ... I was and remain, a dunce. 'a' is 20,000 while 'b' is 19,999.

I apologize to one and all for wasting your time while this old man struggles with the vagaries of age and computer basics.

Best wishes.

bill