/****************************************************************************** * This program demonstrates the math library. * * Copyright © 2016 Richard Lesh. All rights reserved. *****************************************************************************/ import org.pureprogrammer.Utils; public class Math1 { public static void main(String[] args) { final double a = Math.PI / 6; final double b = Math.PI / 4; final double c = -a * 2; final double d = -b * 2; final double e = Math.E; System.out.println(Utils.format("pi = {0:f}", Math.PI)); System.out.println(Utils.format("e = {0:f}", Math.E)); // abs, floor, ceil, round, trunc, min, max System.out.println(Utils.format("abs({0:f}) = {1:f}", a, Math.abs(a))); System.out.println(Utils.format("abs({0:f}) = {1:f}", c, Math.abs(c))); System.out.println(Utils.format("floor({0:f}) = {1:f}", a, Math.floor(a))); System.out.println(Utils.format("floor({0:f}) = {1:f}", c, Math.floor(c))); System.out.println(Utils.format("ceil({0:f}) = {1:f}", a, Math.ceil(a))); System.out.println(Utils.format("ceil({0:f}) = {1:f}", c, Math.ceil(c))); System.out.println(Utils.format("round({0:f}) = {1:f}", a, Math.round(a))); System.out.println(Utils.format("round({0:f}) = {1:f}", c, Math.round(c))); System.out.println(Utils.format("trunc({0:f}) = {1:f}", a, a < 0.0 ? Math.ceil(a) : Math.floor(a))); System.out.println(Utils.format("trunc({0:f}) = {1:f}", c, c < 0.0 ? Math.ceil(c) : Math.floor(c))); System.out.println(Utils.format("min({0:f}, {1:f}) = {2:f}", a, c, Math.min(a, c))); System.out.println(Utils.format("max({0:f}, {1:f}) = {2:f}", a, c, Math.max(a, c))); // sin, cos, tan, atan, atan2, acos, asin System.out.println(Utils.format("sin({0:f}) = {1:f}", a, Math.sin(a))); System.out.println(Utils.format("sin({0:f}) = {1:f}", b, Math.sin(b))); System.out.println(Utils.format("sin({0:f}) = {1:f}", c, Math.sin(c))); System.out.println(Utils.format("sin({0:f}) = {1:f}", d, Math.sin(d))); System.out.println(Utils.format("cos({0:f}) = {1:f}", a, Math.cos(a))); System.out.println(Utils.format("cos({0:f}) = {1:f}", b, Math.cos(b))); System.out.println(Utils.format("cos({0:f}) = {1:f}", c, Math.cos(c))); System.out.println(Utils.format("cos({0:f}) = {1:f}", d, Math.cos(d))); System.out.println(Utils.format("tan({0:f}) = {1:f}", a, Math.tan(a))); System.out.println(Utils.format("tan({0:f}) = {1:f}", b, Math.tan(b))); System.out.println(Utils.format("tan({0:f}) = {1:f}", c, Math.tan(c))); System.out.println(Utils.format("asin({0:f}) = {1:f}", Math.sin(a), Math.asin(Math.sin(a)))); System.out.println(Utils.format("asin({0:f}) = {1:f}", Math.sin(b), Math.asin(Math.sin(b)))); System.out.println(Utils.format("asin({0:f}) = {1:f}", Math.sin(c), Math.asin(Math.sin(c)))); System.out.println(Utils.format("asin({0:f}) = {1:f}", Math.sin(d), Math.asin(Math.sin(d)))); System.out.println(Utils.format("acos({0:f}) = {1:f}", Math.cos(a), Math.acos(Math.cos(a)))); System.out.println(Utils.format("acos({0:f}) = {1:f}", Math.cos(b), Math.acos(Math.cos(b)))); System.out.println(Utils.format("acos({0:f}) = {1:f}", Math.cos(c), Math.acos(Math.cos(c)))); System.out.println(Utils.format("acos({0:f}) = {1:f}", Math.cos(d), Math.acos(Math.cos(d)))); System.out.println(Utils.format("atan({0:f}) = {1:f}", Math.tan(a), Math.atan(Math.tan(a)))); System.out.println(Utils.format("atan({0:f}) = {1:f}", Math.tan(b), Math.atan(Math.tan(b)))); System.out.println(Utils.format("atan({0:f}) = {1:f}", Math.tan(c), Math.atan(Math.tan(c)))); // 45 degrees System.out.println(Utils.format("atan2({0:f}, {1:f}) = {2:f}", 1.0, 1.0, Math.atan2(1.0, 1.0))); // 30 degrees System.out.println(Utils.format("atan2({0:f}, {1:f}) = {2:f}", 1.0, Math.sqrt(3.0), Math.atan2(1.0, Math.sqrt(3.0)))); // sinh, cosh, tanh, atanh, acosh, asinh System.out.println(Utils.format("sinh({0:f}) = {1:f}", a, Math.sinh(a))); System.out.println(Utils.format("sinh({0:f}) = {1:f}", b, Math.sinh(b))); System.out.println(Utils.format("sinh({0:f}) = {1:f}", c, Math.sinh(c))); System.out.println(Utils.format("sinh({0:f}) = {1:f}", d, Math.sinh(d))); System.out.println(Utils.format("cosh({0:f}) = {1:f}", a, Math.cosh(a))); System.out.println(Utils.format("cosh({0:f}) = {1:f}", b, Math.cosh(b))); System.out.println(Utils.format("cosh({0:f}) = {1:f}", c, Math.cosh(c))); System.out.println(Utils.format("cosh({0:f}) = {1:f}", d, Math.cosh(d))); System.out.println(Utils.format("tanh({0:f}) = {1:f}", a, Math.tanh(a))); System.out.println(Utils.format("tanh({0:f}) = {1:f}", b, Math.tanh(b))); System.out.println(Utils.format("tanh({0:f}) = {1:f}", c, Math.tanh(c))); System.out.println(Utils.format("tanh({0:f}) = {1:f}", d, Math.tanh(d))); System.out.println(Utils.format("asinh({0:f}) = {1:f}", Math.sinh(a), Math.log(Math.sinh(a) + Math.sqrt(Math.sinh(a) * Math.sinh(a) + 1.0)))); System.out.println(Utils.format("asinh({0:f}) = {1:f}", Math.sinh(b), Math.log(Math.sinh(b) + Math.sqrt(Math.sinh(b) * Math.sinh(b) + 1.0)))); System.out.println(Utils.format("asinh({0:f}) = {1:f}", Math.sinh(c), Math.log(Math.sinh(c) + Math.sqrt(Math.sinh(c) * Math.sinh(c) + 1.0)))); System.out.println(Utils.format("asinh({0:f}) = {1:f}", Math.sinh(d), Math.log(Math.sinh(d) + Math.sqrt(Math.sinh(d) * Math.sinh(d) + 1.0)))); System.out.println(Utils.format("acosh({0:f}) = {1:f}", Math.cosh(a), Math.log(Math.cosh(a) + Math.sqrt(Math.cosh(a) * Math.cosh(a) - 1.0)))); System.out.println(Utils.format("acosh({0:f}) = {1:f}", Math.cosh(b), Math.log(Math.cosh(b) + Math.sqrt(Math.cosh(b) * Math.cosh(b) - 1.0)))); System.out.println(Utils.format("acosh({0:f}) = {1:f}", Math.cosh(c), Math.log(Math.cosh(c) + Math.sqrt(Math.cosh(c) * Math.cosh(c) - 1.0)))); System.out.println(Utils.format("acosh({0:f}) = {1:f}", Math.cosh(d), Math.log(Math.cosh(d) + Math.sqrt(Math.cosh(d) * Math.cosh(d) - 1.0)))); System.out.println(Utils.format("atanh({0:f}) = {1:f}", Math.tanh(a), 0.5 * Math.log((Math.tanh(a) + 1.0)/(Math.tanh(a) - 1.0)))); System.out.println(Utils.format("atanh({0:f}) = {1:f}", Math.tanh(b), 0.5 * Math.log((Math.tanh(b) + 1.0)/(Math.tanh(b) - 1.0)))); System.out.println(Utils.format("atanh({0:f}) = {1:f}", Math.tanh(c), 0.5 * Math.log((Math.tanh(c) + 1.0)/(Math.tanh(c) - 1.0)))); System.out.println(Utils.format("atanh({0:f}) = {1:f}", Math.tanh(d), 0.5 * Math.log((Math.tanh(d) + 1.0)/(Math.tanh(d) - 1.0)))); // log, log10, exp, pow, sqrt System.out.println(Utils.format("log({0:f}) = {1:f}", a, Math.log(a))); System.out.println(Utils.format("log({0:f}) = {1:f}", b, Math.log(b))); System.out.println(Utils.format("log({0:f}) = {1:f}", -c, Math.log(-c))); System.out.println(Utils.format("log({0:f}) = {1:f}", -d, Math.log(-d))); System.out.println(Utils.format("log({0:f}) = {1:f}", e, Math.log(e))); System.out.println(Utils.format("log10({0:f}) = {1:f}", a, Math.log10(a))); System.out.println(Utils.format("log10({0:f}) = {1:f}", b, Math.log10(b))); System.out.println(Utils.format("log10({0:f}) = {1:f}", -c, Math.log10(-c))); System.out.println(Utils.format("log10({0:f}) = {1:f}", -d, Math.log10(-d))); System.out.println(Utils.format("log10({0:f}) = {1:f}", e, Math.log10(e))); System.out.println(Utils.format("exp({0:f}) = {1:f}", 0.5, Math.exp(0.5))); System.out.println(Utils.format("exp({0:f}) = {1:f}", 1.0, Math.exp(1.0))); System.out.println(Utils.format("exp({0:f}) = {1:f}", 2.0, Math.exp(2.0))); System.out.println(Utils.format("pow({0:f}, {1:f}) = {2:f}", 10.0, 0.5, Math.pow(10.0, 0.5))); System.out.println(Utils.format("pow({0:f}, {1:f}) = {2:f}", 10.0, 1.0, Math.pow(10.0, 1.0))); System.out.println(Utils.format("pow({0:f}, {1:f}) = {2:f}", 10.0, 2.0, Math.pow(10.0, 2.0))); System.out.println(Utils.format("sqrt({0:f}) = {1:f}", 0.5, Math.sqrt(0.5))); System.out.println(Utils.format("sqrt({0:f}) = {1:f}", 2.0, Math.sqrt(2.0))); System.out.println(Utils.format("sqrt({0:f}) = {1:f}", 10.0, Math.sqrt(10.0))); // random numbers System.out.println(Utils.format("random() = {0:f}", Math.random())); System.out.println(Utils.format("random() = {0:f}", Math.random())); System.out.println(Utils.format("random() = {0:f}", Math.random())); } }