From bug-request at octave dot org Thu Jan 27 10:42:20 2005 Subject: Re: dec2base bug? From: David Bateman To: Andre Girard Cc: bug-octave at bevo dot che dot wisc dot edu Date: Thu, 27 Jan 2005 17:43:36 +0100 This is a multi-part message in MIME format. --------------080707070305070707030908 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit David Bateman wrote: > Andre Girard wrote: > >> octave:18> version >> ans = 2.1.57 >> octave:19> for i=1:9 >> > dec2base(10^i,10) >> > end >> ans = 10 >> ans = 100 >> ans = 000 >> ans = 10000 >> ans = 100000 >> ans = 000000 >> ans = 10000000 >> ans = 100000000 >> ans = 000000000 > > > > This is a rounding error in the calculation of the number of digits in > the string. However, its quite complicated to see how to treat this > problem directly as consider cases like dec2base(2^53-1,2) since due > to IEEE754 rounding errors log(2^53-1) is exactly log(2^53). In fact > the only way I see of treating this is to allow that there might be an > extra digit, and then check if it was used and remove it at the end... > > The following patch does that.... Anyone have a better solution > > D. > Opps, I forgot a ";" in my patch.. Use the attached version instead D. -- David Bateman David dot Bateman at motorola dot com Motorola CRM +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax) 91193 Gif-Sur-Yvette FRANCE The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary --------------080707070305070707030908 Content-Type: text/plain; name="patch.dec2base-20050127" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="patch.dec2base-20050127" *** dec2base.m.orig 2005-01-27 16:57:36.640980791 +0100 --- dec2base.m 2005-01-27 17:42:11.609995203 +0100 *************** *** 73,80 **** error ("dec2base: base must be between 2 and 36 or a string of symbols"); 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); --- 73,81 ---- error ("dec2base: base must be between 2 and 36 or a string of symbols"); endif ! ## determine number of digits required to handle all numbers, can overflow ! ## by 1 digit ! max_len = round (log (max (max (n), 1)) ./ log (base)) + 1; if (nargin == 3) max_len = max (max_len, len); *************** *** 88,91 **** --- 89,97 ---- ## convert digits to symbols retval = reshape (symbols (digits+1), size (digits)); + ## Check if the first element is the zero symbol + if (all (retval(:,1) == symbols(1))) + retval = retval(:,2:end); + endif + endfunction --------------080707070305070707030908-- ------------------------------------------------------------- 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 -------------------------------------------------------------