src/share/classes/sun/misc/FloatingDecimal.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import sun.misc.FpUtils;
  29 import sun.misc.DoubleConsts;
  30 import sun.misc.FloatConsts;
  31 import java.util.regex.*;
  32 
  33 public class FloatingDecimal{
  34     boolean     isExceptional;
  35     boolean     isNegative;
  36     int         decExponent;
  37     char        digits[];
  38     int         nDigits;
  39     int         bigIntExp;
  40     int         bigIntNBits;
  41     boolean     mustSetRoundDir = false;
  42     boolean     fromHex = false;
  43     int         roundDir = 0; // set by doubleValue
  44 
  45     private     FloatingDecimal( boolean negSign, int decExponent, char []digits, int n,  boolean e )
  46     {
  47         isNegative = negSign;
  48         isExceptional = e;


2280                  * the point are the round and sticky bits
2281                  *
2282                  * Number       Round(x)
2283                  * x0.00        x0.
2284                  * x0.01        x0.
2285                  * x0.10        x0.
2286                  * x0.11        x1. = x0. +1
2287                  * x1.00        x1.
2288                  * x1.01        x1.
2289                  * x1.10        x1. + 1
2290                  * x1.11        x1. + 1
2291                  */
2292                 boolean incremented = false;
2293                 boolean leastZero  = ((significand & 1L) == 0L);
2294                 if( (  leastZero  && round && sticky ) ||
2295                     ((!leastZero) && round )) {
2296                     incremented = true;
2297                     significand++;
2298                 }
2299 
2300                 FloatingDecimal fd = new FloatingDecimal(FpUtils.rawCopySign(
2301                                                                  Double.longBitsToDouble(significand),
2302                                                                  sign));
2303 
2304                 /*
2305                  * Set roundingDir variable field of fd properly so
2306                  * that the input string can be properly rounded to a
2307                  * float value.  There are two cases to consider:
2308                  *
2309                  * 1. rounding to double discards sticky bit
2310                  * information that would change the result of a float
2311                  * rounding (near halfway case between two floats)
2312                  *
2313                  * 2. rounding to double rounds up when rounding up
2314                  * would not occur when rounding to float.
2315                  *
2316                  * For former case only needs to be considered when
2317                  * the bits rounded away when casting to float are all
2318                  * zero; otherwise, float round bit is properly set
2319                  * and sticky will already be true.
2320                  *




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 

  28 import sun.misc.DoubleConsts;
  29 import sun.misc.FloatConsts;
  30 import java.util.regex.*;
  31 
  32 public class FloatingDecimal{
  33     boolean     isExceptional;
  34     boolean     isNegative;
  35     int         decExponent;
  36     char        digits[];
  37     int         nDigits;
  38     int         bigIntExp;
  39     int         bigIntNBits;
  40     boolean     mustSetRoundDir = false;
  41     boolean     fromHex = false;
  42     int         roundDir = 0; // set by doubleValue
  43 
  44     private     FloatingDecimal( boolean negSign, int decExponent, char []digits, int n,  boolean e )
  45     {
  46         isNegative = negSign;
  47         isExceptional = e;


2279                  * the point are the round and sticky bits
2280                  *
2281                  * Number       Round(x)
2282                  * x0.00        x0.
2283                  * x0.01        x0.
2284                  * x0.10        x0.
2285                  * x0.11        x1. = x0. +1
2286                  * x1.00        x1.
2287                  * x1.01        x1.
2288                  * x1.10        x1. + 1
2289                  * x1.11        x1. + 1
2290                  */
2291                 boolean incremented = false;
2292                 boolean leastZero  = ((significand & 1L) == 0L);
2293                 if( (  leastZero  && round && sticky ) ||
2294                     ((!leastZero) && round )) {
2295                     incremented = true;
2296                     significand++;
2297                 }
2298 
2299                 FloatingDecimal fd = new FloatingDecimal(Math.copySign(
2300                                                               Double.longBitsToDouble(significand),
2301                                                               sign));
2302 
2303                 /*
2304                  * Set roundingDir variable field of fd properly so
2305                  * that the input string can be properly rounded to a
2306                  * float value.  There are two cases to consider:
2307                  *
2308                  * 1. rounding to double discards sticky bit
2309                  * information that would change the result of a float
2310                  * rounding (near halfway case between two floats)
2311                  *
2312                  * 2. rounding to double rounds up when rounding up
2313                  * would not occur when rounding to float.
2314                  *
2315                  * For former case only needs to be considered when
2316                  * the bits rounded away when casting to float are all
2317                  * zero; otherwise, float round bit is properly set
2318                  * and sticky will already be true.
2319                  *