Toot

Written by Julia on 2024-06-01 at 14:24

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

Mentions

Tags

=> View rakulang tag

Proxy Information
Original URL
gemini://mastogem.picasoft.net/toot/112541817675894544
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
230.25272 milliseconds
Gemini-to-HTML Time
1.340913 milliseconds

This content has been proxied by September (ba2dc).