From e3d91921554fd273687a2ac514cb1023b7b69bb7 Mon Sep 17 00:00:00 2001 From: Lucien Grondin Date: Sat, 13 May 2023 10:20:53 +0200 Subject: [PATCH] pin-point one half case --- META6.json | 2 +- lib/TrigPi.pm6 | 18 ++++++++---------- t/edges.t | 16 ++++++++++++---- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/META6.json b/META6.json index b777d9d..86e7a64 100644 --- a/META6.json +++ b/META6.json @@ -2,7 +2,7 @@ "perl" : "6.*", "name" : "TrigPi", "license" : "MIT", - "version" : "1.5.0", + "version" : "1.6.0", "description" : "accurate computation of sin(π*$x) and cos(π*$x)", "author" : "Lucien Grondin", "build-depends" : [ ], diff --git a/lib/TrigPi.pm6 b/lib/TrigPi.pm6 index 595d403..df968f7 100644 --- a/lib/TrigPi.pm6 +++ b/lib/TrigPi.pm6 @@ -1,26 +1,24 @@ unit module TrigPi; sub sinPi(Real $x --> Real) is export { - $x < 0 ?? -sinPi(-$x) !! - $x ≤ 1/2 ?? sin(pi*$x) !! - $x < 1 ?? sinPi(1 - $x) !! - $x < 2 ?? -sinPi($x - 1) !! + $x < 0 ?? -sinPi(-$x) !! + $x < ½ ?? sin(pi*$x) !! + $x == ½ ?? 1 !! + $x < 1 ?? sinPi(1 - $x) !! + $x < 2 ?? -sinPi($x - 1) !! sinPi($x % 2) } sub cosPi(Real $x --> Real) is export { $x < 0 ?? cosPi(-$x) !! - $x ≤ 1/2 ?? cos(pi*$x) !! + $x < ½ ?? cos(pi*$x) !! + $x == ½ ?? 0 !! $x < 1 ?? -cosPi(1 - $x) !! $x < 2 ?? -cosPi($x - 1) !! cosPi($x % 2) } sub cisPi(Real $x --> Complex) is export { - $x < 0 ?? cisPi(-$x).conj !! - $x ≤ 1/2 ?? cis(pi*$x) !! - $x < 1 ?? -cisPi(1-$x).conj !! - $x < 2 ?? -cisPi($x-1) !! - cisPi($x % 2) + Complex.new: cosPi($x), sinPi($x); } diff --git a/t/edges.t b/t/edges.t index 5ed539f..23f7db9 100644 --- a/t/edges.t +++ b/t/edges.t @@ -1,11 +1,19 @@ use Test; +use lib ; use TrigPi; -plan 3*4; +plan 2+4; + +ok cisPi(1/2).reals[0] == 0; +ok cisPi(-1/2).reals[0] == 0; for <0 1/2 1 2> { - is-approx cisPi($_), cis(pi*$_); - is-approx cosPi($_), cos(pi*$_); - is-approx sinPi($_), sin(pi*$_); + subtest ~(+$_), { + plan 3; + is-approx cisPi($_), cis(pi*$_); + is-approx cosPi($_), cos(pi*$_); + is-approx sinPi($_), sin(pi*$_); + } } +