#!/usr/bin/env perl use utf8; ############################################################################### # This program demonstrates the math library. # # Copyright © 2016 Richard Lesh. All rights reserved. ############################################################################### use List::Util qw(min max); use Math::Trig qw(abs acos acosh asin asinh atan atan2 atanh cosh pi pow rand sinh tan tanh); use Math::Trig; use POSIX qw(ceil floor log10); use Utils; use strict; use warnings; MAIN: { my $a = pi() / 6; my $b = pi() / 4; my $c = -$a * 2; my $d = -$b * 2; my $e = exp(1.); print Utils::messageFormat("pi = \{0:f\}", pi()), "\n"; print Utils::messageFormat("e = \{0:f\}", exp(1.)), "\n"; # abs, floor, ceil, round, trunc, min, max print Utils::messageFormat("abs(\{0:f\}) = \{1:f\}", $a, abs($a)), "\n"; print Utils::messageFormat("abs(\{0:f\}) = \{1:f\}", $c, abs($c)), "\n"; print Utils::messageFormat("floor(\{0:f\}) = \{1:f\}", $a, floor($a)), "\n"; print Utils::messageFormat("floor(\{0:f\}) = \{1:f\}", $c, floor($c)), "\n"; print Utils::messageFormat("ceil(\{0:f\}) = \{1:f\}", $a, ceil($a)), "\n"; print Utils::messageFormat("ceil(\{0:f\}) = \{1:f\}", $c, ceil($c)), "\n"; print Utils::messageFormat("round(\{0:f\}) = \{1:f\}", $a, floor($a + 0.5)), "\n"; print Utils::messageFormat("round(\{0:f\}) = \{1:f\}", $c, floor($c + 0.5)), "\n"; print Utils::messageFormat("trunc(\{0:f\}) = \{1:f\}", $a, $a < 0.0 ? ceil($a) : floor($a)), "\n"; print Utils::messageFormat("trunc(\{0:f\}) = \{1:f\}", $c, $c < 0.0 ? ceil($c) : floor($c)), "\n"; print Utils::messageFormat("min(\{0:f\}, \{1:f\}) = \{2:f\}", $a, $c, min($a, $c)), "\n"; print Utils::messageFormat("max(\{0:f\}, \{1:f\}) = \{2:f\}", $a, $c, max($a, $c)), "\n"; # sin, cos, tan, atan, atan2, acos, asin print Utils::messageFormat("sin(\{0:f\}) = \{1:f\}", $a, sin($a)), "\n"; print Utils::messageFormat("sin(\{0:f\}) = \{1:f\}", $b, sin($b)), "\n"; print Utils::messageFormat("sin(\{0:f\}) = \{1:f\}", $c, sin($c)), "\n"; print Utils::messageFormat("sin(\{0:f\}) = \{1:f\}", $d, sin($d)), "\n"; print Utils::messageFormat("cos(\{0:f\}) = \{1:f\}", $a, cos($a)), "\n"; print Utils::messageFormat("cos(\{0:f\}) = \{1:f\}", $b, cos($b)), "\n"; print Utils::messageFormat("cos(\{0:f\}) = \{1:f\}", $c, cos($c)), "\n"; print Utils::messageFormat("cos(\{0:f\}) = \{1:f\}", $d, cos($d)), "\n"; print Utils::messageFormat("tan(\{0:f\}) = \{1:f\}", $a, tan($a)), "\n"; print Utils::messageFormat("tan(\{0:f\}) = \{1:f\}", $b, tan($b)), "\n"; print Utils::messageFormat("tan(\{0:f\}) = \{1:f\}", $c, tan($c)), "\n"; print Utils::messageFormat("asin(\{0:f\}) = \{1:f\}", sin($a), asin(sin($a))), "\n"; print Utils::messageFormat("asin(\{0:f\}) = \{1:f\}", sin($b), asin(sin($b))), "\n"; print Utils::messageFormat("asin(\{0:f\}) = \{1:f\}", sin($c), asin(sin($c))), "\n"; print Utils::messageFormat("asin(\{0:f\}) = \{1:f\}", sin($d), asin(sin($d))), "\n"; print Utils::messageFormat("acos(\{0:f\}) = \{1:f\}", cos($a), acos(cos($a))), "\n"; print Utils::messageFormat("acos(\{0:f\}) = \{1:f\}", cos($b), acos(cos($b))), "\n"; print Utils::messageFormat("acos(\{0:f\}) = \{1:f\}", cos($c), acos(cos($c))), "\n"; print Utils::messageFormat("acos(\{0:f\}) = \{1:f\}", cos($d), acos(cos($d))), "\n"; print Utils::messageFormat("atan(\{0:f\}) = \{1:f\}", tan($a), atan(tan($a))), "\n"; print Utils::messageFormat("atan(\{0:f\}) = \{1:f\}", tan($b), atan(tan($b))), "\n"; print Utils::messageFormat("atan(\{0:f\}) = \{1:f\}", tan($c), atan(tan($c))), "\n"; # 45 degrees print Utils::messageFormat("atan2(\{0:f\}, \{1:f\}) = \{2:f\}", 1.0, 1.0, atan2(1.0, 1.0)), "\n"; # 30 degrees print Utils::messageFormat("atan2(\{0:f\}, \{1:f\}) = \{2:f\}", 1.0, sqrt(3.0), atan2(1.0, sqrt(3.0))), "\n"; # sinh, cosh, tanh, atanh, acosh, asinh print Utils::messageFormat("sinh(\{0:f\}) = \{1:f\}", $a, sinh($a)), "\n"; print Utils::messageFormat("sinh(\{0:f\}) = \{1:f\}", $b, sinh($b)), "\n"; print Utils::messageFormat("sinh(\{0:f\}) = \{1:f\}", $c, sinh($c)), "\n"; print Utils::messageFormat("sinh(\{0:f\}) = \{1:f\}", $d, sinh($d)), "\n"; print Utils::messageFormat("cosh(\{0:f\}) = \{1:f\}", $a, cosh($a)), "\n"; print Utils::messageFormat("cosh(\{0:f\}) = \{1:f\}", $b, cosh($b)), "\n"; print Utils::messageFormat("cosh(\{0:f\}) = \{1:f\}", $c, cosh($c)), "\n"; print Utils::messageFormat("cosh(\{0:f\}) = \{1:f\}", $d, cosh($d)), "\n"; print Utils::messageFormat("tanh(\{0:f\}) = \{1:f\}", $a, tanh($a)), "\n"; print Utils::messageFormat("tanh(\{0:f\}) = \{1:f\}", $b, tanh($b)), "\n"; print Utils::messageFormat("tanh(\{0:f\}) = \{1:f\}", $c, tanh($c)), "\n"; print Utils::messageFormat("tanh(\{0:f\}) = \{1:f\}", $d, tanh($d)), "\n"; print Utils::messageFormat("asinh(\{0:f\}) = \{1:f\}", sinh($a), asinh(sinh($a))), "\n"; print Utils::messageFormat("asinh(\{0:f\}) = \{1:f\}", sinh($b), asinh(sinh($b))), "\n"; print Utils::messageFormat("asinh(\{0:f\}) = \{1:f\}", sinh($c), asinh(sinh($c))), "\n"; print Utils::messageFormat("asinh(\{0:f\}) = \{1:f\}", sinh($d), asinh(sinh($d))), "\n"; print Utils::messageFormat("acosh(\{0:f\}) = \{1:f\}", cosh($a), acosh(cosh($a))), "\n"; print Utils::messageFormat("acosh(\{0:f\}) = \{1:f\}", cosh($b), acosh(cosh($b))), "\n"; print Utils::messageFormat("acosh(\{0:f\}) = \{1:f\}", cosh($c), acosh(cosh($c))), "\n"; print Utils::messageFormat("acosh(\{0:f\}) = \{1:f\}", cosh($d), acosh(cosh($d))), "\n"; print Utils::messageFormat("atanh(\{0:f\}) = \{1:f\}", tanh($a), atanh(tanh($a))), "\n"; print Utils::messageFormat("atanh(\{0:f\}) = \{1:f\}", tanh($b), atanh(tanh($b))), "\n"; print Utils::messageFormat("atanh(\{0:f\}) = \{1:f\}", tanh($c), atanh(tanh($c))), "\n"; print Utils::messageFormat("atanh(\{0:f\}) = \{1:f\}", tanh($d), atanh(tanh($d))), "\n"; # log, log10, exp, pow, sqrt print Utils::messageFormat("log(\{0:f\}) = \{1:f\}", $a, log($a)), "\n"; print Utils::messageFormat("log(\{0:f\}) = \{1:f\}", $b, log($b)), "\n"; print Utils::messageFormat("log(\{0:f\}) = \{1:f\}", -$c, log(-$c)), "\n"; print Utils::messageFormat("log(\{0:f\}) = \{1:f\}", -$d, log(-$d)), "\n"; print Utils::messageFormat("log(\{0:f\}) = \{1:f\}", $e, log($e)), "\n"; print Utils::messageFormat("log10(\{0:f\}) = \{1:f\}", $a, log10($a)), "\n"; print Utils::messageFormat("log10(\{0:f\}) = \{1:f\}", $b, log10($b)), "\n"; print Utils::messageFormat("log10(\{0:f\}) = \{1:f\}", -$c, log10(-$c)), "\n"; print Utils::messageFormat("log10(\{0:f\}) = \{1:f\}", -$d, log10(-$d)), "\n"; print Utils::messageFormat("log10(\{0:f\}) = \{1:f\}", $e, log10($e)), "\n"; print Utils::messageFormat("exp(\{0:f\}) = \{1:f\}", 0.5, exp(0.5)), "\n"; print Utils::messageFormat("exp(\{0:f\}) = \{1:f\}", 1.0, exp(1.0)), "\n"; print Utils::messageFormat("exp(\{0:f\}) = \{1:f\}", 2.0, exp(2.0)), "\n"; print Utils::messageFormat("pow(\{0:f\}, \{1:f\}) = \{2:f\}", 10.0, 0.5, ((10.0) ** (0.5))), "\n"; print Utils::messageFormat("pow(\{0:f\}, \{1:f\}) = \{2:f\}", 10.0, 1.0, ((10.0) ** (1.0))), "\n"; print Utils::messageFormat("pow(\{0:f\}, \{1:f\}) = \{2:f\}", 10.0, 2.0, ((10.0) ** (2.0))), "\n"; print Utils::messageFormat("sqrt(\{0:f\}) = \{1:f\}", 0.5, sqrt(0.5)), "\n"; print Utils::messageFormat("sqrt(\{0:f\}) = \{1:f\}", 2.0, sqrt(2.0)), "\n"; print Utils::messageFormat("sqrt(\{0:f\}) = \{1:f\}", 10.0, sqrt(10.0)), "\n"; # random numbers print Utils::messageFormat("random() = \{0:f\}", rand()), "\n"; print Utils::messageFormat("random() = \{0:f\}", rand()), "\n"; print Utils::messageFormat("random() = \{0:f\}", rand()), "\n"; }