#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    E the Mathematical Constant


    E = 1 + 1/1! + 1/2! + 1/3!...+ 1/i!

    This code, then, should return e at i = 10000, 20000, 30000, 40000, etc. until i=100000.

    For some reason, it returns only calculates 2.0 at those junctures. What's going on? Following the code both manually and by alteration through i=3 should give 2.5 + 1/6, or ~2.6. Unfortunately, it still gives 2.0. What's going on, is something rounding off continually? If so, where?

    import java.io.*;
    import java.util.*;

    public class E_Constant
    {
    public static void main(String[] args)
    {
    double e = 1;
    double item = 1;
    int i;

    for(i=1; i <= 100000; i++)
    {
    item = item * (1/i);
    e = e + item;
    if (i%10000 == 0)
    {
    System.out.println(e);
    }
    }
    }
    }
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    Diving two integers is an integer division in Java. For example,
    Code:
    1 / 2
    yields 0.

    That's probably the most popular error in Java. A simple solution is to use 1.0 instead of 1.

    But the double datatype is much too inaccurate to do calculations of this scale, anyway. The limit is already reached at the 17. iteration, while you want to go up to 100,000. You'll need the BigDecimal class for this and specify an appropriate accuracy.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    Phooey, of course!

    However, double seems to work well.

    There's no difference between the displayed result for i = 10000 and for i = 100000, but that's probably a matter of overwhelming precision. Thankfully, that means I won't have to read through all that documentation (yet)!
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by desert
    There's no difference between the displayed result for i = 10000 and for i = 100000, but that's probably a matter of overwhelming precision.
    Like I said.



    Originally Posted by desert
    Thankfully, that means I won't have to read through all that documentation (yet)!
    Then what's the point of the code? It doesn't do anything after the 17. iteration except keeping the CPU busy. So you might as well stop there.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo