From bug-request at octave dot org Tue Jan 4 22:47:12 2005 Subject: Re: dec2base From: Paul Kienzle To: Yassine Boujelben Cc: bug-octave at bevo dot che dot wisc dot edu Date: Tue, 4 Jan 2005 23:49:45 -0500 --Apple-Mail-4--108149933 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed 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. --Apple-Mail-4--108149933 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0644; name="dec2base.patch" Content-Disposition: attachment; filename=dec2base.patch 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 --Apple-Mail-4--108149933 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-4--108149933-- ------------------------------------------------------------- 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 -------------------------------------------------------------