Results 1 to 10 of 10
Thread: Why does it say I have an error?
 03092013, 11:59 PM #1Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Why does it say I have an error?
I'm suppose to do this without recursion but when I compile my code it says:
Java 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:
Java 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); } }
Last edited by wheehoowaffles; 03102013 at 12:02 AM.
 03102013, 12:22 AM #2Senior Member
 Join Date
 Oct 2010
 Location
 Germany
 Posts
 787
 Rep Power
 5
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...
 03102013, 12:49 AM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,514
 Rep Power
 5
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.
Java 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); }
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03102013, 01:38 AM #4Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Re: Why does it say I have an error?
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:
Java Code:Factorial4.java:55: error: cannot find symbol product *= i; ^ symbol: variable i location: class Factorial4 1 error
Java 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); } }
 03102013, 02:33 AM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,514
 Rep Power
 5
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03102013, 03:45 AM #6Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Re: Why does it say I have an error?
I still don't get. I'm sorry for being so stupid.
 03102013, 04:57 AM #7Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,514
 Rep Power
 5
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03102013, 10:02 PM #8Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Re: Why does it say I have an error?
This is recursion right?
Java 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); } }
Factorial of 13 is 6227020800.
e is approximately 2.718282.
e to the 13th term is approximately 1.000124.
 03102013, 11:14 PM #9Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,514
 Rep Power
 5
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03102013, 11:23 PM #10Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Similar Threads

java.lang.NullpointerException error in tomcat 5.5 / http status 500 error
By rahil in forum Apache CommonsReplies: 3Last Post: 05082012, 05:26 PM 
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03102010, 01:53 PM 
Thread: Error 500Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12052009, 07:28 AM 
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01232009, 08:53 AM
Bookmarks