classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

small patches for BigInteger, BigDecimal, Double


From: J. Russell Smyth
Subject: small patches for BigInteger, BigDecimal, Double
Date: Wed, 25 Apr 2001 00:58:12 -0700

I ran acrossed a few small problems with BigInteger, BigDecimal and
Double, and gnu.java.io.decoder.DecoderEightBitLookup which I have
corrected. Included here
are the changes. These will need to be applied by hand!

java.math.BigInteger - the incrementing of the index happened at the
wrong place causing index out of bounds problems

   static String forEachDigit(String str, int radix) {
     char buf[] = new char[str.length()];
     int i = 0;
     if (str.charAt(0) == '-')
       buf[i++] = '-';

-    while (i < buf.length)
-      if ((buf[i++] =
+ for(;i<buf.length;i++)
+      if ((buf[i] =
            Character.forDigit(Character.digit(str.charAt(i), radix),
radix))
    == '\u0000')
  throw new NumberFormatException(str + " not valid in radix " + radix);


     return new String(buf);
   }


java.math.BigDecimal - string ctor did not understand exponents

   public BigDecimal (String num) throws NumberFormatException
   {
   String exponent;
   int scaleAdj = 0;
   int epos = num.indexOf('E');
   if(epos == -1){
    epos = num.indexOf('e');
   }
   if(epos != -1){
    if(num.charAt(epos+1)=='+'){
     // Skip '+' in exponent
     exponent = num.substring(epos+2);
    }else{
     exponent = num.substring(epos+1);
    }
    scaleAdj = Integer.parseInt(exponent);
    // Strip exponent
    num = num.substring(0,epos);
   }
   int point = num.indexOf('.');
   scale = num.length() - (point == -1 ? num.length () : point + 1) -
scaleAdj;
  StringBuffer val = new StringBuffer(point == -1 ? num : num.substring
(0, point) + num.substring (point + 1));
  // correct for negative scale as per BigDecimal javadoc
  for(;scale<0;scale++){
    val.append("0");
   }
      this.intVal = new BigInteger (val.toString());
   }

gnu/java/io/decode/DecoderEightBitLookup.javav - convertToChar wouldnt
work with ascii values above 128 due to byte-char conversion

public char[]
convertToChars(byte[] buf, int buf_offset, int len, char[] cbuf,
               int cbuf_offset)
{

  for (int i = 0; i < len; i++){
-   cbuf[cbuf_offset + i] = lookup_table[buf[buf_offset + i]];
+   cbuf[cbuf_offset + i]
=lookup_table[buf[buf_offset+i]<0?256+buf[buf_offset+i]:buf[buf_offset+i]];

  }

  return(cbuf);
}

java.lang.Double - native code toString method rounded much to much (ie,
to nearest multiple of 10!)

in native/java.lang/Double.c, Java_java_lang_Double_toString the sprintf
line needs to be changed
(includes a fix mentioned by John Luner earlier)
from:
    sprintf((char*)&buf, "%G", d);
to:
    sprintf(buf, "%.17G", d);





reply via email to

[Prev in Thread] Current Thread [Next in Thread]