Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.Console;
- public class Main
- {
- public static void main(String[] args)
- {
- if(args.length != 2)
- {
- System.err.println("Invalid number of arguments");
- System.exit(1);
- }
- else
- {
- double x;
- double k;
- String error = "Invalid argument! You entered: ";
- try {
- x = Double.parseDouble(args[0]);
- }
- catch (IllegalArgumentException e) {
- System.err.println(error + args[0]);
- x = 1;
- }
- try {
- k = Integer.parseInt(args[1]);
- }
- catch (IllegalArgumentException e) {
- System.err.println(error + args[1]);
- k = 2;
- }
- if ( k <= 1 )
- {
- System.err.println("Invalid argument: " + k);
- System.exit(1);
- }
- double Eps = 1 / Math.pow(10, k+1);
- double result = 0;
- double step = 1;
- int n = 1;
- while (Math.abs(step) >= Eps)
- {
- result += step;
- step = (step * x * x) / (n * (n + 1));
- n += 2;
- }
- String fmt = "%10." + k + "f\n";
- System.out.print("My Taylor: ");
- System.out.printf( fmt, result );
- System.out.printf( fmt, Math.cosh(x));
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement