# Thread: Something weird about a math function

1. Senior Member Join Date
Dec 2011
Posts
102
Rep Power
0

## Something weird about a math function

Hi,

I've been asked to write a program that calculates cos(x) value with it's Taylor equivalent.
I've done it in 2 ways: 1) a loop 2)Recursion.

The thing is, it gives me the right values for every X between 0 and 2pi (using radians), as long as I'm giving it the "n" value of 10. If I input let's say n=100, it gives me "NaN".
What's more weird, if I stay with n=10, but give it a value of X=3 (pi), it gives me a wrong answer :|

You can find the Taylor equivalent of cos(x) here: Taylor series - Wikipedia, the free encyclopedia

And here's my program:

Java Code:
```import java.util.Scanner;

public class RecursionEx2 {

public static void main(String[] args) {
Scanner input= new Scanner(System.in);

int n= input.nextInt();
double X= input.nextDouble();
loop(n,X);
System.out.println("Rec: "+rec(n,X));

}
public static void loop (int n, double X){
double res=0;
for(int i=0; i<=n; i++){
res+=((Math.pow(-1, i)*Math.pow(X, 2*i))/(factorial(2*i)));
}
System.out.println("Loop: "+res);

}

public static double rec (int n, double X){
if (n==0) return (Math.pow(-1, n)*Math.pow(X, 2*n)/factorial(2*n));
else return  ((Math.pow(-1, n)*Math.pow(X, 2*n)/factorial(2*n))+rec(n-1,X));
}

public static int factorial (int n){

if(n>1)return n*factorial(n-1);
else return 1;

}

}```
Thanks :)  Reply With Quote

2. ## Re: Something weird about a math function

Your factorial method overflows for (reasonably) large values of n.

kind regards,

Jos  Reply With Quote

3. Senior Member Join Date
Dec 2011
Posts
102
Rep Power
0

## Re: Something weird about a math function

Oh.. right!
Forgot how fast factorials grow :|
But that explains only the n problem.

What about X when it's larger than 2 (pi)?

Thanks!  Reply With Quote

4. ## Re: Something weird about a math function Originally Posted by Mapisto Oh.. right!
Forgot how fast factorials grow :|
But that explains only the n problem.

What about X when it's larger than 2 (pi)?

Thanks!
A Taylor expansion around zero (a McLaurin expansion actually) is quite inaccurate the further away you approximate a function value. Have a look at this link and pay special attention to the figure on the right side of the page. It is a Taylor expansion of the sine function. See how inaccurate it is around 2*pi.

kind regards,

Jos  Reply With Quote

5. Senior Member Join Date
Dec 2011
Posts
102
Rep Power
0

## Re: Something weird about a math function

I really needed this calculous refreshment :|
I get it now :)

Thanks man!  Reply With Quote

6. ## Re: Something weird about a math function Originally Posted by Mapisto I really needed this calculous refreshment :|
I get it now :)

Thanks man!
You're welcome of course; you could always try the Taylow expansion around pi if you want to know a value (a bit) larger than that; you do need to know the derivatives around pi (instead of zero). See the link I posted in a previous reply.

kind regards,

Jos  Reply With Quote

7. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Something weird about a math function

You're just stringing random words together now...and I'm not sure some of them aren't simply made up!  Reply With Quote

8. ## Re: Something weird about a math function Originally Posted by Tolls You're just stringing random words together now...and I'm not sure some of them aren't simply made up!
Yes, sometimes my brains go bzzzzt! and I start talking like that; don't tell my old mom about it ;-)

kind regards,

Jos  Reply With Quote

#### Posting Permissions

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