Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > BigInteger()

Reply
Thread Tools

BigInteger()

 
 
Colin Hemmings
Guest
Posts: n/a
 
      02-28-2006
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?
 
Reply With Quote
 
 
 
 
Thomas Schodt
Guest
Posts: n/a
 
      02-28-2006
Colin Hemmings wrote:
> At the minute I am having trouble storing any kind of integer in the
> biginteger variable e.g.
> BigInteger bigNum1 = 15000;


You cannot assign a primitive (int) value to an object reference.

Use one of the constructors, like
<http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String)>
as in
BigInteger bigNum1 = new BigInteger("15000");
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      02-28-2006

"Colin Hemmings" <(E-Mail Removed)> wrote in message
news:Ta4Nf.19152$(E-Mail Removed)...
> 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

--
Mike W


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      02-28-2006
Colin Hemmings <(E-Mail Removed)> writes:
>BigInteger bigNum1 = 15000;


It might help to read the API specification:

http://download.java.net/jdk6/docs/a...igInteger.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

 
Reply With Quote
 
ozgwei
Guest
Posts: n/a
 
      02-28-2006
>
> 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...

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-01-2006
On Tue, 28 Feb 2006 22:31:15 GMT, Colin Hemmings
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

> BigInteger bigNum1 = 15000;
>
>I am probably making some stupid mistake but not sure wh


BigIntegers are objects, with an immutable value.
see http://mindprod.com/jgloss/biginteger.html
for sample code to use them. They are not used like primitives.
You can't say BigInteger x = 1;
There is no autoboxing for them either.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      03-01-2006

"ozgwei" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> >
> > 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.
>


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

--
Mike W


 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      03-01-2006
VisionSet wrote:
> "ozgwei" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>>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.

>
> 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()].
 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      03-01-2006

"Thomas Schodt" <(E-Mail Removed)> wrote in message
news:du42j1$26u$1$(E-Mail Removed)...
> VisionSet wrote:
> > "ozgwei" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) oups.com...
> >
> >>>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.

> >
> > 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?

--
Mike W


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-01-2006
On Wed, 01 Mar 2006 12:28:36 GMT, "VisionSet" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments