From: Steve Peters Date: Fri, 15 Apr 2005 09:10:54 +0000 (-0500) Subject: IEEE math for the masses X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0630166f39b2fc31415e8078cd74fafef99606af;p=p5sagit%2Fp5-mst-13.2.git IEEE math for the masses Message-ID: <20050415141054.GA12749@mccoy.peters.homeunix.org> (tests added to t/op/exp.t) p4raw-id: //depot/perl@24371 --- diff --git a/pp.c b/pp.c index d107ab5..2ba5b61 100644 --- a/pp.c +++ b/pp.c @@ -35,6 +35,14 @@ extern Pid_t getpid (void); #endif +/* + * Some BSDs and Cygwin default to POSIX math instead of IEEE. + * This switches them over to IEEE. + */ +#if defined(LIBM_LIB_VERSION) + _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_; +#endif + /* variations on pp_null */ PP(pp_stub) diff --git a/t/op/exp.t b/t/op/exp.t index 689f367..927c5da 100755 --- a/t/op/exp.t +++ b/t/op/exp.t @@ -6,7 +6,7 @@ BEGIN { require './test.pl'; } -plan tests => 6; +plan tests => 22; # compile time evaluation @@ -29,3 +29,35 @@ $s = exp($x1); is(substr($s,0,7), '2.71828'); ok(exp(log($x1)) == 1); + +# tests for transcendental functions + +my $pi = 3.1415926535897931160; +my $pi_2 = 1.5707963267948965580; + +sub round { + my $result = shift; + return sprintf("%.9f", $result); +} + +# sin() tests +ok(sin(0) == 0.0); +ok(round(sin($pi)) == 0.0); +ok(round(sin(-1 * $pi)) == 0.0); +ok(round(sin($pi_2)) == 1.0); +ok(round(sin(-1 * $pi_2)) == -1.0); + +# cos() tests +ok(cos(0) == 1.0); +ok(round(cos($pi)) == -1.0); +ok(round(cos(-1 * $pi)) == -1.0); +ok(round(cos($pi_2)) == 0.0); +ok(round(cos(-1 * $pi_2)) == 0.0); + +# atan2() tests +ok(round(atan2(-0.0, 0.0)) == 0); +ok(round(atan2(0.0, 0.0)) == 0); +ok(round(atan2(-0.0, -0.0)) == round(-1 * $pi)); +ok(round(atan2(0.0, -0.0)) == round($pi)); +ok(round(atan2(-1.0, 0.0)) == round(-1 * $pi_2)); +ok(round(atan2(1.0, 0.0)) == round($pi_2));