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

  • Context: C/C++ 
  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around a C++ program intended to calculate Euler's number, focusing on issues related to truncation of decimal points in the output. Participants explore potential causes of this truncation, including data types and division methods, while providing suggestions for code improvements.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the factorial function returns an integer, suggesting it should be cast to float to avoid truncation.
  • Another participant points out that using 1/factorial will result in integer division, recommending the use of 1.0 to ensure floating-point division.
  • A suggestion is made to use double instead of float for increased precision in calculations.
  • One participant mentions that the assignment statement in the loop could be written as x += 1.0/factorial(i) for clarity.
  • A later reply highlights that truncation will occur at i >= 18 due to the limitations of floating-point representation, suggesting a reversal of the loop order and the use of 64-bit integers for factorial calculations.

Areas of Agreement / Disagreement

Participants express multiple competing views regarding the best approach to avoid truncation, and the discussion remains unresolved as different methods and their implications are considered.

Contextual Notes

Limitations include the dependence on the choice of data types (float vs. double) and the potential for truncation due to the size of factorial values in relation to floating-point representation.

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   Reactions: 1 person
The 1/factorial will do integer division. You need to use 1.0 to get floating pt division.
 
  • Like
Likes   Reactions: 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   Reactions: 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 ·
Replies
22
Views
4K
Replies
12
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 40 ·
2
Replies
40
Views
4K