Results 1 to 7 of 7
  1. #1
    ravinda is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Unhappy [SOLVED] overflow when calculate factorial

    I code a factoial calculating program with self implemented stack. i use long for stack data type. but for values such as 21! it gives a minus value. why is this happening and how to solve it. :confused:

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    21! = 51090942171709440000.
    what is the max value for long?

    You need to use java.math.BigInteger
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
              21! =  51090942171709440000
    Long.MAX_VALUE = 9223372036854775807
    why is this happening and how to solve it.
    As you yourself say, it's overflowing. Try a more appropriate data type like BigInteger. Or, depending on the accuracy required, an approximation like Stirling's formula.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    A bit more explanation.

    I'll just compare this with int and BigInteger. If you are working on with int, after the 12 int produce a garbage. Actually long makes it to 20. Basically throws all the bits out of the range, 32bits for int and 64bits for long. BigInteger also have a limit, which is only on the limit of memory you have.

  5. #5
    ravinda is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Smile Got the Solution

    I try using BigInteger and it works. It works fine for last numbers like 1000!. thanks for every one who try to help. Specially to Eranga. Thank you.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    It's pleasure to help you lol. :)

    So you've solved the problem, please mark the thread solved. You can fin the option on the 'Thread Tools' on top menu of the first post in this thread.

    Good luck!

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    It's pleasure to help you lol. :)

    So you've solved the problem, please mark the thread solved. You can fin the option on the 'Thread Tools' on top menu of the first post in this thread.

    Good luck!

Similar Threads

  1. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 09:01 PM
  2. calculate fft
    By ram.west in forum Advanced Java
    Replies: 2
    Last Post: 08-27-2008, 03:05 AM
  3. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 11:09 AM
  4. Calculate Tax in java
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:03 AM
  5. Calculate what e1 and e2 should be
    By Legoland in forum New To Java
    Replies: 11
    Last Post: 07-02-2007, 06:01 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
  •