# Why does it say I have an error?

• 03-10-2013, 12:59 AM
wheehoowaffles
Why does it say I have an error?
I'm suppose to do this without recursion but when I compile my code it says:
Code:

```Factorial4.java:54: error: possible loss of precision                             ex = 1  + (Math.pow(count, counter) / product);                                     ^   required: long   found:    double 1 error```

I'm asked to do this for my assignment

a) Write an application that reads a nonnegative integer and computes and prints its factorial.
b) Write an application that estimates the value of the mathematical constant e.
c) Write an application that computes the value of e^x. (i'm trying to do e^13)

my code:
Code:

```public class Factorial4 {   public static void main( String [] args) {         fact(13);         }         public static void fact(long num) {                 int product = 1;         for( long j = 2; j <= num; j++)         product *= j;         System.out.print("Factorial of " + num + " is " + product + ".");         System.out.println();         } //// part b ////         public static void Main( String [] args) {         euler(13);         }                 public static void euler(long num) {                 long e = 1;         long product;         System.out.printf("e is approximately ", e);         for(long i = 1; i <= 13; i++) {         product = 1;         for(long j = 13; j > 0; j++) {         product *= j;         }         e += 1/product;         }         System.out.printf("%f ", e);         }         //// part c ////         public static void MAIN( String [] args) {         ex(13);         }                 public static void ex(long num) {         long ex = 1;         long product;         System.out.printf("\n\n e to the 13th term is approximately ");         for (long count = 1; count <= 13; count++)         for (long counter = 1; counter <= 13; counter++) {                             ex = 1  + (Math.pow(count, counter) / product);                 }         System.out.printf("%f \n\n", ex);         } }```
All this in one single class and I'm not sure if I'm doing it right. This is my first time in a java class and I would really appreciate if you can tell me what is wrong. Thanks in advance.
• 03-10-2013, 01:22 AM
eRaaaa
Re: Why does it say I have an error?
- Math.pow returns a double value, ex is delcared as long, so you have to change it to double or you have to cast the result to long
- product isn`t initialized in public static void ex...
• 03-10-2013, 01:49 AM
jim829
Re: Why does it say I have an error?
You have a number of problems here. I will point out the ones in this method.

Code:

```public static void euler(long num) {        long e = 1;     long product;       System.out.printf("e is approximately ", e);       for(long i = 1; i <= 13; i++) {           product = 1;           for(long j = 13; j > 0; j++) {                 product *= j;           }           e += 1/product;     }     System.out.printf("%f ", e); }```
Note: Fixing some of these will may others go away.

First, you are passing an argument num and not using it in the method.
Second, e and product are both longs thus 1/e will always print 0 after it is evaluated since no decimals are involved.
Third, your inner loop will loop forever because your increment should be j-- (it would be clearer if you would
stick with the format of the outer loop.
Four, since product always computes to the same value, there is no reason to keep recalculating it. Also, You may not be calculating e correctly anyway (but I haven't checked for certain).

Regards,
Jim
• 03-10-2013, 02:38 AM
wheehoowaffles
Re: Why does it say I have an error?
Quote:

Originally Posted by jim829
You have a number of problems here. I will point out the ones in this method.

Code:

```public static void euler(long num) {        long e = 1;     long product;       System.out.printf("e is approximately ", e);       for(long i = 1; i <= 13; i++) {           product = 1;           for(long j = 13; j > 0; j++) {                 product *= j;           }           e += 1/product;     }     System.out.printf("%f ", e); }```
Note: Fixing some of these will may others go away.

First, you are passing an argument num and not using it in the method.
Second, e and product are both longs thus 1/e will always print 0 after it is evaluated since no decimals are involved.
Third, your inner loop will loop forever because your increment should be j-- (it would be clearer if you would
stick with the format of the outer loop.
Four, since product always computes to the same value, there is no reason to keep recalculating it. Also, You may not be calculating e correctly anyway (but I haven't checked for certain).

Regards,
Jim

Thanks. How would I calculate e?
I have done this assignment already but my professor said to do it without recursion & I'm having trouble doing that.
I changed somethings and now I get this error:

Code:

```Factorial4.java:55: error: cannot find symbol                         product *= i;                                   ^   symbol:  variable i   location: class Factorial4 1 error```

Code:

```public class Factorial4 {   public static void main( String [] args) {         fact(13);         }         public static void fact(long num) {                 long product = 1;         for( long j = 2; j <= num; j++)         product *= j;         System.out.print("Factorial of " + num + " is " + product + ".");         System.out.println();         } //// part b ////         public static void Main( String [] args) {         euler(13);         }                 public static void euler(long num) {                 double e = 1.0;         double product;         System.out.printf("e is approximately ", e);         for(double i = 1; i <= 13; i++) {         product = 1;         for(double j = num; j > 0; j--) {         product *= j;         }         e += 1/product;         }         System.out.printf("%f ", e);         }         //// part c ////         public static void MAIN( String [] args) {         ex(13);         }                 public static void ex(long num) {         double ex = 1;         long product;         System.out.printf("\n\n e to the 13th term is approximately ");         for (long count = 1; count <= 13; count++)         for (long counter = 1; counter <= 13; counter++) {                             ex = 1  + (Math.pow(count, counter) / product);                         product *= i;                 }         System.out.printf("%f \n\n", ex);         } }```
• 03-10-2013, 03:33 AM
jim829
Re: Why does it say I have an error?
Ok, for this answer, I will use ^ for exponentiation (in Java it is used for exclusive or).

e^x = 1 + x + (x^2)/2! + (x^3)/3! + (x^4)/4! +...+ (x^n)/n!

For just e, it is simplified to x = 1.

Regards,
Jim
• 03-10-2013, 04:45 AM
wheehoowaffles
Re: Why does it say I have an error?
I still don't get. I'm sorry for being so stupid.
• 03-10-2013, 05:57 AM
jim829
Re: Why does it say I have an error?
You're not stupid! However, you mention that you have done this with recursion. For those not familiar with programming, recursive procedures can be very challenging. So now you can do it using a more straight forward approach.

1. Clearly you need a factorial method. This just takes the product of the digits from 1 to some number n. Symbolically shown as n!.
2. You can use the Math.pow method to raise x to a power of y.
3. Then just put these in a loop and sum each term. Here is some pseudo code to do it

e = 0;
x = 1;
for j from 1 to n // n is arbitrary in this case
e = e + math.pow(x, j)/fact(j);

I don't know how much math you've had so remember than 0! = 1.

Regards,
Jim
• 03-10-2013, 11:02 PM
wheehoowaffles
Re: Why does it say I have an error?
This is recursion right?

Code:

```public class Fact {  public static long factorial( int i )     {         if( i <= 1 )                return 1;         else             return i * factorial( i - 1 );     }           public static void main( String [] args ) {         long factorial = 1;         double e = 1.0;         double ex = 1.0; //// part a ////                 for (int i = 13; i <= 13; i++) {                 System.out.println();                        System.out.println( "Factorial of " + 13 + " is " + factorial (13) + "." );                 } //// part b ////                 System.out.printf("e is approximately ", e);                 factorial = 1;                       for (int i = 1; i <= 20; i++) {                           factorial = factorial * i;                           e = e + 1.0 / factorial;                       }                       System.out.printf("%f.", e); //// part c ////                 System.out.printf("\ne to the 13th term is approximately ");                 for (int count = 1; count <= 13; count++)                 for (int counter = 1; counter <= 13; counter++) {                             ex = 1  + (Math.pow(count, counter) / factorial);                 }                 System.out.printf("%f.\n\n", ex);   } }```
Output:

Factorial of 13 is 6227020800.
e is approximately 2.718282.
e to the 13th term is approximately 1.000124.
• 03-11-2013, 12:14 AM
jim829
Re: Why does it say I have an error?
Everything is correct except e to the 13th term (which I believe you mean e to the 13th power). You need to sum up ex but you just replace ex. You need to say ex = ex + .....

Regards,
Jim
• 03-11-2013, 12:23 AM
wheehoowaffles
Re: Why does it say I have an error?
Ok thanks.