Let's calculate (\lim_{x \to 0} \frac{\sin x}{x}) in #RakuLang!
my $last-result = 0;
for (1, * / 10 ... *) -> $x {
my $result = sin($x) / $x;
LEAVE $last-result = $result;
last if $result ≅ $last-result;
say "x = $x\tsin(x) / x = $result";
}
x = 1 sin(x) / x = 0.8414709848078965
x = 0.1 sin(x) / x = 0.9983341664682815
x = 0.01 sin(x) / x = 0.9999833334166665
x = 0.001 sin(x) / x = 0.9999998333333416
x = 0.0001 sin(x) / x = 0.9999999983333334
x = 0.00001 sin(x) / x = 0.9999999999833332
x = 0.000001 sin(x) / x = 0.9999999999998334
x = 0.0000001 sin(x) / x = 0.9999999999999983
x = 0.00000001 sin(x) / x = 1
👏🏻
I tried to use some unique and advanced Raku features: lazy sequences, phasers, and even Unicode operators.
Lazy sequences: (1, * / 10 ... *) is a sequence of "increasingly small values of 1." 😁
$seq[^4]
(1 0.1 0.01 0.001)
Phasers: A phaser is a block of code that is called during a specific execution phase. The LEAVE phaser is called at the end of each iteration.
Unicode operators: ≅ is the "approximately equal" operator. It's equivalent to =~=.
{ my $*TOLERANCE = 1e-5; 878 / 323 =~= e; }
True
=> More informations about this toot | View the thread | More toots from julia@mathstodon.xyz
=> View rakulang tag This content has been proxied by September (ba2dc).Proxy Information
text/gemini