Welcome Guest ( Log In | Register )

Bump TopicReply to this topicRSS feed Start new topic Start Poll

Outline · [ Standard ] · Linear+

> Need help in C++, estimating the value of constant e (C++)

CallMeMonkey
post Aug 28 2009, 12:25 AM, updated 10y ago

On my way
****
Group: Senior Member
Posts: 506

Joined: Jun 2006
From: Jungle

Hi guys, im new to C++ and this is my first assignment

Basically the task is to estimate the value of the constant e

The hint given was
CODE
e = 1 + 1/1! + 1/2! + 1/3! ......+1/k!


I've came out with this code

CODE

#include <iostream>
#include <math>
void main()

{
int k;
double exp = 1;
double x = 1;
double i = 2;


cout << "Please insert the value of k: " << endl;
cin >> k;

while (k > 1)
{
exp = exp + ( 1/x );
x = x*i;
i ++;
k --;
}

cout << "the estimated value for constant e is : " << exp << endl;
}


The problem is now, I can only enter the value of k until 170 only. more than that the program will just hang there..

any idea what should i do? rclxub.gif

notworthy.gif

This post has been edited by CallMeMonkey: Aug 28 2009, 12:38 AM
megablue
post Aug 28 2009, 01:35 AM

Getting Started
**
Group: Junior Member
Posts: 199

Joined: Jan 2003
From: KL
perhaps data overflow happened?
alex_cyw1985
post Aug 28 2009, 01:59 AM

Regular
******
Group: Senior Member
Posts: 1,172

Joined: Jan 2003
From: Malacca



what is your i usage?

i ++; <?
silvestrelsl
post Aug 28 2009, 09:51 AM

Getting Started
**
Group: Junior Member
Posts: 222

Joined: Aug 2009
From: Cyberjaya


I ran your code at my laptop.
It works fine even the k value goes up to 2000.

What compiler you are using?
Is your computer is having a slow processor? Likely not... lol
tkhin
post Aug 28 2009, 12:06 PM

Getting Started
**
Group: Junior Member
Posts: 113

Joined: Mar 2006


smells like a recursive function?

double Factorial(const double& x)
{
if (x!=0)
return Factorial(x-1)*x;
else
return 1.0;

};

call the factorial func within ur exp approx. function...?

CallMeMonkey
post Aug 28 2009, 12:40 PM

On my way
****
Group: Senior Member
Posts: 506

Joined: Jun 2006
From: Jungle

QUOTE(megablue @ Aug 28 2009, 01:35 AM)
perhaps data overflow happened?
*
maybe.. anyway to fix this?
QUOTE(alex_cyw1985 @ Aug 28 2009, 01:59 AM)
what is your i usage?

i ++; <?
*
basically it adds 1 repetitively to my i (which initial is 2) as long as my k is more than 1.

QUOTE(silvestrelsl @ Aug 28 2009, 09:51 AM)
I ran your code at my laptop.
It works fine even the k value goes up to 2000.

What compiler you are using?
Is your computer is having a slow processor? Likely not... lol
*
I see.. maybe its my laptop problem or the compiler problem.

Im using borland C++ 5. as told by our lecturer

btw, what compiler are you using?
QUOTE(tkhin @ Aug 28 2009, 12:06 PM)
smells like a recursive function?

double Factorial(const double& x)
{
  if (x!=0)
   return Factorial(x-1)*x;
  else
   return 1.0;

};

call the factorial func within ur exp approx. function...?
*
Dont have any clue, havent learn this topic yet. so probably wont be using this. tongue.gif

This post has been edited by CallMeMonkey: Aug 28 2009, 12:41 PM
silvestrelsl
post Aug 28 2009, 01:30 PM

Getting Started
**
Group: Junior Member
Posts: 222

Joined: Aug 2009
From: Cyberjaya


I am using Visual C++ 6.0, does not seem like data overflow...
CallMeMonkey
post Aug 28 2009, 01:41 PM

On my way
****
Group: Senior Member
Posts: 506

Joined: Jun 2006
From: Jungle

okay

so this code has nothing wrong then

thanks, will check with my lecturer smile.gif
Find The Way
post Aug 28 2009, 08:02 PM

On my way
****
Group: Senior Member
Posts: 589

Joined: Nov 2004


Try to print out those variables' value or use a debugger, see if you can find any clue smile.gif
CallMeMonkey
post Aug 28 2009, 10:51 PM

On my way
****
Group: Senior Member
Posts: 506

Joined: Jun 2006
From: Jungle

QUOTE(Find The Way @ Aug 28 2009, 08:02 PM)
Try to print out those variables' value or use a debugger, see if you can find any clue smile.gif
*
thx for the idea.

i think i've figured out my problem..

the problem lies on my variable x.

CODE
double x = 1;


the maximum range of values type name 'double can hold is only up to 1.7E +/- 308

so for example, if i put my k = 171.
my x will do the calculation of (1x2x3x4x5x6....x171) which will exceed the maximum range of double (1.7E +/- 308)

any idea what should i do now? :/

changing the type value of double to int and float still wont solve my problem..

edit : i change my date type of x to long double. and it can compute until k = 1750. but the task needs us to use the value of k up to 10000. sweat.gif

This post has been edited by CallMeMonkey: Aug 28 2009, 11:07 PM
silvestrelsl
post Aug 30 2009, 07:04 PM

Getting Started
**
Group: Junior Member
Posts: 222

Joined: Aug 2009
From: Cyberjaya


You can try unsigned variable if your are sure that your value will never has negative value.

Or, you can try to use unsigned long long x, lol

Reference: http://www.cppreference.com/wiki/data_types

Toriton
post Aug 30 2009, 07:10 PM

Getting Started
**
Group: Junior Member
Posts: 274

Joined: Sep 2006
From: Camelot
why don't u use the for loop, see if still got problem...
eclectice
post Aug 31 2009, 01:44 PM

Look at all my stars!!
*******
Group: Senior Member
Posts: 2,344

Joined: Mar 2008
e =2.7182818284590455 at 66th iterations with loss of precision after 16th digit when using unsigned long double.
Actual result should be: 2.71828182845904523536... (Wikipedia)

At 67th iteration, overflow occurs for e.

If you use Windows Calculator:
QUOTE
1/64! = 1/1.2688693218588416410343338933516e+89
1/65! = 1/8.2476505920824706667231703067855e+90


But, when compiled using Visual C++ 2008, this program produces:
QUOTE
+ 1/64! = e[64] = 1/9.2233720368547758000000000000000e+018
+ 1/65! = e[65] = 1/9.2233720368547758000000000000000e+018
VC++ gives an erroneous result starting at 1/21!
QUOTE
1/21! = 1/1.4197454024290337000000000000000e+019


whereas Windows Calculator displays:
QUOTE
1/21! = 1/5.1090942171709440000e+019


The maximum limit in VC++:
QUOTE
unsigned long long (unsigned __int64) -> 18,446,744,073,709,551,615 or 1.8446744073709551615e+019


Interestingly, the binary form of IEEE-754 (1985) for the erroneous 21! = 14197454024290336768 in double-precision 64-bit format for VC++:
CODE

VC++   = 1 10001010000 0111011111010011011010111000110001000000000000000000 (0xC5077D36B8C40000)


The correct 21! = 51090942171709440000 in Windows Calculator:
CODE

WCalc  = 1 10001010000 0111011111010011011010111000110001000000000000000000 (0xC5077D36B8C40000)
Limit  = 1 11111111111 1111111111111111111111111111111111111111111111111111 (0xFFFFFFFFFFFFFFFF)


So, there is a bug in VC++ 2008 when interpreting IEEE-754 for 21! in decimal form, wheres it is correct in binary form!
Ok, the problem lies when passing variable from unsigned long long to unsigned long double; it causes IEEE-754 truncation!

To correct this, edit the original factorial() function as follows by making p and return call as unsigned long double:
CODE

ud factorial(ui n)
{
   ud p = 1;

   //note: 0! = 1, 1! = 1
   for(ui i = 1; i > 0 && i <= n; i++) {  p *= i;}
   return (p);
}


With the correction above, it is correct that factorial() overflows at 171st iteration!

Original code:
CODE

#include <iostream>
#include <iomanip>

#define ui unsigned long long
#define ud unsigned long double

using namespace std;

ui factorial(ui n)
{
   ui p = 1;

   //note: 0! = 1, 1! = 1
   for(ui i = 1; i > 0 && i <= n; i++) {  p *= i;}
   return (p);
}

//e = 1/0! + 1/1! + 1/2! + 1/3! +...+1/k!
int main()
{
   ui k = 66;
   ud e = 0.00;
   
   cout << setiosflags(ios_base::scientific) << setprecision(31);

   for(ui i=0; i < k; i++)
   {
       e += 1/(static_cast<ud>(factorial(i)));

       //debug purpose to verify correctness of factorials
       cout << "+ 1/"<< i << "! = e[" << i << "] = 1/";
       cout << static_cast<ud>(factorial(i)) <<  "\n";
   }
   //e =2.7182818284590455 at 66th iteration
   cout << "Final result:" << e <<"\n";

   return 0;
}


Original code output:
» Click to show Spoiler - click again to hide... «


This post has been edited by eclectice: Aug 31 2009, 05:21 PM
CallMeMonkey
post Sep 1 2009, 12:44 AM

On my way
****
Group: Senior Member
Posts: 506

Joined: Jun 2006
From: Jungle

thanks guys, just discussed it with my friend.

turns out that my program is buggy..

he suggested another code instead.

thanks to all who helped me, really appreciate it. learn quite a few new things about c++ notworthy.gif

This post has been edited by CallMeMonkey: Sep 1 2009, 12:57 AM
silvestrelsl
post Sep 1 2009, 10:00 AM

Getting Started
**
Group: Junior Member
Posts: 222

Joined: Aug 2009
From: Cyberjaya


Nice one, eclectice.
Quite an effort there to find out the root cause.
Thumb up for you.

Bump TopicReply to this topicTopic OptionsStart new topic
 

Switch to:
| Lo-Fi Version
0.0771sec    4.69    5 queries    GZIP Disabled
Time is now: 19th September 2018 - 06:37 PM