0

I'm trying to calculate the value of e using the following Java code:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        
        int n;
        
        System.out.print("number of values: ");
        n = scnr.nextInt();
        
        double e = 0.0;
        double f = 1.0;
        for (int i = 1; i <= n; i++) {
            f = f * (i);
            e += 1 / f;
        }
        e += 1;
        System.out.print(e);
    }
}

However, when I print out e, the number it limited to 2.7182818284590455 instead of a much more specific number (2.7182818284590455 x 10^-308 or something similar). Is it a problem with the Types I'm using?

James
  • 25
  • 4
  • How big is your n, your max index of iteration? – DontKnowMuchBut Getting Better Oct 14 '20 at 21:15
  • Double. MAX_VALUE is the maximum value a double can represent (somewhere around 1.7*10^308). https://stackoverflow.com/questions/16146219/java-double-max-value – DarrenChand Oct 14 '20 at 21:16
  • 1
    Please consider using [BigDecimal](https://www.geeksforgeeks.org/bigdecimal-class-java/) if you find your current variable's size limit too small. – Tim Hunter Oct 14 '20 at 21:21
  • Please also keep in mind, BigDecimal can only go as large as your computer's RAM can handle. You will have to look into methods to push your calculations out to secondary storage if the amount you want to track exceeds this hardware limit. – Tim Hunter Oct 14 '20 at 21:30
  • 170 is the max index of iteration. – James Oct 14 '20 at 21:31
  • Wanted to edit this into my last comment, but was too slow. You may want to consider checking your configurations and [allocate more RAM](https://stackoverflow.com/questions/2294268/how-can-i-increase-the-jvm-memory) before looking at secondary memory options. JVM [by default only uses up to 1/4 of RAM](https://stackoverflow.com/questions/37783246/jvm-how-much-ram-should-i-allocate) so there's room for improvement if needed. – Tim Hunter Oct 14 '20 at 21:39
  • Where do you get 10^-308? That would be way too small for e. – NomadMaker Jan 17 '21 at 15:44

1 Answers1

1

From this answer:

The number of decimal places in a double is 16.


I can't see how you expect to get 2.7182818284590455 x 10^-308:

0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000027182818284590455

That's a very small fraction!

What you're doing is adding an increasingly smaller fraction to e. Based on your code, you can only expect the result to be between 2.0 and 3.0.


What you probably were looking for was a precision of more than 16 decimal places. That simply can't be achieved using double due to precision limitations.

Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45