This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/gv.t working under minitest
[perl5.git] / t / op / exp.t
1 #!./perl
2
3 # Simple tests for the basic math functions.
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = '../lib';
8     require './test.pl';
9 }
10
11 plan tests => 30;
12
13 # compile time evaluation
14
15 eval { $s = sqrt(-1) }; # Kind of compile time.
16 like($@, qr/sqrt of -1/, 'compile time sqrt(-1) fails');
17
18 $s = sqrt(0);
19 is($s, 0, 'compile time sqrt(0)');
20
21 $s = sqrt(1);
22 is($s, 1, 'compile time sqrt(1)');
23
24 $s = sqrt(2);
25 is(substr($s,0,5), '1.414', 'compile time sqrt(2) == 1.414');
26
27 $s = exp(0);
28 is($s, 1, 'compile time exp(0) == 1');
29
30 $s = exp(1);
31 is(substr($s,0,7), '2.71828', 'compile time exp(1) == e');
32
33 eval { $s = log(0) };  # Kind of compile time.
34 like($@, qr/log of 0/, 'compile time log(0) fails');
35
36 $s = log(1);
37 is($s, 0, 'compile time log(1) == 0');
38
39 $s = log(2);
40 is(substr($s,0,5), '0.693', 'compile time log(2)');
41
42 cmp_ok(exp(log(1)), '==', 1, 'compile time exp(log(1)) == 1');
43
44 cmp_ok(round(atan2(1, 2)), '==', '0.463647609', "atan2(1, 2)");
45
46 # run time evaluation
47
48 $x0 = 0;
49 $x1 = 1;
50 $x2 = 2;
51
52 eval { $s = sqrt(-$x1) };
53 like($@, qr/sqrt of -1/, 'run time sqrt(-1) fails');
54
55 $s = sqrt($x0);
56 is($s, 0, 'run time sqrt(0)');
57
58 $s = sqrt($x1);
59 is($s, 1, 'run time sqrt(1)');
60
61 $s = sqrt($x2);
62 is(substr($s,0,5), '1.414', 'run time sqrt(2) == 1.414');
63
64 $s = exp($x0);
65 is($s, 1, 'run time exp(0) = 1');
66
67 $s = exp($x1);
68 is(substr($s,0,7), '2.71828', 'run time exp(1) = e');
69
70 eval { $s = log($x0) };
71 like($@, qr/log of 0/, 'run time log(0) fails');
72
73 $s = log($x1);
74 is($s, 0, 'compile time log(1) == 0');
75
76 $s = log($x2);
77 is(substr($s,0,5), '0.693', 'run time log(2)');
78
79 cmp_ok(exp(log($x1)), '==', 1, 'run time exp(log(1)) == 1');
80
81 # NOTE: do NOT test the trigonometric functions at [+-]Pi
82 # and expect to get exact results like 0, 1, -1, because
83 # you may not be able to feed them exactly [+-]Pi given
84 # all the variations of different long doubles.
85
86 my $pi_2 = 1.5707963267949;
87
88 sub round {
89    my $result = shift;
90    return sprintf("%.9f", $result);
91 }
92
93 # sin() tests
94 cmp_ok(sin(0), '==', 0.0, 'sin(0) == 0');
95 cmp_ok(abs(sin($pi_2) - 1), '<', 1e-9, 'sin(pi/2) == 1');
96 cmp_ok(abs(sin(-1 * $pi_2) - -1), '<', 1e-9, 'sin(-pi/2) == -1');
97
98 cmp_ok(round(sin($x1)), '==', '0.841470985', "sin(1)");
99
100 # cos() tests
101 cmp_ok(cos(0), '==', 1.0, 'cos(0) == 1');
102 cmp_ok(abs(cos($pi_2)), '<', 1e-9, 'cos(pi/2) == 0');
103 cmp_ok(abs(cos(-1 * $pi_2)), '<', 1e-9, 'cos(-pi/2) == 0');
104
105 cmp_ok(round(cos($x1)), '==', '0.540302306', "cos(1)");
106
107 cmp_ok(round(atan2($x1, $x2)), '==', '0.463647609', "atan2($x1, $x2)");
108
109 # atan2() tests testing with -0.0, 0.0, -1.0, 1.0 were removed due to
110 # differing results from calls to atan2() on various OS's and
111 # architectures.  See perlport.pod for more information.