Getting Floating point: overflow error in factorial code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • y8ycgl
    New Member
    • Nov 2007
    • 12

    Getting Floating point: overflow error in factorial code

    i have a program that i made in my class in Turbo C++ on windows 95or98 i belive. it is made to solve for factorials. th problem is that :
    A) it puts it in exponents
    B) theres too many numbers to count so i need a way to put commas every 3 digits and i dont know how to do that.
    C) any factorials over 1754 give the computer an error that says:
    Floating point: overflow
    here is my program:

    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<conio.h>
    void main ()
     {
      long double bfn=0;
      long int m=0;
      char pause;
      cout<<setiosflags(ios::fixed|ios::showpoint|ios::right)<<setprecision(0);
      cout<<"type what you want a factorial of"<<endl;
      cin>>bfn;
      m=bfn-1;
       while (m>0)
        {
          bfn=bfn*m;
          m=m-1;
          cout<<bfn<<endl;
        }
      cout<<bfn<<endl<<endl;
     }
    how can i fix these problems?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    1) You are using void main(). This is non-standard and definitely not advised. You should use int main() and return 0 at the end of the function.

    2) In order to print the number with commas, you;d need to manipulate it as a string, not a number. If you treat it as a string, it's an easy task to print 3 characters at a time, then a comma, then repeat.

    3) The numbers are in exponents because they are huge, like 10+ digits each.

    4) Your computer ives you an error because anything larger then ~1700! is too large for a float to hold. You might e able to use a double for more precision, but realize that factorial numbers are HUGE. 1700! = 1700 * 1699 * 1698 * 1697 * 1696 * ... * 3 * 2 * 1. That's a MASSIVE number. Your program won't be able to handle numbers much bigger than this.

    Comment

    • y8ycgl
      New Member
      • Nov 2007
      • 12

      #3
      my teacher says that void main () works with our compiler. int main () also works but thats irrelevent to the solving of the problem.

      i havent realy goten into strings yet so im lost there.

      i compleatly realize what factorial is and how big it is. the thing is is that there must be a way to:
      A) get rid of the exponents and just show the number and
      B) expand it so that i can show the whole number without an error.

      Comment

      • mschenkelberg
        New Member
        • Jun 2007
        • 44

        #4
        try using for loops and do the multiplication and addition the long way using int array/vector. Thats how I would approach it!

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Originally posted by y8ycgl
          my teacher says that void main () works with our compiler. int main () also works....
          It's irrelevant that your compiler accepts void main(). As many of the experts here will tell you, void main() just isn't correct. It's not supported by C++ standards, and shouldn't ever be used. Your compiler supporting it is an extra that you cannot guarantee will work on, say, my compiler (which doesn't like void main()). If you want to write code that will work universally on any C++ compiler, use int main(). I agree it doesn't help in solving this problem, but it will help you be a better C++ coder, which I'm sure is something you want.

          Comment

          • y8ycgl
            New Member
            • Nov 2007
            • 12

            #6
            so can anyone help me actualy write this code? im not far enough to actualy understand a vector thing...... and once again: what would slove the exponents and what would solve the overflow? as for the main () i will change it to int

            Comment

            • y8ycgl
              New Member
              • Nov 2007
              • 12

              #7
              curent program:
              Code:
              #include<iostream.h>
              #include<iomanip.h>
              #include<conio.h>
              int main ()
               {
                long double bfn=0;
                long int m=0;
                char pause;
                cout<<setiosflags(ios::fixed|ios::showpoint|ios::right)<<setprecision(0);
                cout<<"type what you want a factorial of"<<endl;
                cin>>bfn;
                m=bfn-1;
              	while (m>0)
              	 {
              	  bfn=bfn*m;
              	  m=m-1;
                                cout<<bfn<<endl;
              	 }
                cout<<bfn<<endl<<endl;
               }

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                OP, a number of points are worth making. For one thing, your teacher seems not to have checked a book on modern C++. And he isn’t too concerned about standards either. I realize you just want this problem solved, but we will comment on everything. Standard C++ means int main() as you made the changes. Modern C++ also means new standard header style formats: <iostream>, not <iostream.h> and <iomanip> and not <iomanip.h>. I wouldn’t recommend on relying on conio.h. It’s a non standard header, and it’s a bad thing for beginner C++ programmers to be hooked on to non-standard code unnecessarily.

                Actually, I don’t even know what compiler you are using. If it supports void main(), I don’t have much faith in it. void main() shouldn’t even compile. It doesn’t happen to be an old Borland compiler, does it?

                You indentation isn’t very good. Two things are the reason. One is a bad code editor. I sincerely hope you are not trying to work with this code in something like Notepad. You should be using a proper plain text editor or IDE that has a decent editor. And make sure your indentation is consistent. Indentation is more than making code look pretty. It allows you to read code properly and spot mistakes.

                Now onto your actual question. The answer is, don’t work with such large factorials. You say you understand how large the numbers are. I highly doubt it. Take out a calculator and calculate 10!, then 11!, then 12!. See how quickly the magnitude increases? Just from an order of magnitude calculation, do you realize how zeroes have to be shown on screen for 3000! ? Do you?

                More over, with this enormous number, how in the world do you think a computer is intrinsically capable of handling it? You need to write extra code to properly handle large numbers like this. It’s not a trivial task. If you really want to to achieve it, you should look into large number libraries. Google for gmplib , for example.

                Comment

                • y8ycgl
                  New Member
                  • Nov 2007
                  • 12

                  #9
                  i totaly understand the magnetude of 3000! thats why i want to find it. as for conio, it was for some function or another that i am useing. now the reson that i am posting here is because i WANT TO KNOW. i want to see how i can do this. everyone says that stuff is wrong and that its too big and that i need more code. well? what is that code? im a beginer! i want to see this happen! 3000! will solve how many posabilitys as to how many DIFFERENT humans there can be. most of these are of corce failures and dont even come to life. as for this program i will mod and tweek it till it works but you all need to tell me what i am doing!

                  Comment

                  • y8ycgl
                    New Member
                    • Nov 2007
                    • 12

                    #10
                    curent updated version:
                    [code]
                    #include<iostre am.h>
                    #include<iomani p.h>
                    #include<iostre am>
                    #include<iomani p>

                    int main ()
                    {
                    long double bfn=0;
                    long int m=0;
                    char pause;
                    cout<<setiosfla gs(ios::fixed|i os::showpoint|i os::right)<<set precision(0);
                    cout<<"type what you want a factorial of"<<endl;
                    cin>>bfn;
                    m=bfn-1;
                    while (m>0)
                    {
                    bfn=bfn*m;
                    m=m-1;
                    cout<<bfn<<endl ;
                    }
                    cout<<bfn<<endl <<endl;
                    return 0
                    }

                    Comment

                    • oler1s
                      Recognized Expert Contributor
                      • Aug 2007
                      • 671

                      #11
                      i totaly understand the magnetude of 3000!
                      Ok, but you definitely don't understand the implications of that large magnitude. I realize you are frustrated and just want the code. But it isn't that simple, and we cannot provide you with the code. There are reasons we cannot, and we are trying to explain them. Try to understand what we're saying first.

                      I'll give you a quick lesson. When you do something like a = b + c in the code, it gets translated to a number of hardware operations. The code is compiled down into base line instructions, into something like retrieve the value for b, store in a register, retrieve for c store in a register, add up the registers, push back into memory. That sort of thing. That means all your code that you write is subject to hardware limitations. Take a look at http://en.wikipedia.org/wiki/Integer...ter_science%29. The point is, you can't have a number bigger than the number of bits that the hardware has for that value. Do you understand this conclusion crystal clear?

                      Therefore, arbitrary precision arithmetic (i.e. very large numbers) involves some extra software logic over memory. It gets into some pretty advanced logic and mathematics. You could try to implement something naive. But you'll find even that a monumental task.

                      You should look for libraries of code that support arbitrary-precision arithmetic, and use them. GMPLib is well known. I mentioned this library in my previous post. Of course, you need to be at the level of a C programmer that can work with 3rd party libraries and use GMPLib.

                      Have you understood the points I have made? If you just return and whine about "what is the code", then we can't help you.

                      Comment

                      • oler1s
                        Recognized Expert Contributor
                        • Aug 2007
                        • 671

                        #12
                        FWIW it's worth, there's a calculator on the GMPLib page. It will calculate 3000! for you. I can't paste the result here properly, but no point to it anyway. I'm sure you can find your way aroudn the GMP site.

                        Comment

                        • y8ycgl
                          New Member
                          • Nov 2007
                          • 12

                          #13
                          i see... so i would need a more advanced compiler to get a bigger number? or a better computer?

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            No, you would need more advanced method of actually computing the value, involving manipulating bits themselves, or making a data structure that could manipulate numbers in a different way - possibly as arrays/vectors of digits. But all this requires you doing a lot more advanced calculations than a simple multiplication. Basically, calculating 3000! is a task that doesn't really fall under basic programming skills that you would learn in, say, a first year course.

                            Comment

                            • y8ycgl
                              New Member
                              • Nov 2007
                              • 12

                              #15
                              hmm.... ok then..... how would i go about a vector/array?

                              Comment

                              Working...