Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default 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 10:33 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,658
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Feb 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help with this formula. e^x

    he said to only use int not double

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,658
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 8
    Last Post: 11-22-2012, 07:16 AM
  2. Replies: 1
    Last Post: 07-12-2012, 10:17 PM
  3. main method not found error in class . wat s the error?
    By kirankumaragnihotram in forum New To Java
    Replies: 10
    Last Post: 06-29-2012, 11:26 AM
  4. Array Required, but java.lang.String found
    By raider00321 in forum New To Java
    Replies: 5
    Last Post: 11-28-2010, 02:06 PM
  5. Replies: 6
    Last Post: 10-08-2008, 08:09 AM

Posting Permissions

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