Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpl-opus!walker From: walker@hpl-opus.HP.COM (Rick Walker) Newsgroups: alt.sources.wanted Subject: Re: correction to spline code Message-ID: <71850001@hpl-opus.HP.COM> Date: 20 Aug 90 17:10:27 GMT References: <11054@accuvax.nwu.edu> Organization: HP Labs, High Speed Electronics Dept., Palo Alto, CA Lines: 46 / hpl-opus:alt.sources.wanted / sandell@ferret.ils.nwu.edu / 10:10 am Aug 19, 1990 / From: Greg Sandell > > Okay, here's the spline code from Numerical Recipes. In the original > > code, all arrays began at subscript zero, not one. (I kid you not.) > > That's wrong. I meant the exact opposite. The code in "Numerical > Recipes in C" treats all arrays as though data begins in array > subscript one...obviously an artifact of the translation from > FORTRAN. > > > I've fixed it so it works right with normal C conventions. > > This meant that I have fixed the code so that all arrays begin > at subscript zero. > -------------------------- The authors of "Numerical Recipes" actually made some effort to make their vector allocator work with arbitrary ranges on the subscripts: float *vector(nl,nh) int nl,nh; /* allocates a float vector with range [nl..nh] */ { float *v; v=(float *)malloc((unsigned) (nh-nl+1)*sizeof(float)); if(!v) nrerror("allocation failure in vector()"); return v-nl; } The allocator returns a pointer offset by the value of nl, the lower bound on the index. This means that when the index nl is used, the 0th element will actally be accessed. This approach allows the code writer to use any convention that is best suited to the problem at hand. Sometimes that requires an index [0..n-1], while at other times [1.. n] is more convenient and easier to understand. In some instances it might be convenient to have a vector that is addressed from [100..109]. All in all, I think it is a clever and useful trick. The code is definitely not in need of "fixing". (IMHO). Rick Walker