473,653 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)_lineSta rtTime.Ticks;
JobStart = (ulong)newJob.S tartTime.Ticks;

TicksPerPix = (ulong)time.Tic ks / (ulong)Width;

try
{ // The next line generates an Arithmetic Overflow!
JobLeft = ((ulong)(JobSta rt - LineStart) / (ulong)TicksPer Pix);
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "Exception" );
}
Mar 6 '06 #1
3 3389
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.p ipex.com> wrote in message
news:u1******** ******@TK2MSFTN GP15.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)_lineSta rtTime.Ticks;
JobStart = (ulong)newJob.S tartTime.Ticks;

TicksPerPix = (ulong)time.Tic ks / (ulong)Width;

try
{ // The next line generates an Arithmetic Overflow!
JobLeft = ((ulong)(JobSta rt - LineStart) / (ulong)TicksPer Pix);
}
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.p ipex.com> wrote in message
news:u1******** ******@TK2MSFTN GP15.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)_lineSta rtTime.Ticks;
JobStart = (ulong)newJob.S tartTime.Ticks;

TicksPerPix = (ulong)time.Tic ks / (ulong)Width;

try
{ // The next line generates an Arithmetic Overflow!
JobLeft = ((ulong)(JobSta rt - LineStart) / (ulong)TicksPer Pix);
}
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
2742
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 program. #include <iostream> using namespace std;
5
3844
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 an enum. I prefer
10
2318
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 int a = 65535, b = 10; is a + b defined? And a * b? and b - a?) bye
16
5117
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
4
3596
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 creation process it receives the Overflow or Underflow in arithmetic operation error. I have traced it down to the section of the for where its registering the components... this.Controls.Add(this.cmd_exit); it doesn't matter which control...
4
4138
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 the arithmetic operation' when I attempt to instantiate my first form, as so: Dim frm As New frmMyForm() Based on some things I saw here, I thought it might be that this workstation didn't have a MS Sans Seriff font on it (since that is the...
9
1842
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
6621
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 unhandled exception has occurred in a component in your application. Click continue and application will ignore this error and attempt to continue.
10
4319
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 64-bit machines? Example: uint32_t x = SOME_LARGE_NO; uint64_t y; ....
0
8370
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8811
Oralloy
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...
1
8470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8590
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 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...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
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();...
1
2707
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
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
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...

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.