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 11 1932
Ryan Liu <ad********@onl ine.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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.co m> ????
news:MP******** *************** *@msnews.micros oft.com... Ryan Liu <ad********@onl ine.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
"Ryan Liu" <ad********@onl ine.sh.cn> wrote in message news:%2******** *******@TK2MSFT NGP12.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.co m> ???? news:MP******** *************** *@msnews.micros oft.com... Ryan Liu <ad********@onl ine.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
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 (!stopReceiving Thread && DateTime.Now.Ti cks - 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)timeoutSe conds;
if(timeoutSecon dsL > 210) timeoutSecondsL = 210L; //when it is 220 ,
*10^7 will get negative value
long startTime = DateTime.Now.Ti cks; //100-nanosecond
do
{
//waitedResponseM sgs is a hashtable, its key is cmdType,value is an
ArrayList
object thisTypeMsgs = this.waitedResp onseMsgs[cmdType];
if(thisTypeMsgs != null && ((ArrayList)thi sTypeMsgs).Coun t > 0)
{
foreach (AppMessage rm in ((ArrayList)thi sTypeMsgs))
{
if( ( rm.CommandType == cmdType) && //check if receiving thread get
the reply for this request
(!rm.IsRequest ) )
{
((ArrayList)thi sTypeMsgs).Remo ve(rm);
return rm; //once received reply, break loop and return
}
}
}
if(doEvents) Application.DoE vents();
Thread.Sleep(50 0); // wait some miliseconds
}
while (!stopReceiving Thread && DateTime.Now.Ti cks - startTime <=
timeoutSecondsL * 10000000L);
throw new Exception("Time out");
}
"Bill Butler" <qw****@asdf.co m> дÈëÓʼþ news:CYKSf.38$y o1.35@trndny09. .. "Ryan Liu" <ad********@onl ine.sh.cn> wrote in message
news:%2******** *******@TK2MSFT NGP12.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.co m> ???? news:MP******** *************** *@msnews.micros oft.com... Ryan Liu <ad********@onl ine.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
"Ryan Liu" <ad********@onl ine.sh.cn> wrote in message news:O0******** ******@TK2MSFTN GP11.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 (!stopReceiving Thread && DateTime.Now.Ti cks - 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(i nt timeoutSeconds )
{
bool stopReceivingTh read = false;
Console.WriteLi ne("WaitReplyGo od({0})",timeou tSeconds);
long startTime = DateTime.Now.Ti cks;
do
{
Thread.Sleep(50 0);
Console.WriteLi ne(DateTime.Now );
}
while (!stopReceiving Thread && DateTime.Now.Ti cks - 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 stopReceivingTh read 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)timeoutSe conds; if(timeoutSecon dsL > 210) timeoutSecondsL = 210L; //when it is 220 , *10^7 will get negative value
long startTime = DateTime.Now.Ti cks; //100-nanosecond
do { //waitedResponseM sgs is a hashtable, its key is cmdType,value is an ArrayList
object thisTypeMsgs = this.waitedResp onseMsgs[cmdType]; if(thisTypeMsgs != null && ((ArrayList)thi sTypeMsgs).Coun t > 0) {
foreach (AppMessage rm in ((ArrayList)thi sTypeMsgs)) { if( ( rm.CommandType == cmdType) && //check if receiving thread get the reply for this request (!rm.IsRequest ) )
{ ((ArrayList)thi sTypeMsgs).Remo ve(rm); return rm; //once received reply, break loop and return } }
}
if(doEvents) Application.DoE vents(); Thread.Sleep(50 0); // wait some miliseconds
}
while (!stopReceiving Thread && DateTime.Now.Ti cks - startTime <= timeoutSecondsL * 10000000L);
throw new Exception("Time out");
} "Bill Butler" <qw****@asdf.co m> дÈëÓʼþ news:CYKSf.38$y o1.35@trndny09. .. "Ryan Liu" <ad********@onl ine.sh.cn> wrote in message news:%2******** *******@TK2MSFT NGP12.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.co m> ???? > news:MP******** *************** *@msnews.micros oft.com... >> Ryan Liu <ad********@onl ine.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.co m> >> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet >> If replying to the group, please do not mail me too > >
"Ryan Liu" <ad********@onl ine.sh.cn> wrote in message
news:O0******** ******@TK2MSFTN GP11.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 (!stopReceiving Thread && DateTime.Now.Ti cks - 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)timeoutSe conds; if(timeoutSecon dsL > 210) timeoutSecondsL = 210L; //when it is 220 , *10^7 will get negative value
long startTime = DateTime.Now.Ti cks; //100-nanosecond
do { //waitedResponseM sgs is a hashtable, its key is cmdType,value is an ArrayList
object thisTypeMsgs = this.waitedResp onseMsgs[cmdType]; if(thisTypeMsgs != null && ((ArrayList)thi sTypeMsgs).Coun t > 0) {
foreach (AppMessage rm in ((ArrayList)thi sTypeMsgs)) { if( ( rm.CommandType == cmdType) && //check if receiving thread get the reply for this request (!rm.IsRequest ) )
{ ((ArrayList)thi sTypeMsgs).Remo ve(rm); return rm; //once received reply, break loop and return } }
}
if(doEvents) Application.DoE vents(); Thread.Sleep(50 0); // wait some miliseconds
}
while (!stopReceiving Thread && DateTime.Now.Ti cks - 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.co m> дÈëÓʼþ news:CYKSf.38$y o1.35@trndny09. .. "Ryan Liu" <ad********@onl ine.sh.cn> wrote in message news:%2******** *******@TK2MSFT NGP12.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.co m> ???? > news:MP******** *************** *@msnews.micros oft.com... >> Ryan Liu <ad********@onl ine.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.co m> >> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet >> If replying to the group, please do not mail me too > >
Mike <vi********@yah oo.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.Ad dSeconds (timeoutSeconds );
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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 (timeoutSeconds L * 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- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 issue somewhat; it uses __int64 in some places (with a comment about
strict) but freely uses 'long long' in other places. In any case, these
standard library headers are supposed to be 'standard' w.r.t. C and C++
aren't they? How can a standard...
|
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 example, consider this
four-bit number:
1100
from this number I want to extract two numbers:
1000 and 100
had the four-bit number been 0101 I would want to extract 100 and 1.
How should I do this? I wish I had some code to post but I don't...
|
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 ULONG_LONG_MAX
|
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 gcc compiler that comes
with RedHat linux 9.
If not, what data type will yield the largest unsigned integer for me?
Thanks for your help,
|
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);
| |
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 convenient (and for some things possibly necessary)
to use long long integer and/or unsigned long long integer
variables.
How widely supported are these variable types? How long ago were
they introduced? I notice they are not mentioned in K&R #2.
|
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 sprintf(pHex,"%0X",9987967441778573855). But it only
returns 8
|
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 single line
with "\n" in the middle, or I could use multiple cout and endl. But I
just feel more readable if I can have the whole paragraph as a string.
Thanks,
|
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)
|
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++){
long long int k = 1<<i;
cout<<i<<"\t"<<k<<"\t"<<sizeof(k)<<"\n";
}
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |