#!/usr/bin/env swift; /****************************************************************************** * This program demonstrates the math library. * * Copyright © 2016 Richard Lesh. All rights reserved. *****************************************************************************/ import Foundation import Utils // Begin Main let a:Double = Double.pi / Double(6) let b:Double = Double.pi / Double(4) let c:Double = -a * 2 let d:Double = -b * 2 let e:Double = M_E print(Utils.format("pi = {0:f}", Double.pi)) print(Utils.format("e = {0:f}", M_E)) // abs, floor, ceil, round, trunc, min, max print(Utils.format("abs({0:f}) = {1:f}", a, fabs(a))) print(Utils.format("abs({0:f}) = {1:f}", c, fabs(c))) print(Utils.format("floor({0:f}) = {1:f}", a, floor(a))) print(Utils.format("floor({0:f}) = {1:f}", c, floor(c))) print(Utils.format("ceil({0:f}) = {1:f}", a, ceil(a))) print(Utils.format("ceil({0:f}) = {1:f}", c, ceil(c))) print(Utils.format("round({0:f}) = {1:f}", a, round(a))) print(Utils.format("round({0:f}) = {1:f}", c, round(c))) print(Utils.format("trunc({0:f}) = {1:f}", a, trunc(a))) print(Utils.format("trunc({0:f}) = {1:f}", c, trunc(c))) print(Utils.format("min({0:f}, {1:f}) = {2:f}", a, c, fmin(a, c))) print(Utils.format("max({0:f}, {1:f}) = {2:f}", a, c, fmax(a, c))) // sin, cos, tan, atan, atan2, acos, asin print(Utils.format("sin({0:f}) = {1:f}", a, sin(a))) print(Utils.format("sin({0:f}) = {1:f}", b, sin(b))) print(Utils.format("sin({0:f}) = {1:f}", c, sin(c))) print(Utils.format("sin({0:f}) = {1:f}", d, sin(d))) print(Utils.format("cos({0:f}) = {1:f}", a, cos(a))) print(Utils.format("cos({0:f}) = {1:f}", b, cos(b))) print(Utils.format("cos({0:f}) = {1:f}", c, cos(c))) print(Utils.format("cos({0:f}) = {1:f}", d, cos(d))) print(Utils.format("tan({0:f}) = {1:f}", a, tan(a))) print(Utils.format("tan({0:f}) = {1:f}", b, tan(b))) print(Utils.format("tan({0:f}) = {1:f}", c, tan(c))) print(Utils.format("asin({0:f}) = {1:f}", sin(a), asin(sin(a)))) print(Utils.format("asin({0:f}) = {1:f}", sin(b), asin(sin(b)))) print(Utils.format("asin({0:f}) = {1:f}", sin(c), asin(sin(c)))) print(Utils.format("asin({0:f}) = {1:f}", sin(d), asin(sin(d)))) print(Utils.format("acos({0:f}) = {1:f}", cos(a), acos(cos(a)))) print(Utils.format("acos({0:f}) = {1:f}", cos(b), acos(cos(b)))) print(Utils.format("acos({0:f}) = {1:f}", cos(c), acos(cos(c)))) print(Utils.format("acos({0:f}) = {1:f}", cos(d), acos(cos(d)))) print(Utils.format("atan({0:f}) = {1:f}", tan(a), atan(tan(a)))) print(Utils.format("atan({0:f}) = {1:f}", tan(b), atan(tan(b)))) print(Utils.format("atan({0:f}) = {1:f}", tan(c), atan(tan(c)))) // 45 degrees print(Utils.format("atan2({0:f}, {1:f}) = {2:f}", 1.0, 1.0, atan2(1.0, 1.0))) // 30 degrees print(Utils.format("atan2({0:f}, {1:f}) = {2:f}", 1.0, sqrt(3.0), atan2(1.0, sqrt(3.0)))) // sinh, cosh, tanh, atanh, acosh, asinh print(Utils.format("sinh({0:f}) = {1:f}", a, sinh(a))) print(Utils.format("sinh({0:f}) = {1:f}", b, sinh(b))) print(Utils.format("sinh({0:f}) = {1:f}", c, sinh(c))) print(Utils.format("sinh({0:f}) = {1:f}", d, sinh(d))) print(Utils.format("cosh({0:f}) = {1:f}", a, cosh(a))) print(Utils.format("cosh({0:f}) = {1:f}", b, cosh(b))) print(Utils.format("cosh({0:f}) = {1:f}", c, cosh(c))) print(Utils.format("cosh({0:f}) = {1:f}", d, cosh(d))) print(Utils.format("tanh({0:f}) = {1:f}", a, tanh(a))) print(Utils.format("tanh({0:f}) = {1:f}", b, tanh(b))) print(Utils.format("tanh({0:f}) = {1:f}", c, tanh(c))) print(Utils.format("tanh({0:f}) = {1:f}", d, tanh(d))) print(Utils.format("asinh({0:f}) = {1:f}", sinh(a), asinh(sinh(a)))) print(Utils.format("asinh({0:f}) = {1:f}", sinh(b), asinh(sinh(b)))) print(Utils.format("asinh({0:f}) = {1:f}", sinh(c), asinh(sinh(c)))) print(Utils.format("asinh({0:f}) = {1:f}", sinh(d), asinh(sinh(d)))) print(Utils.format("acosh({0:f}) = {1:f}", cosh(a), acosh(cosh(a)))) print(Utils.format("acosh({0:f}) = {1:f}", cosh(b), acosh(cosh(b)))) print(Utils.format("acosh({0:f}) = {1:f}", cosh(c), acosh(cosh(c)))) print(Utils.format("acosh({0:f}) = {1:f}", cosh(d), acosh(cosh(d)))) print(Utils.format("atanh({0:f}) = {1:f}", tanh(a), atanh(tanh(a)))) print(Utils.format("atanh({0:f}) = {1:f}", tanh(b), atanh(tanh(b)))) print(Utils.format("atanh({0:f}) = {1:f}", tanh(c), atanh(tanh(c)))) print(Utils.format("atanh({0:f}) = {1:f}", tanh(d), atanh(tanh(d)))) // log, log10, exp, pow, sqrt print(Utils.format("log({0:f}) = {1:f}", a, log(a))) print(Utils.format("log({0:f}) = {1:f}", b, log(b))) print(Utils.format("log({0:f}) = {1:f}", -c, log(-c))) print(Utils.format("log({0:f}) = {1:f}", -d, log(-d))) print(Utils.format("log({0:f}) = {1:f}", e, log(e))) print(Utils.format("log10({0:f}) = {1:f}", a, log10(a))) print(Utils.format("log10({0:f}) = {1:f}", b, log10(b))) print(Utils.format("log10({0:f}) = {1:f}", -c, log10(-c))) print(Utils.format("log10({0:f}) = {1:f}", -d, log10(-d))) print(Utils.format("log10({0:f}) = {1:f}", e, log10(e))) print(Utils.format("exp({0:f}) = {1:f}", 0.5, exp(0.5))) print(Utils.format("exp({0:f}) = {1:f}", 1.0, exp(1.0))) print(Utils.format("exp({0:f}) = {1:f}", 2.0, exp(2.0))) print(Utils.format("pow({0:f}, {1:f}) = {2:f}", 10.0, 0.5, pow(10.0, 0.5))) print(Utils.format("pow({0:f}, {1:f}) = {2:f}", 10.0, 1.0, pow(10.0, 1.0))) print(Utils.format("pow({0:f}, {1:f}) = {2:f}", 10.0, 2.0, pow(10.0, 2.0))) print(Utils.format("sqrt({0:f}) = {1:f}", 0.5, sqrt(0.5))) print(Utils.format("sqrt({0:f}) = {1:f}", 2.0, sqrt(2.0))) print(Utils.format("sqrt({0:f}) = {1:f}", 10.0, sqrt(10.0))) // random numbers print(Utils.format("random() = {0:f}", Double.random(in: 0.0 ..< 1.0))) print(Utils.format("random() = {0:f}", Double.random(in: 0.0 ..< 1.0))) print(Utils.format("random() = {0:f}", Double.random(in: 0.0 ..< 1.0))) exit(EXIT_SUCCESS)