473,395 Members | 1,535 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,395 software developers and data experts.

Arithmetic Overflow


Hi,

I am developing a program that is doinbg time calculations using Ticks.
Below is a piece of code that is causing an arithmetic overflow
exception - the single line inside the try statement.

All the variables are defined as ulong, the difference between JobStart
and LineStart is approx 584 000 000 000, and TicksPerPix is approx. 909
000 000
Can anyone see what I can't after staring at it for hours?

Thankyou.
Paul

ulong TicksPerPix, LineStart, JobStart, JobLeft;
TimeSpan time = _lineEndTime - _lineStartTime;

LineStart = (ulong)_lineStartTime.Ticks;
JobStart = (ulong)newJob.StartTime.Ticks;

TicksPerPix = (ulong)time.Ticks / (ulong)Width;

try
{ // The next line generates an Arithmetic Overflow!
JobLeft = ((ulong)(JobStart - LineStart) / (ulong)TicksPerPix);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception");
}
Mar 6 '06 #1
3 3372
Paul Cheetham wrote:
I am developing a program that is doinbg time calculations using Ticks.
Below is a piece of code that is causing an arithmetic overflow
exception - the single line inside the try statement.


Could you provide a short but complete program that demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Mar 6 '06 #2
Bob
Hi Paul,
What happens if you break the calc up?
ulong temp=JobStart - LineStart;
JobLeft = temp / TickPerPix;
(Everything is declared ulong so you shouldn't need the casts in the calc.)
If you use constants of the expected values in the calc does it still error?
I tried with; jobStart 1008E9 , LineStart 504E9 TickPerpix 909E6 and it
executed OK.
regards
Bob
"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl...

Hi,

I am developing a program that is doinbg time calculations using Ticks.
Below is a piece of code that is causing an arithmetic overflow
exception - the single line inside the try statement.

All the variables are defined as ulong, the difference between JobStart
and LineStart is approx 584 000 000 000, and TicksPerPix is approx. 909
000 000
Can anyone see what I can't after staring at it for hours?

Thankyou.
Paul

ulong TicksPerPix, LineStart, JobStart, JobLeft;
TimeSpan time = _lineEndTime - _lineStartTime;

LineStart = (ulong)_lineStartTime.Ticks;
JobStart = (ulong)newJob.StartTime.Ticks;

TicksPerPix = (ulong)time.Ticks / (ulong)Width;

try
{ // The next line generates an Arithmetic Overflow!
JobLeft = ((ulong)(JobStart - LineStart) / (ulong)TicksPerPix);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception");
}

Mar 6 '06 #3
Paul,

I can't repro your problem. You should probably post a compilable sample
that demosntrates the problem.
--

Stoitcho Goutsev (100)

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl...

Hi,

I am developing a program that is doinbg time calculations using Ticks.
Below is a piece of code that is causing an arithmetic overflow
exception - the single line inside the try statement.

All the variables are defined as ulong, the difference between JobStart
and LineStart is approx 584 000 000 000, and TicksPerPix is approx. 909
000 000
Can anyone see what I can't after staring at it for hours?

Thankyou.
Paul

ulong TicksPerPix, LineStart, JobStart, JobLeft;
TimeSpan time = _lineEndTime - _lineStartTime;

LineStart = (ulong)_lineStartTime.Ticks;
JobStart = (ulong)newJob.StartTime.Ticks;

TicksPerPix = (ulong)time.Ticks / (ulong)Width;

try
{ // The next line generates an Arithmetic Overflow!
JobLeft = ((ulong)(JobStart - LineStart) / (ulong)TicksPerPix);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception");
}

Mar 7 '06 #4

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

Similar topics

3
by: Karthik | last post by:
Hi, I am writing this application that needs a lot of arithmetic calculations. I was wondering if C++ language specifies any way of detecting arithmetic overflows. Let us consider the following...
5
by: Fraser Ross | last post by:
void f(unsigned int const x) { }; int main(int argc, char* argv){ //unsigned short a=0xFFFF; enum { a=0xFFFF }; f(a*0x10000+0xFFFF); return 0; } I get an arithmetic overflow when using a as...
10
by: Massimiliano Alberti | last post by:
Are there rules on how should the C behave with the arithmetic operations? Are the overflows always ignored? And are they simply truncated? (if I have a 16-bits unsigned short ints unsigned short...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
4
by: glenn | last post by:
I have a COM Server I've written in C#. I have a client app I've written in Delphi that is calling the C# COM Server. However, one of the functions in the COM Server creates a form and during the...
4
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in...
9
by: Mike Aubury | last post by:
Is there any standard (or even non-standard) way to detect limit overflow in arithmetic in C ? eg. /* assuming 4 byte ints.. */ int a=2147483647; int b=2147483647; int c;
1
by: thebigsquid | last post by:
hi, its me again, so this software obviously has serious problems. now i'm getting this error message when i try to access any of it! any ideas much appreciated thanks the big squid An...
10
by: Why Tea | last post by:
I understood that the CPU registers determine the size of the machine. But what is the correct way to code an arithmetic operation to avoid wrap-around when the code is to be run on both 32-bit and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...

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.