3 # Simple tests for the basic math functions.
15 # compile time evaluation
17 eval { $s = sqrt(-1) }; # Kind of compile time.
18 like($@, qr/sqrt of -1/, 'compile time sqrt(-1) fails');
21 is($s, 0, 'compile time sqrt(0)');
24 is($s, 1, 'compile time sqrt(1)');
27 is(substr($s,0,5), '1.414', 'compile time sqrt(2) == 1.414');
30 is($s, 1, 'compile time exp(0) == 1');
33 is(substr($s,0,7), '2.71828', 'compile time exp(1) == e');
35 eval { $s = log(0) }; # Kind of compile time.
36 like($@, qr/log of 0/, 'compile time log(0) fails');
39 is($s, 0, 'compile time log(1) == 0');
42 is(substr($s,0,5), '0.693', 'compile time log(2)');
44 cmp_ok(exp(log(1)), '==', 1, 'compile time exp(log(1)) == 1');
46 cmp_ok(round(atan2(1, 2)), '==', '0.463647609', "atan2(1, 2)");
54 eval { $s = sqrt(-$x1) };
55 like($@, qr/sqrt of -1/, 'run time sqrt(-1) fails');
58 is($s, 0, 'run time sqrt(0)');
61 is($s, 1, 'run time sqrt(1)');
64 is(substr($s,0,5), '1.414', 'run time sqrt(2) == 1.414');
67 is($s, 1, 'run time exp(0) = 1');
70 is(substr($s,0,7), '2.71828', 'run time exp(1) = e');
72 eval { $s = log($x0) };
73 like($@, qr/log of 0/, 'run time log(0) fails');
76 is($s, 0, 'compile time log(1) == 0');
79 is(substr($s,0,5), '0.693', 'run time log(2)');
81 cmp_ok(exp(log($x1)), '==', 1, 'run time exp(log(1)) == 1');
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.
88 my $pi_2 = 1.5707963267949;
92 return sprintf("%.9f", $result);
96 cmp_ok(sin(0), '==', 0.0, 'sin(0) == 0');
97 cmp_ok(abs(sin($pi_2) - 1), '<', 1e-9, 'sin(pi/2) == 1');
98 cmp_ok(abs(sin(-1 * $pi_2) - -1), '<', 1e-9, 'sin(-pi/2) == -1');
100 cmp_ok(round(sin($x1)), '==', '0.841470985', "sin(1)");
103 cmp_ok(cos(0), '==', 1.0, 'cos(0) == 1');
104 cmp_ok(abs(cos($pi_2)), '<', 1e-9, 'cos(pi/2) == 0');
105 cmp_ok(abs(cos(-1 * $pi_2)), '<', 1e-9, 'cos(-pi/2) == 0');
107 cmp_ok(round(cos($x1)), '==', '0.540302306', "cos(1)");
109 cmp_ok(round(atan2($x1, $x2)), '==', '0.463647609', "atan2($x1, $x2)");
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.
116 unless ($Config{usequadmath}) {
117 skip "need usequadmath", 1;
119 # For quadmath we have a known precision.
120 is(sqrt(2), '1.4142135623730950488016887242097', "quadmath sqrt");