What would you do if you need to raise an integer to a non-integer power?
If you are dealing with non-integer powers, you should use double precision variables
and the pow()
method from Java.lang.Math
.
Here are
some additional methods of the BigInteger
class.
See its documentation for details.
The exercises discuss some of them.
public BigInteger abs()
Returns aBigInteger
whose values is the absolute value of thisBigInteger
.
public int bitLength()
Returns the smallest number of bits it would take to represent thisBigInteger
using two's complement, excluding the sign bit. For positive integers this is the number of bits it would take to represent the integer using the same type of representation as used inint
s.
public double doubleValue()
Returns the value of thisBigInteger
as adouble
. The result may not be exact. It theBigInteger
is too big or too negative the value of thedouble
will be set to a special value, Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY.
public BigInteger gcd(BigInteger val)
Returns the value of the greatest common divisor of thisBigInteger
andval
. The gcd is always zero or positive. It is zero when either of the arguments is zero.
public int intValue()
Returns the value of thisBigInteger
as anint
. If the value is too big, the least-significant 32 bits are returned. No error indication is given if this happens, so be careful.
public long longValue()
Returns the value of thisBigInteger
as anint
. If the value is too big, the least-significant 64 bits are returned. No error indication is given if this happens, so be careful.
public BigInteger mod(BigInteger modulus)
Returns aBigInteger
whose value is (this mod m). This is different thanremainder
because mod always returns a zero or positiveBigInteger
.remainder()
returns a value that is sometimes negative. The value returned bymod()
andremainder()
is the same if the operands are positive.
public static BigInteger valueOf(long val)
Returns aBigInteger
whose value the same asval
. This may be more convenient than using a constructor.
In addition to the above, there are bit-manipulation methods, shift right and shift left, and methods that return integers that are likely to be prime.
How can an integer only be a "likely prime" ?