BigInteger()

C

Colin Hemmings

Hi There,
Can anybody give me some help using BigInteger() in Java, I have
tried the API but I am still unsure on how it works.

I am simply trying to store big integers taken in from a user and do
simple arithmetic operations on them.

At the minute I am having trouble storing any kind of integer in the
biginteger variable e.g.
BigInteger bigNum1 = 15000;

I am probably making some stupid mistake but not sure what

pleas would somebody be able to help?
 
V

VisionSet

Colin Hemmings said:
Hi There,
Can anybody give me some help using BigInteger() in Java, I have
tried the API but I am still unsure on how it works.

I am simply trying to store big integers taken in from a user and do
simple arithmetic operations on them.

At the minute I am having trouble storing any kind of integer in the
biginteger variable e.g.
BigInteger bigNum1 = 15000;

I am probably making some stupid mistake but not sure what

pleas would somebody be able to help?

You're trying to use it as if it were a primitive:

eg

int i = 10;

it isn't, it is an object, objects are instantiated, try:

BigInteger bi = new BigInteger(15000);

do you really need a BigInteger?

A long goes up to +2^63 - 1
 
S

Stefan Ram

Colin Hemmings said:
BigInteger bigNum1 = 15000;

It might help to read the API specification:

http://download.java.net/jdk6/docs/api/java/math/BigInteger.html

Here are two example programs written by me:

public class Main
{ public static void main( final java.lang.String[] args )
{ final java.math.BigInteger b11 = new java.math.BigInteger( "11" );
final java.math.BigInteger b121 = new java.math.BigInteger( "121" );
final java.math.BigInteger product = b11.multiply( b11 );
final boolean result = product.equals( b121 );
System.out.println( result ); }}

true

public class Main
{ public static void main( final java.lang.String[] args )
{ System.out.println
( ( new java.math.BigInteger( "11" ).multiply( new java.math.BigInteger( "11" )))
.equals
( new java.math.BigInteger( "121" ))); }}

true
 
O

ozgwei

BigInteger bigNum1 = new BigInteger(15000);

This is not recommended.

A better way is:

BigInteger bigNum1 = BigInteger.valueOf(15000l);

This allows BigInteger objects with the same number to be reused.

See "Effective Java Programming Language Guide" Item 1 "Consider
providing static factory methods instead of constructors" & Item 4
"Avoid creating duplicate objects".

The same can be applied to BigDecimal. Instead of:
BigDecimal decimal = new BigDecimal("15000.00");
or
BigDecimal decimal = new BigDecimal(15000.00);
or
BigDecimal decimal = new BigDecimal(new BigInteger(15000));
or
BigDecimal decimal = new BigDecimal(new BigInteger(1500000), 2);

use:
BigDecimal decimal = BigDecimal.valueOf(15000l);
or
BigDecimal decimal = BigDecimal.valueOf(1500000l, 2);

Creating a lot of BigDecimal can be a performance problem, if you have
to deal with a lot of monetary calculations...
 
V

VisionSet

ozgwei said:
This is not recommended.

A better way is:

BigInteger bigNum1 = BigInteger.valueOf(15000l);

This allows BigInteger objects with the same number to be reused.

Actually it looks like the class has a large overhead, when it loads it
creates Integer.MAX_VALUE*2 objects for that cache.
 
T

Thomas Schodt

VisionSet said:
Actually it looks like the class has a large overhead, when it loads it
creates Integer.MAX_VALUE*2 objects for that cache.

A peek at the source shows MAX_CONSTANT = 16
which suggests 2 times 16 BigIntegers
(1 to 16 and -1 to -16) are allocated.

And ZERO, ONE, TWO, TEN refer to these preallocated constants
[through valueOf()].
 
V

VisionSet

Thomas Schodt said:
VisionSet said:
Actually it looks like the class has a large overhead, when it loads it
creates Integer.MAX_VALUE*2 objects for that cache.

A peek at the source shows MAX_CONSTANT = 16
which suggests 2 times 16 BigIntegers
(1 to 16 and -1 to -16) are allocated.

And ZERO, ONE, TWO, TEN refer to these preallocated constants
[through valueOf()].

I thought that would be a bit odd, should have looked closer.
But what is so significant about -16 to +16?
 
R

Roedy Green

I thought that would be a bit odd, should have looked closer.
But what is so significant about -16 to +16?

look at all the mathematical and physical formulas you can find
anywhere. You will discover the they don't tend to use constants like
42. They are very big on quite small integers and numbers like e, and
pi.

It is part of the inherent drive to simplicity in the fundamental
"design" of the universe.
 
R

Roedy Green

long bigNum1 = 15000;

it is probably best to get into the habit of writing that:

long bigNum1 = 15000L;

If you write

long bigNum1 = 111222333444;

It won't work.
 
J

James McGill

Maybe all you want is a long

long bigNum1 = 15000;

I don't know if it's required for the same reasons C requires it,
but, should this not be:

long bigNum1 = 15000L; //?
 
V

VisionSet

James McGill said:
I don't know if it's required for the same reasons C requires it,
but, should this not be:

long bigNum1 = 15000L; //?

all literal int looking values are ints in Java, the fact

long x = 15000;

works is because it is a literal and the compiler says 'I know there isn't a
problem converting that int to a long, so I'll silently just do it'

however, if you do

long x = 9999999999999;

it won't compile because 9999999999999 is not an int
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top