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