Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How can I do this?

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Question How can I do this?

    my objective is to multiply any 2 numbers(int). The numbers can be extremely large (i.e. run into hundreds of digits) and are provided as strings. The expected output is a string which represents the product of the two numbers. "output shuld be a integer." here is what i coded.
    public class LargeMultiply {
    	static String test1a = "268435456";
    	static String test1b = "524288";
     
    	static String testcaseA = test1a;
    	static String testcaseB = test1b;	
     
    	public static void main(String args[]){
    		LargeMultiply inst = new LargeMultiply();
    		String v = inst.multiply(testcaseA,testcaseB);
    		System.out.println(v);
    	}
     
    	public String multiply(String num1, String num2){
    	Long a=Long.parseLong(num1);
    	Long b=Long.parseLong(num2);
    	return String.valueOf(a*b);
    	}
    }


    and here are outputs

    if inputs are 268435456 524288 these then output is 140737488355328 the above code perform well in this case.
    if inputs are 26843545623423 52428824234 then expected output is 1407375535307804420432982 this but it gives a -ve value due to max limit reached for "long" but if i use "double' then answer is in scientfic form and that is not needed i want output in integer form.

    Is any way to display double in normal form(not with decimal point)??

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How can I do this?

    You could probably use BigInteger, but I would guess you're supposed to write code that computes the answer manually. How do you multiply large numbers without a computer?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    arvindbis (March 7th, 2012)

  4. #3
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: How can I do this?

    you mean to say that implement a algo. which actually we use in real life?

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How can I do this?

    Quote Originally Posted by arvindbis View Post
    you mean to say that implement a algo. which actually we use in real life?
    I'm only guessing because I don't have your requirements in front of me, but this is a pretty common homework assignment. You won't be able to multiply arbitrary-sized numbers by using a standard primitive, so you either have to implement the multiplication yourself or use BigInteger, which is probably cheating.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    arvindbis (March 7th, 2012)

  7. #5
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: How can I do this?

    the only requirement is to display result in integers, double does this but it has decimal piont and exponent system. thats it. I will try with biginteger

    biginterger is damn good but still will use it untill i code something using primitive types.
    Last edited by arvindbis; March 7th, 2012 at 11:36 AM.

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How can I do this?

    Doubles might seem to work, but if there really is no limit on how many characters your digits can have, double won't cover all cases.

    If you don't believe me, check out Double.MAX_VALUE. What happens if you use that as an operand?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #7
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: How can I do this?

    Indeed, double.maximum is equals to 1.7976931348623157E308 very large number but here is the problem as yi=ou can see the decimal piont and "E308" the exponent part. All I want is how can I change position of decimal(dot) i.e. after 13 digits on left or something higher?

  10. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How can I do this?

    You can use DecimalFormat, but like I keep saying, that's not going to work for arbitrarily large numbers. If I was an instructor grading this program, I promise I would use Double.MAX_VALUE as a test case, which will break if you're doing it the way you're suggesting.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!