Skip to content

Instantly share code, notes, and snippets.

@voldenet
Last active June 30, 2023 02:50
Show Gist options
  • Save voldenet/76b4e296511f93570623f0c405c18e04 to your computer and use it in GitHub Desktop.
Save voldenet/76b4e296511f93570623f0c405c18e04 to your computer and use it in GitHub Desktop.
attempt to create more accurate trigonometric functions
(-270° 0 1 NaN)
(-180° -1 0 0)
(-90° 0 -1 NaN)
(0° 1 0 0)
(30° 0.8660254037844387 0.5 0.5773502691896257)
(45° 0.7071067811865476 0.7071067811865475 1)
(60° 0.5 0.8660254037844386 1.7320508075688767)
(90° 0 1 NaN)
(180° -1 0 0)
(103762935414616227840° 1 0 0)
class Deg {
has $.m; method new(Rat() $r) { self.bless(m => $r) };
method mul(Rat $r) { Deg.new($r * self.m); };
method gist { $.m ~ "°" };
method a { self.m % 360 }
};
my \pie = Deg.new(180);
multi sub sin(Deg $a) {
given $a.a {
when 180 { 0 }
when 30 { .5 }
default { sin($_/180 * pi) }
}
}
multi sub cos(Deg $a) {
given $a.a {
when 270|90 { 0 }
when 60 { .5 }
default { cos($_/180 * pi) }
}
}
multi sub tan(Deg $a) {
given $a.a % 180 {
when 90 { NaN }
when 45 { 1 }
when 135 { -1 }
default { tan($_/180 * pi) }
}
}
multi sub infix:<*>(Rat() $r, Deg:D $a) { $a.mul($r) };
for (-1.5, -1, -.5, 0, 1/6, 1/4, 1/3, .5, 1, 2**59).map(* * pie) { say ($_, cos($_), sin($_), tan($_)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment