473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

long, not too long

In Visual Studio 2003, when i debug a C# program, I found:

300L * 10000000l -1294967296 int
210L * 10000000L 2100000000 int
How come? a long type can not handle 300L * 10000000l, the result is too
big for long type?

Thanks!
Ryan
Mar 17 '06 #1
11 1878
Ryan Liu <ad********@online.sh.cn> wrote:
In Visual Studio 2003, when i debug a C# program, I found:

300L * 10000000l -1294967296 int
210L * 10000000L 2100000000 int
How come? a long type can not handle 300L * 10000000l, the result is too
big for long type?


It's a debugger issue, that's all. It works fine in normal code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 17 '06 #2
I guess not quite so. I found this problem when my code does not work, then
I begin to debug and find this issue.

Regards,
"Jon Skeet [C# MVP]" <sk***@pobox.com> ????
news:MP************************@msnews.microsoft.c om...
Ryan Liu <ad********@online.sh.cn> wrote:
In Visual Studio 2003, when i debug a C# program, I found:

300L * 10000000l -1294967296 int
210L * 10000000L 2100000000 int
How come? a long type can not handle 300L * 10000000l, the result is too big for long type?


It's a debugger issue, that's all. It works fine in normal code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 18 '06 #3
"Ryan Liu" <ad********@online.sh.cn> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl...
I guess not quite so. I found this problem when my code does not work, then
I begin to debug and find this issue.
Are you by any chance stuffing it into an int instead of a long?
Why don't you post the offending code.

Bill


"Jon Skeet [C# MVP]" <sk***@pobox.com> ????
news:MP************************@msnews.microsoft.c om...
Ryan Liu <ad********@online.sh.cn> wrote:
> In Visual Studio 2003, when i debug a C# program, I found:
>
> 300L * 10000000l -1294967296 int
> 210L * 10000000L 2100000000 int
>
>
> How come? a long type can not handle 300L * 10000000l, the result is too > big for long type?


It's a debugger issue, that's all. It works fine in normal code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Mar 18 '06 #4
Here is the code. I did make sure are datas are used long type

All it does is scan the ArrayList which is one of the values of a Hashtable.
If it find right AppMessage object in this arraylist, it returns it.
Otherwise it keep scan until time out.

The code makes trouble is the last line -- to check if time out part:

do
{ ......
}
while (!stopReceivingThread && DateTime.Now.Ticks - startTime <=
timeoutSecondsL * 10000000L);

I found it returns immediately and tells me it is time out if I put
timeoutSeconds larger then 210.
So in my code, I restrict the time out less than 210 seconds.

Thanks,
Ryan
/// <summary>
/// send a message to AppServer and wait for reply
/// </summary>
/// <param name="appMsg"></param>
/// <returns>the message returned from server</returns>
public AppMessage WaitReply(int cmdType, bool doEvents, int
timeoutSeconds )
{

long timeoutSecondsL = (long)timeoutSeconds;
if(timeoutSecondsL > 210) timeoutSecondsL = 210L; //when it is 220 ,
*10^7 will get negative value

long startTime = DateTime.Now.Ticks; //100-nanosecond

do
{
//waitedResponseMsgs is a hashtable, its key is cmdType,value is an
ArrayList

object thisTypeMsgs = this.waitedResponseMsgs[cmdType];
if(thisTypeMsgs != null && ((ArrayList)thisTypeMsgs).Count > 0)
{

foreach (AppMessage rm in ((ArrayList)thisTypeMsgs))
{
if( ( rm.CommandType == cmdType) && //check if receiving thread get
the reply for this request
(!rm.IsRequest ) )

{
((ArrayList)thisTypeMsgs).Remove(rm);
return rm; //once received reply, break loop and return
}
}

}

if(doEvents) Application.DoEvents();
Thread.Sleep(500); // wait some miliseconds

}

while (!stopReceivingThread && DateTime.Now.Ticks - startTime <=
timeoutSecondsL * 10000000L);
throw new Exception("Time out");

}
"Bill Butler" <qw****@asdf.com> дÈëÓʼþ news:CYKSf.38$yo1.35@trndny09...
"Ryan Liu" <ad********@online.sh.cn> wrote in message

news:%2***************@TK2MSFTNGP12.phx.gbl...
I guess not quite so. I found this problem when my code does not work, then I begin to debug and find this issue.


Are you by any chance stuffing it into an int instead of a long?
Why don't you post the offending code.

Bill


"Jon Skeet [C# MVP]" <sk***@pobox.com> ????
news:MP************************@msnews.microsoft.c om...
Ryan Liu <ad********@online.sh.cn> wrote:
> In Visual Studio 2003, when i debug a C# program, I found:
>
> 300L * 10000000l -1294967296 int
> 210L * 10000000L 2100000000 int
>
>
> How come? a long type can not handle 300L * 10000000l, the result is

too
> big for long type?

It's a debugger issue, that's all. It works fine in normal code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Mar 18 '06 #5
"Ryan Liu" <ad********@online.sh.cn> wrote in message news:O0**************@TK2MSFTNGP11.phx.gbl...
Here is the code. I did make sure are datas are used long type

All it does is scan the ArrayList which is one of the values of a Hashtable.
If it find right AppMessage object in this arraylist, it returns it.
Otherwise it keep scan until time out.

The code makes trouble is the last line -- to check if time out part:

do
{ ......
}
while (!stopReceivingThread && DateTime.Now.Ticks - startTime <=
timeoutSecondsL * 10000000L);

I found it returns immediately and tells me it is time out if I put
timeoutSeconds larger then 210.
So in my code, I restrict the time out less than 210 seconds.
First:
Does it return or does it throw an exception?

clipping your method down to a minimum I go the following

public void WaitReplyGood(int timeoutSeconds )
{
bool stopReceivingThread = false;
Console.WriteLine("WaitReplyGood({0})",timeoutSeco nds);
long startTime = DateTime.Now.Ticks;
do
{
Thread.Sleep(500);
Console.WriteLine(DateTime.Now);
}
while (!stopReceivingThread && DateTime.Now.Ticks - startTime <= timeoutSeconds * 10000000L);
return;
}

This does not exhibit the behavior that you described (I even used an int for timeoutSeconds since
that makes no difference).
If you replace the 10000000L by simply 10000000 you do get the behavior that you described.
If stopReceivingThread is true you get that behavior as well.

See if you can comment out more of your code and still make the error occur.

Keep us posted

Bill

/// <summary>
/// send a message to AppServer and wait for reply
/// </summary>
/// <param name="appMsg"></param>
/// <returns>the message returned from server</returns>
public AppMessage WaitReply(int cmdType, bool doEvents, int
timeoutSeconds )
{

long timeoutSecondsL = (long)timeoutSeconds;
if(timeoutSecondsL > 210) timeoutSecondsL = 210L; //when it is 220 ,
*10^7 will get negative value

long startTime = DateTime.Now.Ticks; //100-nanosecond

do
{
//waitedResponseMsgs is a hashtable, its key is cmdType,value is an
ArrayList

object thisTypeMsgs = this.waitedResponseMsgs[cmdType];
if(thisTypeMsgs != null && ((ArrayList)thisTypeMsgs).Count > 0)
{

foreach (AppMessage rm in ((ArrayList)thisTypeMsgs))
{
if( ( rm.CommandType == cmdType) && //check if receiving thread get
the reply for this request
(!rm.IsRequest ) )

{
((ArrayList)thisTypeMsgs).Remove(rm);
return rm; //once received reply, break loop and return
}
}

}

if(doEvents) Application.DoEvents();
Thread.Sleep(500); // wait some miliseconds

}

while (!stopReceivingThread && DateTime.Now.Ticks - startTime <=
timeoutSecondsL * 10000000L);
throw new Exception("Time out");

}
"Bill Butler" <qw****@asdf.com> дÈëÓʼþ news:CYKSf.38$yo1.35@trndny09...
"Ryan Liu" <ad********@online.sh.cn> wrote in message

news:%2***************@TK2MSFTNGP12.phx.gbl...
>I guess not quite so. I found this problem when my code does not work, then > I begin to debug and find this issue.


Are you by any chance stuffing it into an int instead of a long?
Why don't you post the offending code.

Bill

>
> "Jon Skeet [C# MVP]" <sk***@pobox.com> ????
> news:MP************************@msnews.microsoft.c om...
>> Ryan Liu <ad********@online.sh.cn> wrote:
>> > In Visual Studio 2003, when i debug a C# program, I found:
>> >
>> > 300L * 10000000l -1294967296 int
>> > 210L * 10000000L 2100000000 int
>> >
>> >
>> > How come? a long type can not handle 300L * 10000000l, the result is
> too
>> > big for long type?
>>
>> It's a debugger issue, that's all. It works fine in normal code.
>>
>> --
>> Jon Skeet - <sk***@pobox.com>
>> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
>> If replying to the group, please do not mail me too
>
>



Mar 18 '06 #6
Ryan Liu <ad********@online.sh.cn> wrote:
I guess not quite so. I found this problem when my code does not work, then
I begin to debug and find this issue.


Well, 300L * 10000000L works absolutely fine in code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 18 '06 #7
"Ryan Liu" <ad********@online.sh.cn> wrote in message
news:O0**************@TK2MSFTNGP11.phx.gbl...
Here is the code. I did make sure are datas are used long type

All it does is scan the ArrayList which is one of the values of a
Hashtable.
If it find right AppMessage object in this arraylist, it returns it.
Otherwise it keep scan until time out.

The code makes trouble is the last line -- to check if time out part:

do
{ ......
}
while (!stopReceivingThread && DateTime.Now.Ticks - startTime <=
timeoutSecondsL * 10000000L);

I found it returns immediately and tells me it is time out if I put
timeoutSeconds larger then 210.
So in my code, I restrict the time out less than 210 seconds.

Thanks,
Ryan
/// <summary>
/// send a message to AppServer and wait for reply
/// </summary>
/// <param name="appMsg"></param>
/// <returns>the message returned from server</returns>
public AppMessage WaitReply(int cmdType, bool doEvents, int
timeoutSeconds )
{

long timeoutSecondsL = (long)timeoutSeconds;
if(timeoutSecondsL > 210) timeoutSecondsL = 210L; //when it is 220 ,
*10^7 will get negative value

long startTime = DateTime.Now.Ticks; //100-nanosecond

do
{
//waitedResponseMsgs is a hashtable, its key is cmdType,value is an
ArrayList

object thisTypeMsgs = this.waitedResponseMsgs[cmdType];
if(thisTypeMsgs != null && ((ArrayList)thisTypeMsgs).Count > 0)
{

foreach (AppMessage rm in ((ArrayList)thisTypeMsgs))
{
if( ( rm.CommandType == cmdType) && //check if receiving thread get
the reply for this request
(!rm.IsRequest ) )

{
((ArrayList)thisTypeMsgs).Remove(rm);
return rm; //once received reply, break loop and return
}
}

}

if(doEvents) Application.DoEvents();
Thread.Sleep(500); // wait some miliseconds

}

while (!stopReceivingThread && DateTime.Now.Ticks - startTime <=
timeoutSecondsL * 10000000L);
If you do this, it'll be much more clear, and you'll avoid whatever int/long
issue is facing you:

DateTime dateTimeEnd = DateTime.Now + new TimeSpan( 0, 0,
timeoutSecondsL);
do
{ }
while (... &&(DateTime.Now <= DateTime.End)
m


throw new Exception("Time out");

}
"Bill Butler" <qw****@asdf.com> дÈëÓʼþ news:CYKSf.38$yo1.35@trndny09...
"Ryan Liu" <ad********@online.sh.cn> wrote in message

news:%2***************@TK2MSFTNGP12.phx.gbl...
>I guess not quite so. I found this problem when my code does not work, then > I begin to debug and find this issue.


Are you by any chance stuffing it into an int instead of a long?
Why don't you post the offending code.

Bill

>
> "Jon Skeet [C# MVP]" <sk***@pobox.com> ????
> news:MP************************@msnews.microsoft.c om...
>> Ryan Liu <ad********@online.sh.cn> wrote:
>> > In Visual Studio 2003, when i debug a C# program, I found:
>> >
>> > 300L * 10000000l -1294967296 int
>> > 210L * 10000000L 2100000000 int
>> >
>> >
>> > How come? a long type can not handle 300L * 10000000l, the result
>> > is
> too
>> > big for long type?
>>
>> It's a debugger issue, that's all. It works fine in normal code.
>>
>> --
>> Jon Skeet - <sk***@pobox.com>
>> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
>> If replying to the group, please do not mail me too
>
>



Mar 18 '06 #8
Mike <vi********@yahoo.com> wrote:
If you do this, it'll be much more clear, and you'll avoid whatever int/long
issue is facing you:

DateTime dateTimeEnd = DateTime.Now + new TimeSpan( 0, 0,
timeoutSecondsL);
do
{ }
while (... &&(DateTime.Now <= DateTime.End)


Or to make it even clearer (and avoid problems with knowing how many 0s
to put in):

DateTime end = DateTime.Now.AddSeconds (timeoutSeconds);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 18 '06 #9
Ryan Liu wrote:
In Visual Studio 2003, when i debug a C# program, I found:

300L * 10000000l -1294967296 int
210L * 10000000L 2100000000 int
How come? a long type can not handle 300L * 10000000l, the result is too
big for long type?


It doesn't look like it *is* going into a long. It looks like the result (or
maybe an intermediate result) is being stuffed into an int, in which case (210 *
10000000 = 2100000000) would fit, whereas (300 * 10000000 = 3000000000) would be
an overflow (greater than 2147483647, which is the maximum positive integer).
But as has been noted - if you want a more definitive answer you should post a
short complete program . . . your snippet does not show the data type of
timeoutSecondsL, for example.

In any case recalculating (timeoutSecondsL * 10000000L) on each iteration is
kind of bad form - calculate it once before the loop, stuff the result into a
long, test against the long in your loop and likely your whole problem will go
away anyhow . . .

HTH,
-rick-
Mar 18 '06 #10
Re my previous post, here is a prediction which would give more evidence that
arithmetic overflow is at the root of your problem: your program will work if
the constant is 214L but will fail if it is 215L.

-rick-

Rick Lones wrote:
Ryan Liu wrote:
In Visual Studio 2003, when i debug a C# program, I found:

300L * 10000000l -1294967296 int
210L * 10000000L 2100000000 int
How come? a long type can not handle 300L * 10000000l, the result is too
big for long type?

It doesn't look like it *is* going into a long. It looks like the result
(or maybe an intermediate result) is being stuffed into an int, in which
case (210 * 10000000 = 2100000000) would fit, whereas (300 * 10000000 =
3000000000) would be an overflow (greater than 2147483647, which is the
maximum positive integer). But as has been noted - if you want a more
definitive answer you should post a short complete program . . . your
snippet does not show the data type of timeoutSecondsL, for example.

In any case recalculating (timeoutSecondsL * 10000000L) on each
iteration is kind of bad form - calculate it once before the loop, stuff
the result into a long, test against the long in your loop and likely
your whole problem will go away anyhow . . .

HTH,
-rick-

Mar 18 '06 #11
Thanks everyone and sorry for the delay, I was out !

You were right, and it was my fault. It was becase I didn't cast to long at
the first place.

I remember I casted to long last time, but when I test again today, it works
fine if I cast to long and problem occurs if I don't.

Thanks again!
Ryan

"Jon Skeet [C# MVP]" <sk***@pobox.com> ????
news:MP************************@msnews.microsoft.c om...
Ryan Liu <ad********@online.sh.cn> wrote:
I guess not quite so. I found this problem when my code does not work, then I begin to debug and find this issue.


Well, 300L * 10000000L works absolutely fine in code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 21 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tim Clacy | last post by:
How is a 64 bit type defined in strict C++? It seems C has support for 'long long' since C99, but not so for C++? Looking through one compiler vendor's standard library headers has clouded the...
7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
5
by: Mark Shelor | last post by:
Problem: find a portable way to determine whether a compiler supports the "long long" type of C99. I thought I had this one solved with the following code: #include <limits.h> #ifdef...
29
by: Richard A. Huebner | last post by:
Is the unsigned long long primitive data type supported in ANSI standard C? I've tried using it a couple of times in standard C, but to no avail. I'm using both MS VIsual C++ 6, as well as the...
9
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
21
by: Charles Sullivan | last post by:
I maintain/enhance some inherited FOSS software in C which has compiler options for quite a few different Unix-like operating systems, many of which I've never even heard of. It would be...
12
by: Ahmad Jalil Qarshi | last post by:
Hi, I have an integer value which is very long like 9987967441778573855. Now I want to convert it into equivalent Hex value. The result must be 8A9C63784361021F I have used...
2
by: PengYu.UT | last post by:
Hi, In python, triple quote (""") can be used to quote a paragraph (multiple lines). I'm wondering if there is any equivalent in C++. For the following code, I could write the long string in a...
10
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
15
by: Oliver Graeser | last post by:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte = 64 bit right? but when I try to assign a value with more than 32 bit, it fails. To illustrate: for (i=0; i<64; i++){...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.