C/C++ What is causing my C++ program to truncate Euler's number?

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion centers on a C++ program designed to calculate Euler's number, which is experiencing issues with decimal truncation. The primary problem identified is that the division operation in the expression `1/factorial(i)` performs integer division, leading to loss of precision. To resolve this, it is recommended to use `1.0` instead of `1` to ensure floating-point division. Additionally, switching from `float` to `double` is suggested for improved precision. Participants also recommend using the `+=` operator for more concise code and suggest minor improvements in code readability through better indentation. A note is made about the limitations of factorial calculations, highlighting that values like `25!` exceed the range of standard data types, which can lead to truncation issues when adding terms to the sum. A potential workaround is to reverse the loop order and utilize 64-bit integers for factorial calculations, although this may not significantly enhance the program's accuracy.
Superposed_Cat
Messages
388
Reaction score
5
Hi, just started c++ and I made this program to calculate the value of eulers number. It works in c# but truncates the decimal points as far as I can tell. What am I doing wrong?
Code:
#include <iostream>

using namespace std;
int factorial(int a){
    int b=1;
while(a>0){
    b=b*a;
    a--;
    
    }
    return(b);
}

int main()
{
 float x=1.0;
 for(int i=1;i<25;i++){
     x=x+1/factorial(i);
 }
   cout <<x; 

   return 0;
}
 
Technology news on Phys.org
factorial is an integer function; cast it to float ...
 
  • Like
Likes 1 person
The 1/factorial will do integer division. You need to use 1.0 to get floating pt division.
 
  • Like
Likes 1 person
Thanks. Those must be the fastest responses I've ever had.
 
For more precision, use double instead of float.

Also, many programmers would write the assignment statement in the for loop as
Code:
x += 1.0/factorial(i);

You can do something similar in the code in your factorial function.

Your indentation could be tweaked a bit to make your code more readable. On the plus side you used code tags - good!

Here's how I would indent this code. I have also added more spaces
Code:
#include <iostream>

using namespace std;

int factorial(int a)
{
    int b = 1;
    while(a > 0){
       b *= a;
       a--;
    
   }
   return(b);
}

int main()
{
   float x = 1.0;
   for(int i = 1; i < 25; i++){
      x += 1.0/factorial(i);
   }
   cout <<x; 

   return 0;
}
 
Last edited:
  • Like
Likes 1 person
noticed that after posting.(the += thing the double I completely forgot about, thanks)
 
Note that 25! is greater than 2^83. 18! is greater than 2^52, so truncation will occur at i >= 18 when adding 1/i! to a double >= 2.0. You could reverse the order of the loop (for i = 20; i >= 1; i--) and use 64 bit integers for factorial (20! < 2^62), but it wouldn't make much difference.
 
Last edited:

Similar threads

Replies
22
Views
3K
Replies
13
Views
2K
Replies
5
Views
3K
Replies
40
Views
3K
Replies
1
Views
1K
Replies
6
Views
1K
Replies
39
Views
4K
Back
Top