BigInteger()

Discussion in 'Java' started by Colin Hemmings, Feb 28, 2006.

  1. 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?
    Colin Hemmings, Feb 28, 2006
    #1
    1. Advertising

  2. 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");
    Thomas Schodt, Feb 28, 2006
    #2
    1. Advertising

  3. Colin Hemmings

    VisionSet Guest

    "Colin Hemmings" <> wrote in message
    news:Ta4Nf.19152$...
    > 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
    VisionSet, Feb 28, 2006
    #3
  4. Colin Hemmings

    Stefan Ram Guest

    Colin Hemmings <> writes:
    >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
    Stefan Ram, Feb 28, 2006
    #4
  5. Colin Hemmings

    ozgwei Guest

    >
    > 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...
    ozgwei, Feb 28, 2006
    #5
  6. Colin Hemmings

    Roedy Green Guest

    On Tue, 28 Feb 2006 22:31:15 GMT, Colin Hemmings
    <> 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.
    Roedy Green, Mar 1, 2006
    #6
  7. Colin Hemmings

    VisionSet Guest

    "ozgwei" <> wrote in message
    news:...
    > >
    > > 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
    VisionSet, Mar 1, 2006
    #7
  8. VisionSet wrote:
    > "ozgwei" <> wrote in message
    > news:...
    >
    >>>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()].
    Thomas Schodt, Mar 1, 2006
    #8
  9. Colin Hemmings

    VisionSet Guest

    "Thomas Schodt" <> wrote in message
    news:du42j1$26u$1$...
    > VisionSet wrote:
    > > "ozgwei" <> wrote in message
    > > news:...
    > >
    > >>>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
    VisionSet, Mar 1, 2006
    #9
  10. Colin Hemmings

    Roedy Green Guest

    On Wed, 01 Mar 2006 12:28:36 GMT, "VisionSet" <>
    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.
    Roedy Green, Mar 1, 2006
    #10
  11. Colin Hemmings

    P.Hill Guest

    Colin Hemmings wrote:
    > BigInteger bigNum1 = 15000;


    Maybe all you want is a long

    long bigNum1 = 15000;

    -Paul
    P.Hill, Mar 2, 2006
    #11
  12. Colin Hemmings

    Roedy Green Guest

    On Wed, 01 Mar 2006 23:02:00 -0800, "P.Hill"
    <> wrote, quoted or indirectly quoted
    someone who said :

    > 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 2, 2006
    #12
  13. Colin Hemmings

    James McGill Guest

    On Wed, 2006-03-01 at 23:02 -0800, P.Hill wrote:
    > Colin Hemmings wrote:
    > > BigInteger bigNum1 = 15000;

    >
    > 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; //?
    James McGill, Mar 2, 2006
    #13
  14. Colin Hemmings

    VisionSet Guest

    "James McGill" <> wrote in message
    news:...
    > On Wed, 2006-03-01 at 23:02 -0800, P.Hill wrote:
    > > Colin Hemmings wrote:
    > > > BigInteger bigNum1 = 15000;

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


    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

    --
    Mike W
    VisionSet, Mar 2, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pete kirkham
    Replies:
    5
    Views:
    458
    pete kirkham
    Aug 13, 2003
  2. xarax
    Replies:
    6
    Views:
    8,603
    xarax
    Sep 8, 2003
  3. David Goulet

    BigInteger

    David Goulet, Jan 25, 2004, in forum: Java
    Replies:
    1
    Views:
    650
  4. nick
    Replies:
    0
    Views:
    894
  5. nick
    Replies:
    1
    Views:
    31,747
    Eric Sosman
    Oct 26, 2004
Loading...

Share This Page