Skip to content

Commit

Permalink
pin-point one half case
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucien Grondin committed May 13, 2023
1 parent 35cad96 commit e3d9192
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : [ ],
Expand Down
18 changes: 8 additions & 10 deletions lib/TrigPi.pm6
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
unit module TrigPi;

sub sinPi(Real $x --> Real) is export {
$x < 0 ?? -sinPi(-$x) !!
$x1/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) !!
$x1/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 !!
$x1/2 ?? cis(pi*$x) !!
$x < 1 ?? -cisPi(1-$x).conj !!
$x < 2 ?? -cisPi($x-1) !!
cisPi($x % 2)
Complex.new: cosPi($x), sinPi($x);
}

16 changes: 12 additions & 4 deletions t/edges.t
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use Test;
use lib <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*$_);
}
}


0 comments on commit e3d9192

Please sign in to comment.