You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by sb...@apache.org on 2001/06/12 03:56:14 UTC

cvs commit: xml-xalan/java/src/org/apache/xpath/objects XNumber.java

sboag       01/06/11 18:56:14

  Modified:    java/src/org/apache/xpath/objects Tag: DTM_EXP XNumber.java
  Log:
  Just going back to the older str() function (the one that doesn't
  use BigDecimal.  I believe personally that the one that uses BigDecimal
  is correct, but right now it will fail the conformance test, and we
  have to figure out the validation for this once and for all, including
  in regards to the datapower metrics.xsl test.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.8.2.5   +172 -172  xml-xalan/java/src/org/apache/xpath/objects/XNumber.java
  
  Index: XNumber.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xpath/objects/XNumber.java,v
  retrieving revision 1.8.2.4
  retrieving revision 1.8.2.5
  diff -u -r1.8.2.4 -r1.8.2.5
  --- XNumber.java	2001/06/11 20:17:00	1.8.2.4
  +++ XNumber.java	2001/06/12 01:56:14	1.8.2.5
  @@ -140,162 +140,13 @@
       return (Double.isNaN(m_val) || (m_val == 0.0)) ? false : true;
     }
   
  -  /**
  -   * Cast result object to a string.
  -   *
  -   * @return "NaN" if the number is NaN, Infinity or -Infinity if
  -   * the number is infinite or the string value of the number.
  -   */
  -  private static final int PRECISION = 16;
  -  public String str()
  -  {
  -
  -    if (Double.isNaN(m_val))
  -    {
  -      return "NaN";
  -    }
  -    else if (Double.isInfinite(m_val))
  -    {
  -      if (m_val > 0)
  -        return "Infinity";
  -      else
  -        return "-Infinity";
  -    }
  -
  -    long longVal = (long)m_val;
  -    if ((double)longVal == m_val)
  -      return Long.toString(longVal);
  -
  -
  -    String s = Double.toString(m_val);
  -    int len = s.length();
  -
  -    if (s.charAt(len - 2) == '.' && s.charAt(len - 1) == '0')
  -    {
  -      return s.substring(0, len - 2);
  -    }
  -
  -    int exp = 0;
  -    int e = s.indexOf('E');
  -    if (e != -1)
  -    {
  -      exp = Integer.parseInt(s.substring(e + 1));
  -      s = s.substring(0,e);
  -      len = e;
  -    }
  -
  -    // Calculate Significant Digits:
  -    // look from start of string for first digit
  -    // look from end for last digit
  -    // significant digits = end - start + (0 or 1 depending on decimal location)
  -
  -    int decimalPos = -1;
  -    int start = (s.charAt(0) == '-') ? 1 : 0;
  -    findStart: for( ; start < len; start++ )
  -    {
  -      switch (s.charAt(start))
  -      {
  -      case '0':
  -        break;
  -      case '.':
  -        decimalPos = start;
  -        break;
  -      default:
  -        break findStart;
  -      }
  -    }
  -    int end = s.length() - 1;
  -    findEnd: for( ; end > start; end-- )
  -    {
  -      switch (s.charAt(end))
  -      {
  -      case '0':
  -        break;
  -      case '.':
  -        decimalPos = end;
  -        break;
  -      default:
  -        break findEnd;
  -      }
  -    }
  -
  -    int sigDig = end - start;
  -
  -    // clarify decimal location if it has not yet been found
  -    if (decimalPos == -1)
  -      decimalPos = s.indexOf('.');
  -
  -    // if decimal is not between start and end, add one to sigDig
  -    if (decimalPos < start || decimalPos > end)
  -      ++sigDig;
  -
  -    // reduce significant digits to PRECISION if necessary
  -    if (sigDig > PRECISION)
  -    {
  -      // re-scale BigDecimal in order to get significant digits = PRECISION
  -      BigDecimal num = new BigDecimal(s);
  -      int newScale = num.scale() - (sigDig - PRECISION);
  -      if (newScale < 0)
  -        newScale = 0;
  -      s = num.setScale(newScale, BigDecimal.ROUND_HALF_UP).toString();
  -
  -      // remove trailing '0's; keep track of decimalPos
  -      int truncatePoint = s.length();
  -      while (s.charAt(--truncatePoint) == '0')
  -        ;
  -
  -      if (s.charAt(truncatePoint) == '.')
  -      {
  -        decimalPos = truncatePoint;
  -      }
  -      else
  -      {
  -        decimalPos = s.indexOf('.');
  -        truncatePoint += 1;
  -      }
  -
  -      s = s.substring(0, truncatePoint);
  -      len = s.length();
  -    }
  -
  -    // Account for exponent by adding zeros as needed 
  -    // and moving the decimal place
  -
  -    if (exp == 0)
  -       return s;
  -
  -    start = 0;
  -    String sign;
  -    if (s.charAt(0) == '-')
  -    {
  -      sign = "-";
  -      start++;
  -    }
  -    else
  -      sign = "";
  -
  -    String wholePart = s.substring(start, decimalPos);
  -    String decimalPart = s.substring(decimalPos + 1);
  -
  -    // get the number of digits right of the decimal
  -    int decimalLen = decimalPart.length();
  -
  -    if (exp >= decimalLen)
  -      return sign + wholePart + decimalPart + zeros(exp - decimalLen);
  -
  -    if (exp > 0)
  -      return sign + wholePart + decimalPart.substring(0, exp) + "."
  -             + decimalPart.substring(exp);
  -
  -    return sign + "0." + zeros(-1 - exp) + wholePart + decimalPart;
  -  }
  -
   //  /**
   //   * Cast result object to a string.
   //   *
   //   * @return "NaN" if the number is NaN, Infinity or -Infinity if
   //   * the number is infinite or the string value of the number.
   //   */
  +//  private static final int PRECISION = 16;
   //  public String str()
   //  {
   //
  @@ -310,52 +161,201 @@
   //      else
   //        return "-Infinity";
   //    }
  +//
  +//    long longVal = (long)m_val;
  +//    if ((double)longVal == m_val)
  +//      return Long.toString(longVal);
   //
  -//    double num = m_val;
  -//    String s = Double.toString(num);
  +//
  +//    String s = Double.toString(m_val);
   //    int len = s.length();
   //
   //    if (s.charAt(len - 2) == '.' && s.charAt(len - 1) == '0')
  +//    {
  +//      return s.substring(0, len - 2);
  +//    }
  +//
  +//    int exp = 0;
  +//    int e = s.indexOf('E');
  +//    if (e != -1)
   //    {
  -//      s = s.substring(0, len - 2);
  +//      exp = Integer.parseInt(s.substring(e + 1));
  +//      s = s.substring(0,e);
  +//      len = e;
  +//    }
  +//
  +//    // Calculate Significant Digits:
  +//    // look from start of string for first digit
  +//    // look from end for last digit
  +//    // significant digits = end - start + (0 or 1 depending on decimal location)
  +//
  +//    int decimalPos = -1;
  +//    int start = (s.charAt(0) == '-') ? 1 : 0;
  +//    findStart: for( ; start < len; start++ )
  +//    {
  +//      switch (s.charAt(start))
  +//      {
  +//      case '0':
  +//        break;
  +//      case '.':
  +//        decimalPos = start;
  +//        break;
  +//      default:
  +//        break findStart;
  +//      }
  +//    }
  +//    int end = s.length() - 1;
  +//    findEnd: for( ; end > start; end-- )
  +//    {
  +//      switch (s.charAt(end))
  +//      {
  +//      case '0':
  +//        break;
  +//      case '.':
  +//        decimalPos = end;
  +//        break;
  +//      default:
  +//        break findEnd;
  +//      }
  +//    }
   //
  -//      if (s.equals("-0"))
  -//        return "0";
  +//    int sigDig = end - start;
   //
  -//      return s;
  +//    // clarify decimal location if it has not yet been found
  +//    if (decimalPos == -1)
  +//      decimalPos = s.indexOf('.');
  +//
  +//    // if decimal is not between start and end, add one to sigDig
  +//    if (decimalPos < start || decimalPos > end)
  +//      ++sigDig;
  +//
  +//    // reduce significant digits to PRECISION if necessary
  +//    if (sigDig > PRECISION)
  +//    {
  +//      // re-scale BigDecimal in order to get significant digits = PRECISION
  +//      BigDecimal num = new BigDecimal(s);
  +//      int newScale = num.scale() - (sigDig - PRECISION);
  +//      if (newScale < 0)
  +//        newScale = 0;
  +//      s = num.setScale(newScale, BigDecimal.ROUND_HALF_UP).toString();
  +//
  +//      // remove trailing '0's; keep track of decimalPos
  +//      int truncatePoint = s.length();
  +//      while (s.charAt(--truncatePoint) == '0')
  +//        ;
  +//
  +//      if (s.charAt(truncatePoint) == '.')
  +//      {
  +//        decimalPos = truncatePoint;
  +//      }
  +//      else
  +//      {
  +//        decimalPos = s.indexOf('.');
  +//        truncatePoint += 1;
  +//      }
  +//
  +//      s = s.substring(0, truncatePoint);
  +//      len = s.length();
   //    }
   //
  -//    int e = s.indexOf('E');
  +//    // Account for exponent by adding zeros as needed 
  +//    // and moving the decimal place
   //
  -//    if (e < 0)
  -//      return s;
  +//    if (exp == 0)
  +//       return s;
   //
  -//    int exp = Integer.parseInt(s.substring(e + 1));
  +//    start = 0;
   //    String sign;
  -//
   //    if (s.charAt(0) == '-')
   //    {
   //      sign = "-";
  -//      s = s.substring(1);
  -//
  -//      --e;
  +//      start++;
   //    }
   //    else
   //      sign = "";
  +//
  +//    String wholePart = s.substring(start, decimalPos);
  +//    String decimalPart = s.substring(decimalPos + 1);
   //
  -//    int nDigits = e - 2;
  +//    // get the number of digits right of the decimal
  +//    int decimalLen = decimalPart.length();
   //
  -//    if (exp >= nDigits)
  -//      return sign + s.substring(0, 1) + s.substring(2, e)
  -//             + zeros(exp - nDigits);
  +//    if (exp >= decimalLen)
  +//      return sign + wholePart + decimalPart + zeros(exp - decimalLen);
   //
   //    if (exp > 0)
  -//      return sign + s.substring(0, 1) + s.substring(2, 2 + exp) + "."
  -//             + s.substring(2 + exp, e);
  +//      return sign + wholePart + decimalPart.substring(0, exp) + "."
  +//             + decimalPart.substring(exp);
   //
  -//    return sign + "0." + zeros(-1 - exp) + s.substring(0, 1)
  -//           + s.substring(2, e);
  +//    return sign + "0." + zeros(-1 - exp) + wholePart + decimalPart;
   //  }
  +
  +  /**
  +   * Cast result object to a string.
  +   *
  +   * @return "NaN" if the number is NaN, Infinity or -Infinity if
  +   * the number is infinite or the string value of the number.
  +   */
  +  public String str()
  +  {
  +
  +    if (Double.isNaN(m_val))
  +    {
  +      return "NaN";
  +    }
  +    else if (Double.isInfinite(m_val))
  +    {
  +      if (m_val > 0)
  +        return "Infinity";
  +      else
  +        return "-Infinity";
  +    }
  +
  +    double num = m_val;
  +    String s = Double.toString(num);
  +    int len = s.length();
  +
  +    if (s.charAt(len - 2) == '.' && s.charAt(len - 1) == '0')
  +    {
  +      s = s.substring(0, len - 2);
  +
  +      if (s.equals("-0"))
  +        return "0";
  +
  +      return s;
  +    }
  +
  +    int e = s.indexOf('E');
  +
  +    if (e < 0)
  +      return s;
  +
  +    int exp = Integer.parseInt(s.substring(e + 1));
  +    String sign;
  +
  +    if (s.charAt(0) == '-')
  +    {
  +      sign = "-";
  +      s = s.substring(1);
  +
  +      --e;
  +    }
  +    else
  +      sign = "";
  +
  +    int nDigits = e - 2;
  +
  +    if (exp >= nDigits)
  +      return sign + s.substring(0, 1) + s.substring(2, e)
  +             + zeros(exp - nDigits);
  +
  +    if (exp > 0)
  +      return sign + s.substring(0, 1) + s.substring(2, 2 + exp) + "."
  +             + s.substring(2 + exp, e);
  +
  +    return sign + "0." + zeros(-1 - exp) + s.substring(0, 1)
  +           + s.substring(2, e);
  +  }
   
   
     /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org