Arithmetic Overflow

  • Thread starter Thread starter Paul Cheetham
  • Start date Start date
P

Paul Cheetham

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");
}
 
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,

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top