473,729 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting Floating point: overflow error in factorial code

12 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<iomanip.h>
  3. #include<conio.h>
  4. void main ()
  5.  {
  6.   long double bfn=0;
  7.   long int m=0;
  8.   char pause;
  9.   cout<<setiosflags(ios::fixed|ios::showpoint|ios::right)<<setprecision(0);
  10.   cout<<"type what you want a factorial of"<<endl;
  11.   cin>>bfn;
  12.   m=bfn-1;
  13.    while (m>0)
  14.     {
  15.       bfn=bfn*m;
  16.       m=m-1;
  17.       cout<<bfn<<endl;
  18.     }
  19.   cout<<bfn<<endl<<endl;
  20.  }
  21.  
how can i fix these problems?
Nov 14 '07 #1
26 9438
Ganon11
3,652 Recognized Expert Specialist
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.
Nov 14 '07 #2
y8ycgl
12 New Member
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.
Nov 14 '07 #3
mschenkelberg
44 New Member
try using for loops and do the multiplication and addition the long way using int array/vector. Thats how I would approach it!
Nov 14 '07 #4
Ganon11
3,652 Recognized Expert Specialist
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.
Nov 14 '07 #5
y8ycgl
12 New Member
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
Nov 14 '07 #6
y8ycgl
12 New Member
curent program:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<iomanip.h>
  3. #include<conio.h>
  4. int main ()
  5.  {
  6.   long double bfn=0;
  7.   long int m=0;
  8.   char pause;
  9.   cout<<setiosflags(ios::fixed|ios::showpoint|ios::right)<<setprecision(0);
  10.   cout<<"type what you want a factorial of"<<endl;
  11.   cin>>bfn;
  12.   m=bfn-1;
  13.     while (m>0)
  14.      {
  15.       bfn=bfn*m;
  16.       m=m-1;
  17.                   cout<<bfn<<endl;
  18.      }
  19.   cout<<bfn<<endl<<endl;
  20.  }
  21.  
Nov 14 '07 #7
oler1s
671 Recognized Expert Contributor
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.
Nov 14 '07 #8
y8ycgl
12 New Member
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!
Nov 14 '07 #9
y8ycgl
12 New Member
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
}
Nov 14 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1399
by: Mahuskin | last post by:
Using Microsoft Visual C++ .NET 69462-270-0000007-18921 When I build the following code in Visual C++ .NET for release, there is a serious optimization error that corrupts the resulting value double m1 = 1.05 m1 = 1.0/m1 In release image, m1 is much smaller than 1e-9 In debug image, m1 is correct
2
2222
by: Hamish Dean | last post by:
Hi. To explain this, i'll list some relevant info: Prog1: A c++ dll, written by me. Prog2: A c++ dll, written by a 3rd party. Prog3: A c++ application, written by me. Prog4: A delphi 7 application, written by me. Prog5: A delphi 4 application, written by someone else. Prog1 calls a function from Prog2:
3
3796
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that programmers who don't range- check their data can e.g. multiply two very tiny values and end up with a very large one. This is (AFAICT) quite fine according to the Standard but is certainly a QoI issue and I'd like to raise ours. I believe that it's...
13
7783
by: tings | last post by:
An article states: "In floating point maths, where if you divide by a sufficiently large number sufficiently often, you will always be able to reach a value too small to distinguish from zero, given the finite precision you have." What happens when "a value too small to distinguish from zero" is reached by the above division? Underflow? What does it mean "sufficiently often" since only one division should be enough to cause underflow...
2
26249
by: Steven Jones | last post by:
Im just learning C an do not understand why the following code fails. The while loop apperas to work fine until it bombs out with a floating point exception. Whats going on? Thanks /* * chapter 5, program 7 * Calculate GCD of two numbers.
13
4148
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain amount of floating point work was approximately. To attempt to do this I made a program that...
3
10739
by: jer006 | last post by:
Hi I am writing a select statement that has an arithmetic function inside a case statement that uses logic to decide whether to divide or multiply and when I run the arithmetic statements outside the case statement they work fine, but blow up with an overflow error in the case statement. The select looks like: SELECT a.acct_cd, a.crrncy_cd, a.mkt_val, c.rate,
15
8059
by: Mukesh_Singh_Nick | last post by:
Why does floating point have a rounding error? How to work around it? For example, the following: flaot f = 1234.12345678F; printf("%2f\n", f) //prints 1234.123413 and
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.