Results 1 to 4 of 4
 03152013, 09:35 PM #1Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Need help with this formula. e^x
I'm asked to write a function for e^x and my teacher said to write it using only int. The error says it found double but I don't know how to fix this since I'm new to java.Please & thank you for your help.
error
Functions.java:33: error: possible loss of precision
ex = ex + 1 + (Math.pow(x, counter) / fact);
^(under plus sign)
required: int
found: double
1 error
error found in here
Java Code:int eulerx( int x ) { int ex = 1; int fact = 1; for (int count = 1; count <= x; count++) for (int i = 1; i <= x; i++) { fact *= i; ex = ex + 1 + (Math.pow(x, countx) / fact); } return ex; }
it works if I change is like this but he wants it all in int
Java Code:double eulerx( int x ) { double ex = 1; int fact = 1; for (int count = 1; count <= x; count++) for (int i = 1; i <= x; i++) { fact *= i; ex = ex + 1 + (Math.pow(x, count) / fact); } return ex; }
Functions code
Java Code:class Functions { public Functions( ) { } long factorial(long n) { long f = 1; for (long i = 1; i <= n; i++) { f *= i; } return f; } int euler( int n) { int e = 1; int fact = 1; for (int i = 1; i <= n; i++) { fact *= i; e = e + 1 / fact; } return e; } int eulerx( int x ) { int ex = 1; int fact = 1; for (int count = 1; count <= x; count++) for (int i = 1; i <= x; i++) { fact *= i; ex = ex + 1 + (Math.pow(x count) / fact); } return ex; } }
FunctionsTest code
Java Code:class FunctionsTest { public static void main(String [ ] args) { Functions f = new Functions( ); Functions e = new Functions( ); Functions ex = new Functions( ); long result; int ee; int eex; result = f.factorial(13); ee = e.euler(13); eex = ex.eulerx(13); System.out.println( ); System.out.println( "Factorial of 13 is "+ result + "." ); System.out.println( ); System.out.println( "e is approximately "+ ee + "." ); System.out.println( ); System.out.println( "e to the 13th power is "+ eex + "." ); System.out.println( ); } }
Last edited by wheehoowaffles; 03152013 at 10:33 PM.
 03152013, 10:44 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,610
 Rep Power
 5
Re: Need help with this formula. e^x
Can you please be more specific. e is a floating point number (i.e. double or float). It cannot be represented as an int. When your teach said you could only use int, what did s/he mean exactly?
Regards,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03152013, 11:38 PM #3Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Re: Need help with this formula. e^x
he said to only use int not double
 03162013, 12:01 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 3,610
 Rep Power
 5
Re: Need help with this formula. e^x
I must assume your teacher meant that the exponent of e (x in this case) must be an int. So e, and 1/fact must be a double. Since fact is an int, 1/fact will always evaluate to 0 for fact > 1. And your Math.pow method call is missing a comma.
Regards,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
Similar Threads

Help me I get this error: Error: Main method not found in class Position
By lo2 in forum New To JavaReplies: 8Last Post: 11222012, 07:16 AM 
java error incompatible types found int required boolean
By Code Oranj in forum New To JavaReplies: 1Last Post: 07122012, 10:17 PM 
main method not found error in class . wat s the error?
By kirankumaragnihotram in forum New To JavaReplies: 10Last Post: 06292012, 11:26 AM 
Array Required, but java.lang.String found
By raider00321 in forum New To JavaReplies: 5Last Post: 11282010, 02:06 PM 
Error: incompatible types, found: int required: boolean
By silvia in forum New To JavaReplies: 6Last Post: 10082008, 08:09 AM
Bookmarks