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

1. Member
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; 03-15-2013 at 11:33 PM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,930
Rep Power
10

## 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

3. Member
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

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,930
Rep Power
10

## 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•