473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting Floating point: overflow error in factorial code

12
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 9390
Ganon11
3,652 Expert 2GB
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
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
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 Expert 2GB
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
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
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 Expert 512MB
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
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
curent updated version:
[code]
#include<iostream.h>
#include<iomanip.h>
#include<iostream>
#include<iomanip>

int main ()
{
long double bfn=0;
long int m=0;
char pause;
cout<<setiosflags(ios::fixed|ios::showpoint|ios::r ight)<<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;
return 0
}
Nov 14 '07 #10
oler1s
671 Expert 512MB
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.
Nov 15 '07 #11
oler1s
671 Expert 512MB
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.
Nov 15 '07 #12
y8ycgl
12
i see... so i would need a more advanced compiler to get a bigger number? or a better computer?
Nov 15 '07 #13
Ganon11
3,652 Expert 2GB
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.
Nov 15 '07 #14
y8ycgl
12
hmm.... ok then..... how would i go about a vector/array?
Nov 15 '07 #15
Spoonfeeding removed, as well as links
Nov 20 '07 #16
y8ycgl
12
well i did take a look at it before you took it off.... but it didnt help. at all. it was compleat and total giberish to me. i dont want somone to tell me the code and expect me to understand. i want to know what im doing AS I DO IT........peace by peace.
Nov 20 '07 #17
JosAH
11,448 Expert 8TB
well i did take a look at it before you took it off.... but it didnt help. at all. it was compleat and total giberish to me. i dont want somone to tell me the code and expect me to understand. i want to know what im doing AS I DO IT........peace by peace.
3000! takes 3000/5+3000/25+3000/625 ~ 724 trailing zeros and a lot of digits to
the left of it. Use a big integer class for that because native types just can't handle it.

kind regards,

Jos
Nov 20 '07 #18
NeoPa
32,556 Expert Mod 16PB
well i did take a look at it before you took it off.... but it didnt help. at all. it was compleat and total giberish to me. i dont want somone to tell me the code and expect me to understand. i want to know what im doing AS I DO IT........peace by peace.
I think you need to consider the tone of your posts very carefully before posting again.

Shouting your demands at people who give up their valuable time to try to get some basic understanding into you, even though by, all appearances, you are unwilling or unable to grasp what you're being told, is not acceptable behaviour on this site.

I appreciate your frustration, but it is clear (even to me - no .NET expert) that you are not paying enough attention to what is being said and simply want your problem solved for you. If you read what has been said and try to grasp the significance, you will better understand the very helpful responses you are being offered.

Please don't cause me to take this matter further.

ADMINISTRATOR.
Nov 23 '07 #19
JosAH
11,448 Expert 8TB
Just to close the case now: (stand back here come a whole lot of digits):

41493596034378540855568670930866121709511191949318 09917689467657697558565123531950086000765217800342 00751846353836171184957508711140459077945534021610 68339611621037904199177522062663390179682805164719 69749596884245772876609710300372611109534024112711 88331577388153284389297376130211063129303744014853 78725446079610290429491049793888120762511625132917 00464166896211759020357517548898065357786891528509 37824699946746991908320935110683638242870635222685 44339213775150488588104036818809099292912497141900 50893899440471535147315453158744150996017426787508 74603679741170723687472771439889206836916185036081 98459718093784453523958505377611086511162363145920 88610855745087451394530543621371189815084719209442 63742032750299963337849440147756714146808242074999 14714878359669720638954670589960178569480263388767 11287106800495082740071712481947638640136919354435 41203127866014347925499591435301206531034066255032 31020738351502195103148673612338739395096551462159 34901578994994407231100442692483814014145548787273 80458560235615832043179459530558306933512468907212 46151468485308724031267967089113548982733475375756 89936517639642478173346251087901574343739892049226 70983170339321071763439833524445760404765654004144 14699479984354554597799386702839428513413188913165 69531084851352509400614777404700733140654179442800 44366919036854692708572717016480115120574524486079 68773784803660653009109815639091294110633715621540 90380013505867162426233390243416662871652122859027 45688335048979268693697928783768948414365738664369 55075473964882256222183380014600761196859217603234 80846745521633041173800433114422592624369055878291 49079738857587845857398286953903023838372658824276 54306437517757897215045071361801730051628424476294 22748575562782876349876719528136891358391882449928 47415916831303340321999467520829148857643458638323 13545205075955912062067273296951386122994658607527 31788445244986534816416923884488906149585093437344 28898148844273218171312725338915345065811438233812 05875379808605080889761753882896252933633750454549 16860026722959122552885458448268665532431301135375 48124095612376860780077007079395418489071494673778 54407528307872988103912945121929864793703451257436 44558145975714082270598632516535290658457112358527 02119334529811055683988098840949803461850780252730 38736784042169427237980464304250045030806637032760 01634192144280570880243085056789210864697745513953 91198386361671903002781463801369324823327715951805 96193069504237836082620570887209297929797429404576 87733831987744468554429480032174105668942371054502 88704196119150727390000316420144742133232938716180 29555614004602867400422885389854650328028428515122 29602879574180162182323609832097144104701253306731 48961532367887349845539496043970503523477662113959 14519270422122231426998692087463520980686224354813 37619439513194286811348653156222817321497648170538 18461553265961875302964786011608722636404439222576 01926494610916885151013143945574398303192557154162 15144246912237051914909786184943615096310993363959 45617965933968519586053386311763241470668422571923 94742531726479559749993283247279807896470753054014 19409020060971267475318636552540321275775785393069 75300565952082074574994718981444537722482078884433 35118545601568853708182892895218300139654376947286 41877666576281538973734015941054368143543734613424 46920670700827824236455574508825566701572427528103 17141640631410681384330924027281318960884813040665 22616955282563718386246494429568885939384672672369 41994755713205460182634257310291153535327288081827 73021596787088437293412117084511580629967697266601 66363527695996902150212210495425956727859318551626 84471003744346204220035353912037383930954206950214 86207390653190910821344334251497896284236198571674 77384812609744305503625086635472073097129808469719 65377227798931602005607250580075124074944481633922 14398118492748281978655178478547749198714138485042 29038395409057084203813727713566770356504108178052 06950321362335216927405310153409217618340788177356 74646749071616600653230438902639786065509005309872 43544568931560132994240711229501545377152105194244 55127953649712148722221937292891598330017423979775 92530501318837883494884232222507318816399438935627 81710287543258879455885774278039071716638125790379 81491484455268858716299310145107332155547732645760 35916184298708323237568837917135073006026738292294 68708103075194602037643813867710733377931258225735 64355345771628040304809257859097472334139329040722 39860005448269296110393640127539539899397420021925 26892862256495927913636954698324731449409429749421 32087169636628129638461913781146092107010330121199 34264941666449130310898493535366401831282683112506 57838642590653719701090727642933053475129733671692 94150478709492417781215349794994497323584451302100 29720359993576507730563696950539990891252004810120 09056963314436817919424796356338910248625077336724 93998017234516270488501494383437358264400534814749 57421328873648479589553843836378275601433377798816 12685446240649413441611910895265332676162766022113 08792116659243794965348380302360642949819855410143 11566601739518539426008673198564586684635442730180 02229260758976719219836752952836515871552188769831 79990058531215186910377766768836542912474198260994 34535671529412823837612115555686210454583810355154 40495371847072636321853277548650181100262133122842 98609261121595730660239320774767428009094626743221 38805290643067711276964013735906251051050623568241 31765153303077535897513456514742416740151747072083 91018699899932793649108926879247397058141528555439 65954222603919059265825637344676406359525838966981 51198395988660368375304201799032818594556941255051 90663028548695333776829846000318080938221300381022 14387057461181304251961916405970456035183121708151 65864735655654053292841174862895708285679230005352 58463770612805914520355463899321278759063496278379 75871352588618213252263577038396202737385324908353 68049799008570152248330343952519734465334299465256 52360967428345505237397339023742618088717992837222 85366293439240895762913154442106573609205481842139 36589386771554284247727510016673435774309363894844 45647643771840738743794710078671510704495546576262 81566137550730763768080600031844296233977808233311 35978757713698301281757162567168328728151193733668 57894371090977485812228681268241223172726811849752 07863453107495331708260153159440253645365524453587 95203474521342924891664450480435535228197772198197 18690548841768963987827047820661269214725486182478 59626434279190274503452994769367997217285165465591 79947178906788568727857447008428972377823476308074 09195129662383464278396538650173246658501921440916 94630371265581197700774682562035198318782913591013 99781730363517376470671438399281029122446084832051 89832483488551310255397215831849316536707322731729 95431750775475634748127320956655431851879586978172 49172170086576809890832783083824043773797445534252 56887128988555131809670124978594542906096273705906 59970784172738420721605576789060565167694565490120 38816577586193923092436298338954985727987452339809 04998584674848503995091093988342104246931136178759 78611803096108774362764990414655167545507613665725 91499337611434024376291029038413588853131259113254 48492258960071848511693901939854346494154837823383 02531368775990005443722332901462568184095998830522 52158532859983399033659541893269668016326589935823 46632470803240204297913574257554985493728961920916 50794671997121439832581553945835125648010889886887 05688271122262873403577241842480323117302733844222 06040156092420795694932049438094024655625303033288 24165302038006041288444384884189129393985971765670 21150161134012116935553586498480294156323827944757 63150426857342698631165628009321645781654104118990 78396210758605145091526528422433647230880469088426 41252512658472913405919517175429115262200222975698 69279591246209643630570521330992164222584376518891 93630329851223282950806126200573565554213183555838 28931813879594096230379277723034442343234156160355 85905023244752745026308698314141253963717544136118 97269158650716722308083435295578401087236027347001 11878614623318543943105705848377047480603500455688 50206027302222563976307389399850249781551826799169 94164145540329909813190506654358156657691529068908 18620413844409145635529124206490171743643047345519 13759229149532829881518087400767334869976953228714 50791584448703980405737673555777873593937891577147 95602334070845639231417011839255523461811977591567 33859559192652706240637342777602158465110353680579 63320714896942663358570375305829676608224208465464 55855666788922262761999026396179263745785165254091 87566085438596612219442487204249600000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0000000000000000000000000000000

kind regards,

Jos
Nov 23 '07 #20
NeoPa
32,556 Expert Mod 16PB
I'm sure digits #8,384 through #9,131 are correct Jos, but are you sure about digit #5,648 (000535258)?

PS. I think he may just get it after your post ;D
Nov 26 '07 #21
y8ycgl
12
@_@ hmmmm
so how would i begin writing a program with a vector/array? i dont quite know the code just yet. but if you do tell me a line of code i would also like to understand it, so please tell me what it means.......>.>
Nov 30 '07 #22
sicarie
4,677 Expert Mod 4TB
I would suggest researching vectors and arrays and deciding which one you want, as the datatypes behave differently. Pick the one you feel would be best served in your application, then look it up through Google.
Nov 30 '07 #23
y8ycgl
12
and NeoPa ..... HOW DID YOU FIND THAT? did you compare the entire number?............
Nov 30 '07 #24
NeoPa
32,556 Expert Mod 16PB
No. I was bluffing mainly. I'm sure Jos's answer is 100% accurate.
I have a text editor that shows what position it's at so counting the length of the number wasn't hard. With so many multiples of ten in the calculation it's obvious that there will be many zeroes at the end of the number.
Dec 1 '07 #25
y8ycgl
12
well that solves the problem.... but i thought i would have fun making this program. i havent much idea as to the code part of what im doing.
Dec 5 '07 #26
NeoPa
32,556 Expert Mod 16PB
If you ever learned long-multiplication at school, then that's the sort of process you need to be thinking of duplicating. String variables would be required to hold your working variables.
Dec 9 '07 #27

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

Similar topics

1
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 ...
2
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...
3
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...
13
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,...
2
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 /* *...
13
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...
3
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...
15
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.