IEEE math for the masses
[p5sagit/p5-mst-13.2.git] / t / op / exp.t
CommitLineData
8d063cd8 1#!./perl
2
c057f5bc 3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8d063cd8 8
0630166f 9plan tests => 22;
8d063cd8 10
11# compile time evaluation
12
13$s = sqrt(2);
c057f5bc 14is(substr($s,0,5), '1.414');
8d063cd8 15
16$s = exp(1);
c057f5bc 17is(substr($s,0,7), '2.71828');
8d063cd8 18
c057f5bc 19ok(exp(log(1)) == 1);
8d063cd8 20
21# run time evaluation
22
23$x1 = 1;
24$x2 = 2;
25$s = sqrt($x2);
c057f5bc 26is(substr($s,0,5), '1.414');
8d063cd8 27
28$s = exp($x1);
c057f5bc 29is(substr($s,0,7), '2.71828');
8d063cd8 30
c057f5bc 31ok(exp(log($x1)) == 1);
0630166f 32
33# tests for transcendental functions
34
35my $pi = 3.1415926535897931160;
36my $pi_2 = 1.5707963267948965580;
37
38sub round {
39 my $result = shift;
40 return sprintf("%.9f", $result);
41}
42
43# sin() tests
44ok(sin(0) == 0.0);
45ok(round(sin($pi)) == 0.0);
46ok(round(sin(-1 * $pi)) == 0.0);
47ok(round(sin($pi_2)) == 1.0);
48ok(round(sin(-1 * $pi_2)) == -1.0);
49
50# cos() tests
51ok(cos(0) == 1.0);
52ok(round(cos($pi)) == -1.0);
53ok(round(cos(-1 * $pi)) == -1.0);
54ok(round(cos($pi_2)) == 0.0);
55ok(round(cos(-1 * $pi_2)) == 0.0);
56
57# atan2() tests
58ok(round(atan2(-0.0, 0.0)) == 0);
59ok(round(atan2(0.0, 0.0)) == 0);
60ok(round(atan2(-0.0, -0.0)) == round(-1 * $pi));
61ok(round(atan2(0.0, -0.0)) == round($pi));
62ok(round(atan2(-1.0, 0.0)) == round(-1 * $pi_2));
63ok(round(atan2(1.0, 0.0)) == round($pi_2));