Re: [vox-tech] detecting overflows
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [vox-tech] detecting overflows
- Subject: Re: [vox-tech] detecting overflows
- From: Micah Cowan <miMAPScah@cowanbox.com>
- Date: Mon, 12 Mar 2001 10:15:19 -0800
- References: 20010312100837.A28062@dirac.org
On Mon, Mar 12, 2001 at 10:08:37AM -0800, Peter Jay Salzman wrote:
> dear all,
>
> i have code which looks like:
>
> long double dr;
> long double dt;
> long double rend;
> long double ratio;
> long int N;
> long unsigned int max_tstep;
>
> ratio = 2.0L;
> N = 800;
> dr = rend / ((long double)N - 1.0L);
> dt = ratio*pow(dr, 2.0L);
>
> max_tstep = (long unsigned int) ceil(endtime / dt);
>
> sometimes ceil(endtime / dt) is very large, like 2.10944e+85. this is too
> large even for a long long int. max_tstep overflows.
>
> sometimes i don't catch the overflow, and my program can run for a long
> time, producing garbage.
>
> is there some kind of flag that i can check that gets raised when a
> calculation overflows? i have many such calculations (which are performed
> once at the beginning, so i'm not worried about performance penalty).
>
> i guess i can take a look at limit.h to and compare the maximum long
> unsigned int to the value of endtime / dt, but i'd prefer a different
> solution if one exists. surely there's got to be SOME way of detecting this
> kind of error?
>
> i care not for portability. it's running on a bunch of pentium II, III and
> athlon class machines and nothing else. gcc extensions are welcome
> (although i took a look and didn't see anything immediately helpful).
>
> the problem is fairly pernicious. normally i would rescale variables, but
> for this particular problem, a rescaling simply shifts the problem from one
> variable to another.
>
> thanks!
> pete
>
> --
> "Coffee... I've conquered the Borg on coffee!" p@dirac.org
> -- Kathryn Janeway on the virtues of coffee www.dirac.org/p
Must you absolutely place the value into an int?
I might suggest that you check the floating point value against
LONG_MAX *before* the assignment, which is an easy way to catch this.
You might also look at the Arithmetic Operations section of the libc
manual (texinfo), but I don't think that you can find a function or
something to dectect overflow for integer assignments - you /can/ for
operations which involve only floating point types.
HTH,
Micah
|