# Someone help please? Why does the error say it found double & required int?

• 03-15-2013, 10:35 PM
wheehoowaffles
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

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

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

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

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( );         } }```
• 03-15-2013, 11:44 PM
jim829
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,
Jim
• 03-16-2013, 12:38 AM
wheehoowaffles
Re: Need help with this formula. e^x
he said to only use int not double
• 03-16-2013, 01:01 AM
jim829
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,
Jim