[GRASS5] r.contour crashes if step=0.5

Roger Bivand rsb at reclus.nhh.no
Sat Jan 27 08:26:32 EST 2001


On Sat, 27 Jan 2001, Markus Neteler wrote:

> Roger,
> 
> On Sat, Jan 27, 2001 at 01:38:02PM +0100, Roger Bivand wrote:
> > On Sat, 27 Jan 2001, Markus Neteler wrote:
> > > On Sat, Jan 27, 2001 at 12:48:44PM +0100, Roger Bivand wrote:
> > > > On Sat, 27 Jan 2001, Markus Neteler wrote:
> > > > 
> > > > > Hi developers,
> > > > > 
> > > > > r.contour crashes if step=0.5... Floating point exception.
> > > > > I actually cannot see the problem. The offending line is a macro:
> > > > > 
> > > > >   dmax = (max->answer) ? atof (max->answer) : zmax - (int)zmax%(int)dstep;
> > > > > 
> > > > > dstep contains 0.5, zmax is determined my the program zmax=7.1231 here).
> > > > 
> > > > It's the (int) cast on dstep, which becomes integer 0, and which gives a
> > > > NaN when used for division. For this code to work, dstep must be >= 1. It
> > > > looks a bit cludgy too.
> > > 
> It seems that C behaves differently than "R".
> The compiler doesn't accept modulo with doubles:
> 
> gcc -g -O2   -I/home/neteler/ggg/src/include  -c main.c -o
> OBJ.i686-pc-linux-gnu/main.o
> main.c: In function 'getlevels':
> main.c:233: invalid operands to binary %
> main.c:234: invalid operands to binary %
> main.c:235: invalid operands to binary %
> 
> You are right, if I add max= and min= the problem of course disappears.
> Maybe we can get rid of this macro? To be honest, I just don't get
> the idea of it. Do we need it (like that)?
> 
I think we need the fmod(x,y) function (K&R2: floating point remainder of
x/y, same sign as x; y=0 needs to be trapped, p. 251 in my edition). So:

dmax = (max->answer) ? atof (max->answer) : zmax - (int)zmax%(int)dstep;

could be

dmax = (max->answer) ? atof (max->answer) : dstep == 0 ?
        G_error(***) : zmax - fmod(zmax, dstep);

though I haven't tried it. 

Roger

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no
and: Department of Geography and Regional Development, University of
Gdansk, al. Mar. J. Pilsudskiego 46, PL-81 378 Gdynia, Poland.


---------------------------------------- 
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo at geog.uni-hannover.de with
subject 'unsubscribe grass5'



More information about the grass-dev mailing list