赞
踩
- public class Test18 {
- public static void main(String[] args) {
- System.out.println(mySin(Math.PI / 2));
- System.out.println(myCos(Math.PI * 87 / 180));
- }
-
- public static double mySin(double x) {
- double sin;
- double sum = 0;
- for (int n = 0; ; n++) {
- double pow1 = Math.pow(-1, n);
- double pow2 = Math.pow(x, 2 * n + 1);
- int f = factorial(2 * n + 1);
- sin = (pow1 * pow2) / f;
- if (Math.abs(sin) < 0.00001) {
- break;
- }
- sum += sin;
- }
- return sum;
- }
-
- public static double myCos(double x) {
- double cos;
- double sum = 0;
- for (int n = 0; ; n++) {
- double pow1 = Math.pow(-1, n);
- double pow2 = Math.pow(x, 2 * n);
- int f = factorial(2 * n);
- cos = (pow1 * pow2) / f;
- if (Math.abs(cos) < 0.00001) {
- break;
- }
- sum += cos;
- }
- return sum;
- }
-
- //阶乘
- public static int factorial(int num) {
- int sum = 1;
- if (num == 0) {
- sum = 1;
- }
- for (int i = 1; i <= num; i++) {
-
- sum *= i;
- }
- return sum;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。