< prev index next >

src/java.base/share/classes/java/math/BigInteger.java

Print this page
rev 60127 : 8249205: Remove unnecessary trademark symbols


  37 import java.util.Objects;
  38 import java.util.Random;
  39 import java.util.concurrent.ThreadLocalRandom;
  40 
  41 import jdk.internal.math.DoubleConsts;
  42 import jdk.internal.math.FloatConsts;
  43 import jdk.internal.HotSpotIntrinsicCandidate;
  44 import jdk.internal.vm.annotation.Stable;
  45 import jdk.internal.vm.annotation.ForceInline;
  46 
  47 /**
  48  * Immutable arbitrary-precision integers.  All operations behave as if
  49  * BigIntegers were represented in two's-complement notation (like Java's
  50  * primitive integer types).  BigInteger provides analogues to all of Java's
  51  * primitive integer operators, and all relevant methods from java.lang.Math.
  52  * Additionally, BigInteger provides operations for modular arithmetic, GCD
  53  * calculation, primality testing, prime generation, bit manipulation,
  54  * and a few other miscellaneous operations.
  55  *
  56  * <p>Semantics of arithmetic operations exactly mimic those of Java's integer
  57  * arithmetic operators, as defined in <i>The Java&trade; Language Specification</i>.
  58  * For example, division by zero throws an {@code ArithmeticException}, and
  59  * division of a negative by a positive yields a negative (or zero) remainder.
  60  *
  61  * <p>Semantics of shift operations extend those of Java's shift operators
  62  * to allow for negative shift distances.  A right-shift with a negative
  63  * shift distance results in a left shift, and vice-versa.  The unsigned
  64  * right shift operator ({@code >>>}) is omitted since this operation
  65  * only makes sense for a fixed sized word and not for a
  66  * representation conceptually having an infinite number of leading
  67  * virtual sign bits.
  68  *
  69  * <p>Semantics of bitwise logical operations exactly mimic those of Java's
  70  * bitwise integer operators.  The binary operators ({@code and},
  71  * {@code or}, {@code xor}) implicitly perform sign extension on the shorter
  72  * of the two operands prior to performing the operation.
  73  *
  74  * <p>Comparison operations perform signed integer comparisons, analogous to
  75  * those performed by Java's relational and equality operators.
  76  *
  77  * <p>Modular arithmetic operations are provided to compute residues, perform


4135         byte[] byteArray = new byte[byteLen];
4136 
4137         for (int i=byteLen-1, bytesCopied=4, nextInt=0, intIndex=0; i >= 0; i--) {
4138             if (bytesCopied == 4) {
4139                 nextInt = getInt(intIndex++);
4140                 bytesCopied = 1;
4141             } else {
4142                 nextInt >>>= 8;
4143                 bytesCopied++;
4144             }
4145             byteArray[i] = (byte)nextInt;
4146         }
4147         return byteArray;
4148     }
4149 
4150     /**
4151      * Converts this BigInteger to an {@code int}.  This
4152      * conversion is analogous to a
4153      * <i>narrowing primitive conversion</i> from {@code long} to
4154      * {@code int} as defined in
4155      * <cite>The Java&trade; Language Specification</cite>:
4156      * if this BigInteger is too big to fit in an
4157      * {@code int}, only the low-order 32 bits are returned.
4158      * Note that this conversion can lose information about the
4159      * overall magnitude of the BigInteger value as well as return a
4160      * result with the opposite sign.
4161      *
4162      * @return this BigInteger converted to an {@code int}.
4163      * @see #intValueExact()
4164      * @jls 5.1.3 Narrowing Primitive Conversion
4165      */
4166     public int intValue() {
4167         int result = 0;
4168         result = getInt(0);
4169         return result;
4170     }
4171 
4172     /**
4173      * Converts this BigInteger to a {@code long}.  This
4174      * conversion is analogous to a
4175      * <i>narrowing primitive conversion</i> from {@code long} to
4176      * {@code int} as defined in
4177      * <cite>The Java&trade; Language Specification</cite>:
4178      * if this BigInteger is too big to fit in a
4179      * {@code long}, only the low-order 64 bits are returned.
4180      * Note that this conversion can lose information about the
4181      * overall magnitude of the BigInteger value as well as return a
4182      * result with the opposite sign.
4183      *
4184      * @return this BigInteger converted to a {@code long}.
4185      * @see #longValueExact()
4186      * @jls 5.1.3 Narrowing Primitive Conversion
4187      */
4188     public long longValue() {
4189         long result = 0;
4190 
4191         for (int i=1; i >= 0; i--)
4192             result = (result << 32) + (getInt(i) & LONG_MASK);
4193         return result;
4194     }
4195 
4196     /**
4197      * Converts this BigInteger to a {@code float}.  This
4198      * conversion is similar to the
4199      * <i>narrowing primitive conversion</i> from {@code double} to
4200      * {@code float} as defined in
4201      * <cite>The Java&trade; Language Specification</cite>:
4202      * if this BigInteger has too great a magnitude
4203      * to represent as a {@code float}, it will be converted to
4204      * {@link Float#NEGATIVE_INFINITY} or {@link
4205      * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
4206      * the return value is finite, this conversion can lose
4207      * information about the precision of the BigInteger value.
4208      *
4209      * @return this BigInteger converted to a {@code float}.
4210      * @jls 5.1.3 Narrowing Primitive Conversion
4211      */
4212     public float floatValue() {
4213         if (signum == 0) {
4214             return 0.0f;
4215         }
4216 
4217         int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1;
4218 
4219         // exponent == floor(log2(abs(this)))
4220         if (exponent < Long.SIZE - 1) {
4221             return longValue();


4266         int signifRounded = increment ? signifFloor + 1 : signifFloor;
4267         int bits = ((exponent + FloatConsts.EXP_BIAS))
4268                 << (FloatConsts.SIGNIFICAND_WIDTH - 1);
4269         bits += signifRounded;
4270         /*
4271          * If signifRounded == 2^24, we'd need to set all of the significand
4272          * bits to zero and add 1 to the exponent. This is exactly the behavior
4273          * we get from just adding signifRounded to bits directly. If the
4274          * exponent is Float.MAX_EXPONENT, we round up (correctly) to
4275          * Float.POSITIVE_INFINITY.
4276          */
4277         bits |= signum & FloatConsts.SIGN_BIT_MASK;
4278         return Float.intBitsToFloat(bits);
4279     }
4280 
4281     /**
4282      * Converts this BigInteger to a {@code double}.  This
4283      * conversion is similar to the
4284      * <i>narrowing primitive conversion</i> from {@code double} to
4285      * {@code float} as defined in
4286      * <cite>The Java&trade; Language Specification</cite>:
4287      * if this BigInteger has too great a magnitude
4288      * to represent as a {@code double}, it will be converted to
4289      * {@link Double#NEGATIVE_INFINITY} or {@link
4290      * Double#POSITIVE_INFINITY} as appropriate.  Note that even when
4291      * the return value is finite, this conversion can lose
4292      * information about the precision of the BigInteger value.
4293      *
4294      * @return this BigInteger converted to a {@code double}.
4295      * @jls 5.1.3 Narrowing Primitive Conversion
4296      */
4297     public double doubleValue() {
4298         if (signum == 0) {
4299             return 0.0;
4300         }
4301 
4302         int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1;
4303 
4304         // exponent == floor(log2(abs(this))Double)
4305         if (exponent < Long.SIZE - 1) {
4306             return longValue();




  37 import java.util.Objects;
  38 import java.util.Random;
  39 import java.util.concurrent.ThreadLocalRandom;
  40 
  41 import jdk.internal.math.DoubleConsts;
  42 import jdk.internal.math.FloatConsts;
  43 import jdk.internal.HotSpotIntrinsicCandidate;
  44 import jdk.internal.vm.annotation.Stable;
  45 import jdk.internal.vm.annotation.ForceInline;
  46 
  47 /**
  48  * Immutable arbitrary-precision integers.  All operations behave as if
  49  * BigIntegers were represented in two's-complement notation (like Java's
  50  * primitive integer types).  BigInteger provides analogues to all of Java's
  51  * primitive integer operators, and all relevant methods from java.lang.Math.
  52  * Additionally, BigInteger provides operations for modular arithmetic, GCD
  53  * calculation, primality testing, prime generation, bit manipulation,
  54  * and a few other miscellaneous operations.
  55  *
  56  * <p>Semantics of arithmetic operations exactly mimic those of Java's integer
  57  * arithmetic operators, as defined in <i>The Java Language Specification</i>.
  58  * For example, division by zero throws an {@code ArithmeticException}, and
  59  * division of a negative by a positive yields a negative (or zero) remainder.
  60  *
  61  * <p>Semantics of shift operations extend those of Java's shift operators
  62  * to allow for negative shift distances.  A right-shift with a negative
  63  * shift distance results in a left shift, and vice-versa.  The unsigned
  64  * right shift operator ({@code >>>}) is omitted since this operation
  65  * only makes sense for a fixed sized word and not for a
  66  * representation conceptually having an infinite number of leading
  67  * virtual sign bits.
  68  *
  69  * <p>Semantics of bitwise logical operations exactly mimic those of Java's
  70  * bitwise integer operators.  The binary operators ({@code and},
  71  * {@code or}, {@code xor}) implicitly perform sign extension on the shorter
  72  * of the two operands prior to performing the operation.
  73  *
  74  * <p>Comparison operations perform signed integer comparisons, analogous to
  75  * those performed by Java's relational and equality operators.
  76  *
  77  * <p>Modular arithmetic operations are provided to compute residues, perform


4135         byte[] byteArray = new byte[byteLen];
4136 
4137         for (int i=byteLen-1, bytesCopied=4, nextInt=0, intIndex=0; i >= 0; i--) {
4138             if (bytesCopied == 4) {
4139                 nextInt = getInt(intIndex++);
4140                 bytesCopied = 1;
4141             } else {
4142                 nextInt >>>= 8;
4143                 bytesCopied++;
4144             }
4145             byteArray[i] = (byte)nextInt;
4146         }
4147         return byteArray;
4148     }
4149 
4150     /**
4151      * Converts this BigInteger to an {@code int}.  This
4152      * conversion is analogous to a
4153      * <i>narrowing primitive conversion</i> from {@code long} to
4154      * {@code int} as defined in
4155      * <cite>The Java Language Specification</cite>:
4156      * if this BigInteger is too big to fit in an
4157      * {@code int}, only the low-order 32 bits are returned.
4158      * Note that this conversion can lose information about the
4159      * overall magnitude of the BigInteger value as well as return a
4160      * result with the opposite sign.
4161      *
4162      * @return this BigInteger converted to an {@code int}.
4163      * @see #intValueExact()
4164      * @jls 5.1.3 Narrowing Primitive Conversion
4165      */
4166     public int intValue() {
4167         int result = 0;
4168         result = getInt(0);
4169         return result;
4170     }
4171 
4172     /**
4173      * Converts this BigInteger to a {@code long}.  This
4174      * conversion is analogous to a
4175      * <i>narrowing primitive conversion</i> from {@code long} to
4176      * {@code int} as defined in
4177      * <cite>The Java Language Specification</cite>:
4178      * if this BigInteger is too big to fit in a
4179      * {@code long}, only the low-order 64 bits are returned.
4180      * Note that this conversion can lose information about the
4181      * overall magnitude of the BigInteger value as well as return a
4182      * result with the opposite sign.
4183      *
4184      * @return this BigInteger converted to a {@code long}.
4185      * @see #longValueExact()
4186      * @jls 5.1.3 Narrowing Primitive Conversion
4187      */
4188     public long longValue() {
4189         long result = 0;
4190 
4191         for (int i=1; i >= 0; i--)
4192             result = (result << 32) + (getInt(i) & LONG_MASK);
4193         return result;
4194     }
4195 
4196     /**
4197      * Converts this BigInteger to a {@code float}.  This
4198      * conversion is similar to the
4199      * <i>narrowing primitive conversion</i> from {@code double} to
4200      * {@code float} as defined in
4201      * <cite>The Java Language Specification</cite>:
4202      * if this BigInteger has too great a magnitude
4203      * to represent as a {@code float}, it will be converted to
4204      * {@link Float#NEGATIVE_INFINITY} or {@link
4205      * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
4206      * the return value is finite, this conversion can lose
4207      * information about the precision of the BigInteger value.
4208      *
4209      * @return this BigInteger converted to a {@code float}.
4210      * @jls 5.1.3 Narrowing Primitive Conversion
4211      */
4212     public float floatValue() {
4213         if (signum == 0) {
4214             return 0.0f;
4215         }
4216 
4217         int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1;
4218 
4219         // exponent == floor(log2(abs(this)))
4220         if (exponent < Long.SIZE - 1) {
4221             return longValue();


4266         int signifRounded = increment ? signifFloor + 1 : signifFloor;
4267         int bits = ((exponent + FloatConsts.EXP_BIAS))
4268                 << (FloatConsts.SIGNIFICAND_WIDTH - 1);
4269         bits += signifRounded;
4270         /*
4271          * If signifRounded == 2^24, we'd need to set all of the significand
4272          * bits to zero and add 1 to the exponent. This is exactly the behavior
4273          * we get from just adding signifRounded to bits directly. If the
4274          * exponent is Float.MAX_EXPONENT, we round up (correctly) to
4275          * Float.POSITIVE_INFINITY.
4276          */
4277         bits |= signum & FloatConsts.SIGN_BIT_MASK;
4278         return Float.intBitsToFloat(bits);
4279     }
4280 
4281     /**
4282      * Converts this BigInteger to a {@code double}.  This
4283      * conversion is similar to the
4284      * <i>narrowing primitive conversion</i> from {@code double} to
4285      * {@code float} as defined in
4286      * <cite>The Java Language Specification</cite>:
4287      * if this BigInteger has too great a magnitude
4288      * to represent as a {@code double}, it will be converted to
4289      * {@link Double#NEGATIVE_INFINITY} or {@link
4290      * Double#POSITIVE_INFINITY} as appropriate.  Note that even when
4291      * the return value is finite, this conversion can lose
4292      * information about the precision of the BigInteger value.
4293      *
4294      * @return this BigInteger converted to a {@code double}.
4295      * @jls 5.1.3 Narrowing Primitive Conversion
4296      */
4297     public double doubleValue() {
4298         if (signum == 0) {
4299             return 0.0;
4300         }
4301 
4302         int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1;
4303 
4304         // exponent == floor(log2(abs(this))Double)
4305         if (exponent < Long.SIZE - 1) {
4306             return longValue();


< prev index next >