From bug-octave-request at bevo dot che dot wisc dot edu Thu Oct 5 12:28:51 1995 Subject: linspace() can generate an array that is one element short From: Marvin Vis To: bug-octave at bevo dot che dot wisc dot edu Date: Thu, 5 Oct 95 11:28:45 MDT I noticed that this bug has already been reported (2/13/95) , but there was no response/patch. I ran across this problem when using hist(), which calls linspace, when using a step size of only 2/30 (not even close to as small as eps). Below is the patch that I typically use when I run into these small round-off problems of creating a vector using a statement like x1:stepsize:x2 The patch I used on linspace is as follows: csh> diff linspace.m linspace.patched 50c50,55 < retval = x1:delta:x2; --- > overshoot = eps * npoints * max(abs(x1),abs(x2)); > if(overshoot>delta) > fprintf(stderr,... > "Warning: numerical errors in linspace due to small step size\n"); > fprintf(stderr,... > " length of vector returned may be different than %d\n",npoints); > overshoot = 0; > endif > retval = x1:delta:(x2+overshoot); This fix catches almost all cases, and the cases that it doesn't catch are problematic due to a small step size relative to the largest value of (x1,x2). The final value in the vector will be slightly larger than x2, but the purpose of linspace is to return a vector of length (specified) N, not simply hardlimit at x2. Perhaps we should re-write it so that we split the error at each end...(?) This could be a tougher problem, though, since the errors are not deterministic. M.