Java: sin/cos/tan/ln/other precision (for double)?
-
01-07-2021 - |
Вопрос
Those functions usually give the irrational number for a result, and are probably calculated via taylor series but is there any documentation of how many times are those series repeated?
I'm thinking if this is sin:
public double sin(double x,int n){
double res = 0;
for (int i=0;i<n;i++){
res+=Math.pow(x,i)/this.factorial(x);
}
return res;
}
what should the n
be?
I need this because I need to create Math functions myself so I should know how many times should I repeat these operations.
thanks
Решение
Calculating pow
and factorial
this way is very expensive and prone to rounding error. BTW I think you mean factorial(i)
not (x)
public static double sin(double x, int n) {
double result = x;
double term = x;
for (int i = 3, n2 = n * 2; i <= n2; i += 2) {
term *= -x * x / ((i - 1) * i);
result += term;
}
return result;
}
public static void main(String... args) {
/*
for (int i = -20; i <= 20; i++) {
double d = i / 10.0;
System.out.println(Math.sin(d) + " vs " + sin(d, 14));
}
*/
double d = -1.5;
double sin = Math.sin(d);
System.out.println("Searching for sin(" + d + ") = " + sin);
for (int n = 2; n <= 14; n++) {
System.out.println(n + ": " + sin + " vs " + sin(d, n) + " err: " + (sin(d, n) - sin));
}
}
prints
Searching for sin(-1.5) = -0.9974949866040544
2: -0.9974949866040544 vs -0.9375 err: 0.059994986604054446
3: -0.9974949866040544 vs -1.00078125 err: -0.00328626339594551
4: -0.9974949866040544 vs -0.9973911830357143 err: 1.0380356834016613E-4
5: -0.9974949866040544 vs -0.9974971226283482 err: -2.1360242937751295E-6
6: -0.9974949866040544 vs -0.9974949556821353 err: 3.092191913633968E-8
7: -0.9974949866040544 vs -0.9974949869361672 err: -3.321127817201841E-10
8: -0.9974949866040544 vs -0.9974949866013026 err: 2.751798788835913E-12
9: -0.9974949866040544 vs -0.9974949866040727 err: -1.8207657603852567E-14
10: -0.9974949866040544 vs -0.9974949866040544 err: 0.0
11: -0.9974949866040544 vs -0.9974949866040546 err: -1.1102230246251565E-16
12: -0.9974949866040544 vs -0.9974949866040546 err: -1.1102230246251565E-16
13: -0.9974949866040544 vs -0.9974949866040546 err: -1.1102230246251565E-16
14: -0.9974949866040544 vs -0.9974949866040546 err: -1.1102230246251565E-16
Другие советы
For Java 6, the Math
library delegates several of its calls (including cos
) to StrictMath
, which says in the class documentation:
These algorithms are available from the well-known network library netlib as the package "Freely Distributable Math Library," fdlibm.
So I Googled for fdlibm, and then took a look at k_cos.c
, and the implementation there approximates with a degree 14 polynomial.
Just look at the documentation of the Math class. It states that the maximum error is one ulp, being an ulp
For a given floating-point format, an ulp of a specific real number value is the distance between the two floating-point values bracketing that numerical value
For the sin, the error is 1 ulp.
The basic idea is that you keep repeating until the double doesn't change anymore because the precision you add is too small to represent in a double.