Built-in routines

Trigonometry — sin, cos, tan & friends

Full

Trigonometric and hyperbolic functions, in radians, as subs or methods.

Raku provides the full set of trigonometric functions — sin, cos, tan, their inverses, reciprocals, and hyperbolic variants. Each works as a sub (sin($x)) or a method ($x.sin), and all angles are in radians. The constants pi, tau (2·pi), and e are built in.

Sine, cosine, tangent #

say sin(0);
say cos(pi);
say (pi/2).sin;
Output
0
-1
1

cos(pi) is -1, and the method form (pi/2).sin is 1 — a quarter turn's sine.

Working in radians #

Angles are radians, so convert from degrees by multiplying by pi/180. sin of 30° is 0.5.

say sin(30 * pi / 180).round(1e-9);
say cos(60 * pi / 180).round(1e-9);
Output
0.5
0.5

Inverse functions #

asin, acos, atan, and the two-argument atan2 return an angle in radians. atan2(y, x) is the angle of the point (x, y) — the robust way to get a bearing.

say atan2(1, 1);
say asin(1);
Output
0.7853981633974483
1.5707963267948966

atan2(1, 1) is π/4 (45°) and asin(1) is π/2 (90°), both in radians.

Hyperbolic functions #

sinh, cosh, and tanh (with inverses asinh, acosh, atanh) are the hyperbolic counterparts.

say sinh(0);
say cosh(0);
say tanh(0);
Output
0
1
0

Notes #