Results 1 to 10 of 10
Thread: Why does it say I have an error?
- 03-09-2013, 11:59 PM #1
Member
- 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:
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.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; 03-10-2013 at 12:02 AM.
- 03-10-2013, 12:22 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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, 12:49 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
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.
Note: Fixing some of these will may others go away.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
YAT -- Yet Another Typo
- 03-10-2013, 01:38 AM #4
Member
- 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); } }
- 03-10-2013, 02:33 AM #5
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
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
YAT -- Yet Another Typo
- 03-10-2013, 03:45 AM #6
Member
- 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.
- 03-10-2013, 04:57 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
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
YAT -- Yet Another Typo
- 03-10-2013, 10:02 PM #8
Member
- Join Date
- Feb 2013
- Posts
- 33
- Rep Power
- 0
Re: Why does it say I have an error?
This is recursion right?
Output: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.
- 03-10-2013, 11:14 PM #9
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
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
YAT -- Yet Another Typo
- 03-10-2013, 11:23 PM #10
Member
- 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: 05-08-2012, 05:26 PM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks