Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //qaLa
- public class Program {
- public final static double EPSILON = 1E-16;
- //EPSILON defined
- public static double faCtorial(double x){
- //factorial function
- double result = 1;
- for (double i = 1; i<=x; i++){
- result = result * i;
- }
- return result;
- }
- /**
- * @param x
- * @return
- */
- public static double sinh(final double x) {
- //sinh method
- double delta = x;
- double result = x;
- int i = 1;
- while (true){
- //always true, unless interrupted by break
- delta = Math.pow(x, 2*i+1) / faCtorial(2*i+1);
- result += delta;
- i++;
- if (Math.abs(delta) <= Math.abs(result)*EPSILON){
- //interrupts loop
- break;
- }
- }
- return result; //replaceD :)
- }
- public static void main(String[] args) {
- //modificated output
- System.out.println("sinh(0)=" + sinh(0.0));
- System.out.println("sinh(1)=" + sinh(1.0));
- System.out.println("sinh(-1)=" + sinh(-1.0));
- System.out.println("sinh(1000)=" + sinh(1000.0));
- //validation follows
- System.out.println();
- System.out.println("The following validates our results:");
- System.out.println("Validating the faCtorial method, inserting 5, result: "+ faCtorial(5));
- System.out.println("Value 1 must be: "+Math.sinh(0.0));
- System.out.println("Value 2 must be: "+Math.sinh(1.0));
- System.out.println("Value 3 must be: "+Math.sinh(-1.0));
- System.out.println("Value 4 must be: "+Math.sinh(1000.0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement