From bug-request at octave dot org Thu Jan 27 10:38:21 2005 Subject: Re: dec2base From: "John W. Eaton" To: Paul Kienzle Cc: Yassine Boujelben , Andre Girard , bug-octave@bevo.che.wisc.edu Date: Thu, 27 Jan 2005 11:41:46 -0500 On 4-Jan-2005, Paul Kienzle wrote: | On Jan 3, 2005, at 3:58 PM, Yassine Boujelben wrote: | | > I found the following bug in the dec2base function: | > dec2base(1000,10)=000 | | Try the attached patch. | | Paul Kienzle | * strings/dec2base.m: Use integer arithmetic to count number of digits. | Index: dec2base.m | =================================================================== | RCS file: /cvs/octave/scripts/strings/dec2base.m,v | retrieving revision 1.4 | diff -c -p -r1.4 dec2base.m | *** a/dec2base.m 29 Aug 2003 17:09:38 -0000 1.4 | --- b/dec2base.m 5 Jan 2005 04:44:29 -0000 | *************** function retval = dec2base (n, base, len | *** 74,80 **** | endif | | ## determine number of digits required to handle all numbers | ! max_len = floor (log (max (max (n), 1)) ./ log (base)) + 1; | | if (nargin == 3) | max_len = max (max_len, len); | --- 74,82 ---- | endif | | ## determine number of digits required to handle all numbers | ! max_n = max(max(n),1); | ! max_len = ceil(log(max_n)/log(base)); | ! if (base^max_len <= max_n) max_len++; endif | | if (nargin == 3) | max_len = max (max_len, len); | *************** function retval = dec2base (n, base, len | *** 89,91 **** | --- 91,119 ---- | retval = reshape (symbols (digits+1), size (digits)); | | endfunction | + | + %!test | + %! s0=''; | + %! for n=1:13 | + %! for b=2:16 | + %! pp=dec2base(b^n+1,b); | + %! assert(dec2base(b^n,b),['1',s0,'0']); | + %! assert(dec2base(b^n+1,b),['1',s0,'1']); | + %! end | + %! s0=[s0,'0']; | + %! end | + | + %!test | + %! digits='0123456789ABCDEF'; | + %! for n=1:13 | + %! for b=2:16 | + %! pm=dec2base(b^n-1,b); | + %! assert(length(pm),n); | + %! assert(all(pm==digits(b))); | + %! end | + %! end | + | + %!test | + %! for b=2:16 | + %! assert(dec2base(0,b),'0'); | + %! end I applied this change. Thanks, jwe ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------