Results 1 to 10 of 10
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default inputting large numbers into java

    I want to do an analysis (once) using a java program on a very large amount of digits (1000). My question is: how do I hard code it or input it in my program? I tried hard coding it, but it won't except literals that exceed the int limit.

    edit: So I want those 1000 digits to go into a 1000 member integer array.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    thecodewall is offline Member
    Join Date
    Dec 2010
    Location
    Philippines
    Posts
    5
    Rep Power
    0

    Default

    Try to use double data type

    double myvar = 0.0;

    http://thecodewall.blogspot.com
    Last edited by thecodewall; 12-26-2010 at 12:38 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by thecodewall View Post
    Try to use double data type

    double myvar = 0.0;

    The Code Wall
    Sure, a double has only less than 16 digits of accuracy; the answer either is to use BigInteger or an ordinary int (or short or char or byte) array.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    thecodewall is offline Member
    Join Date
    Dec 2010
    Location
    Philippines
    Posts
    5
    Rep Power
    0

    Default

    thanks for this info.
    The Code Wall

  6. #6
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    How do you hardcode BigIntegers?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by imorio View Post
    How do you hardcode BigIntegers?
    I don't, I simply use the BigInteger class; why reinvent the weel?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    thecodewall is offline Member
    Join Date
    Dec 2010
    Location
    Philippines
    Posts
    5
    Rep Power
    0

    Default

    // sample from Programming tutorials and source code examples

    import java.math.BigInteger;
    public class Main {
    public static void main(String[] argv) throws Exception {
    // Create via a string
    BigInteger bi1 = new BigInteger("1234567890123456890");

    // Create via a long
    BigInteger bi2 = BigInteger.valueOf(123L);

    bi1 = bi1.add(bi2);
    bi1 = bi1.multiply(bi2);
    bi1 = bi1.subtract(bi2);
    bi1 = bi1.divide(bi2);
    bi1 = bi1.negate();
    int exponent = 2;
    bi1 = bi1.pow(exponent);

    }
    }

    http://thecodewall.blogspot.com

  9. #9
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Ah, this is a start. That would about halve the lines needed. So hardcoding a 1000 digits into an integer array would go like this:
    Java Code:
    BigInteger temp;
    temp=new BigInteger("first 20 digits");
    f1(temp);
    temp=new BigInteger("next 20 digits");
    f1(temp);
    temp=new BigInteger("next 20 digits");
    f1(temp);
    ...
    ...
    temp=new BigInteger("last 20 digits");
    f1(temp);
    With f1 being a function that puts the digits into an integer array.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by imorio View Post
    Ah, this is a start. That would about halve the lines needed. So hardcoding a 1000 digits into an integer array would go like this:
    Java Code:
    BigInteger temp;
    temp=new BigInteger("first 20 digits");
    f1(temp);
    temp=new BigInteger("next 20 digits");
    f1(temp);
    temp=new BigInteger("next 20 digits");
    f1(temp);
    ...
    ...
    temp=new BigInteger("last 20 digits");
    f1(temp);
    With f1 being a function that puts the digits into an integer array.
    If you want those 1000 digits to repesent one single number, do this:

    Java Code:
    StringWriter sw= new StringWriter();
    String line;
    while ((line= reader.readLine()) != null)
       sw.write(line);
    BigInteger bi= new BigInteger(sw.toString());
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Getting rid of commas in large numbers?
    By wwuster in forum Advanced Java
    Replies: 12
    Last Post: 03-05-2012, 10:35 AM
  2. Inputting some text in a GUI.
    By AJArmstron@aol.com in forum New To Java
    Replies: 6
    Last Post: 04-15-2010, 01:20 PM
  3. crunching large numbers
    By aaroncarpet in forum Advanced Java
    Replies: 26
    Last Post: 11-30-2009, 02:10 AM
  4. Crunching large numbers part II
    By aaroncarpet in forum Advanced Java
    Replies: 0
    Last Post: 11-28-2009, 05:34 AM
  5. Inputting custom numbers
    By Jakora33 in forum New To Java
    Replies: 14
    Last Post: 09-08-2009, 09:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •