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 4 of 4

Thread: how to handle big numbers

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb how to handle big numbers

    How to handle a big number value like
    Math.pow(41,77) gives a large value and I needs to take mod( % ) of that value. So I directly write a stmt as Math.pow(41,77)%119;
    The correct result of this equation is 6(I know its the correct ans. *check it by calculator ). But my code returns 88 so what's wrong in my code?
    How can I rewrite the same code to get the correct output.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to handle big numbers

    Use a BigInteger to prevent the overflow of an int

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to handle big numbers

    Integer can hold 6 right? Why we need to go for big integer ?
    The value of Math.pow(41,77) is 1.5 E124 this value can hold in double variable.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to handle big numbers

    A double variable doesn't hold every single digit of 1.5e124. It holds a fixed "mantissa" which is ~16 digits, and an integer exponent.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    copeg (August 13th, 2013)

Similar Threads

  1. Replies: 3
    Last Post: January 25th, 2013, 06:53 AM
  2. Try : handle an exception by yourself.
    By try in forum Member Introductions
    Replies: 1
    Last Post: July 12th, 2011, 10:37 AM
  3. Adding Big Numbers
    By hoiberg in forum Java Theory & Questions
    Replies: 5
    Last Post: December 16th, 2010, 08:44 AM
  4. El Gamal signature -Handle big integer
    By miotvsingtel in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 9th, 2010, 12:05 PM