Java BigInteger Returning Incorrect Value

java biginteger max value
how to initialize biginteger in java
java biginteger bits
how biginteger works in java
how to add(biginteger in java)
biginteger zero value
taking biginteger input in java
biginteger(java 8)

I am trying to generate n digit number with all ones using BigInteger.

for(int i=0;i<n;i++){
    genL = genL.add(BigDecimal.valueOf(Math.pow(10,i)).toBigInteger());
    System.out.println(i + " "+ genL);
            }

I am expecting all ones in the output result sequence. But I am getting the below output. Zero is getting inserted for i = 23 and 24. Is there anything I am missing out?

0 1

1 11

2 111

3 1111

4 11111

5 111111

6 1111111

7 11111111

8 111111111

9 1111111111

10 11111111111

11 111111111111

12 1111111111111

13 11111111111111

14 111111111111111

15 1111111111111111

16 11111111111111111

17 111111111111111111

18 1111111111111111111

19 11111111111111111111

20 111111111111111111111

21 1111111111111111111111

22 11111111111111111111111

23 111111111111111101111111

24 1111111111111111101111111

Is there anything I am missing out?

Yea. Math.pow(10,i) is returning a double and that only has 53 bits of precision.


You could rewrite your code to use the BigInteger.pow method instead.

However, an (IMO) simpler version would be

genL = BigInteger.ZERO;
for (int i = 0; i < n; i++) {
    genL = genL.mult(BigInteger.TEN) + BigInteger.ONE;
    System.out.println(i + " " + genL);
}

Or if you only care about what the output looks like, just use a string builder / string concatenation; e.g.

genL = new StringBuilder();
for (int i = 0; i < n; i++) {
    genL.append("1");
    System.out.println(i + " " + genL);
}

BigInteger intValue() Method in Java, If the value returned by this function is too big to fit into integer value, then it will The method returns an int value which represents integer value for this BigInteger . Please Improve this article if you find anything incorrect by clicking on the� The java.math.BigInteger.intValue () converts this BigInteger to an integer value. If the value returned by this function is too big to fit into integer value, then it will return only the low-order 32 bits. Further there is chance that this conversion can loose information about the overall magnitude of the BigInteger value.

Math.pow return a double value, which couldn't guarantee an exact accuracy for large number.

In this case, Math.pow(10, 23) returns 1.0000000000000001E23, which when converted to BigInteger, it becomes 100000000000000010000000, thus causing that 0 in the middle.

I'd suggested that you replaced Math.pow(10, i) with BigInteger.TEN.pow(i) instead.

JDK-6910473 java.math.BigInteger.bitLength() may return , JDK-8022780 - Incorrect BigInteger division because of MutableBigInteger. In these case bitLength() method returns negative "int" value and toByteArray()� The java.math.BigInteger.valueOf(long val) returns a BigInteger whose value is equal to that of the specified long. This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers. Declaration. Following is the declaration for java.math.BigInteger.valueOf() method.

It would be much simpler (and more efficient) to create your BigInteger with the BigInteger(String val) constructor:

new BigInteger("111111111111111111111111111111"); // as many 1s as you want

Or, to generalize:

char[] ones = new char[n];
Arrays.fill(ones,'1');
BigInteger genL = new BigInteger(new String (ones));

JDK-8022780 Incorrect BigInteger division because of , JDK-8022780 : Incorrect BigInteger division because of MutableBigInteger. bitLength() bitLength() may return negative "int" on large numbers. Description. FULL PRODUCT VERSION : java version " 1.8.0-ea " Java(TM) SE Runtime Environment (build Divisor is the first value above BURNIKEL_ZIEGLER_THRESHOLD. exponent − Exponent to which this BigInteger is to be raised. Return Value. This method returns a BigInteger object whose value is this exponent. Exception. ArithmeticException − Exponent is negative. This would cause the operation to yield a non-integer value. Example. The following example shows the usage of math.BigInteger.pow() method.

You should be using my code as follows, works like a charm!

    BigInteger i = new BigInteger("0");
    BigInteger ten = new BigInteger("10");
    BigInteger one = new BigInteger("1");
    for (int j = 0; j < 64; j++) {
        i = i.multiply(ten).add(one);
        System.out.println(i);
    }

JDK-8021204 Constructor BigInteger(String val, int radix , JDK-8021204 : Constructor BigInteger(String val, int radix) doesn't detect overflow. Type: Bug; Component: core-libs; Sub-Component: java.math; Affected Version: 8 bitLength() may return negative "int" on large numbers They may consume long string silently and construct BigInteger object with incorrect value. STEPS� The java.math.BigInteger.longValue() converts this BigInteger to a long value. If the value return by this function is too big to fit into long value, then it will return only the low-order 64 bits. There is chance that this conversion can loose information about the overall magnitude of the BigInteger value.

Java: java.math.BigInteger, java.math.BigInteger. Unbounded range. To work with integers that are larger static void main(String[] args) { int bad = 2000000000; //Close to int max value. Description. The java.math.BigInteger.add(BigInteger val) returns a BigInteger object whose value is (this + val).. Declaration. Following is the declaration for java

BigInteger (Java Platform SE 7 ), These methods always return a non-negative result, between 0 and (modulus - 1) Returns a BigInteger whose value is the absolute value of this BigInteger. BigInteger flipBit(int n): This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped. float floatValue() : This method converts this BigInteger to a float.

NUM00-J. Detect or prevent integer overflow, The meaning of the return value of the compareTo() method is defined only in Type BigInteger is the standard arbitrary-precision integer type provided by the of the Java 8 release, and they also either return a mathematically correct value� The java.math.BigInteger.intValue () converts this BigInteger to an int. This conversion is analogous to a narrowing primitive conversion from long to int. If this BigInteger is too big to fit in an int, only the low-order 32 bits are returned.

Comments
  • Just for the record: when you want to print strings like 1, 11, 111, then just treat them as strings. Start with "1" and keep appending "1" to that. Then there is 0 chance that "0" will be printed anyway.
  • It would be much simpler to just print a simple string, and keep adding "1" to the end.