Math
Standard library namespace · Always available, with no import
Math holds the math that isn’t a method of one number: the constants π and e,
trigonometry, logarithms, and powers. Every function takes Float values and gives back a
Float, and an Int is accepted wherever a Float is expected.
const radius = 3const area = Math.pi * radius ** 2print(area.round_to(2)) # prints 28.27
Math.log10(1000) # → 3.0Math.power(2, 10) # → 1024.0Angles are in radians, as in most programming languages, not degrees. Math.sin(90) is the
sine of 90 radians, not of a right angle. A number’s own
to_radians() and
to_degrees() convert between them:
Math.sin(90) # → 0.8939966636005579Math.sin(90.0.to_radians()) # → 1.0Math.arc_tan(1).to_degrees() # → 45.0Like all Float arithmetic, results are close approximations: Math.sin(30.0.to_radians())
is 0.49999999999999994, not 0.5. Round a result when you show it.
An impossible question gets an answer rather than an error. The logarithm of a negative
number, or the arcsine of 2, is NaN, “not a number”, and the natural logarithm of 0 is
-Infinity. Check for them with nan?() and
infinite?().
A number’s own methods cover the rest: square_root(),
abs(), and rounding.
At a glance
Section titled “At a glance”| Constants | |
|---|---|
Math.pi |
π, the ratio of a circle’s circumference to its diameter |
Math.e |
e, the base of natural logarithms |
| Trigonometry | |
|---|---|
Math.sin(radians), Math.cos(radians), Math.tan(radians) |
Sine, cosine, and tangent |
Math.arc_sin(value), Math.arc_cos(value), Math.arc_tan(value) |
The angle with that sine, cosine, or tangent |
Math.arc_tan2(y, x) |
The angle of a point from the origin |
| Logarithms and powers | |
|---|---|
Math.natural_log(value) |
The logarithm with base e |
Math.log10(value) |
The logarithm with base 10 |
Math.log(value, base) |
The logarithm with any base |
Math.power(base, exponent) |
base raised to exponent |
Constants
Section titled “Constants”π, the ratio of a circle’s circumference to its diameter.
Math.pi # → 3.141592653589793e, the base of natural logarithms.
Math.e # → 2.718281828459045Trigonometry
Section titled “Trigonometry”Math.sin(radians: Float): Float
The sine of an angle in radians.
Math.sin(Math.pi / 2) # → 1.0Math.sin(30.0.to_radians()).round_to(2) # → 0.5Math.cos(radians: Float): Float
The cosine of an angle in radians.
Math.cos(0) # → 1.0Math.cos(Math.pi) # → -1.0Math.tan(radians: Float): Float
The tangent of an angle in radians.
Math.tan(Math.pi / 4) # → 0.9999999999999999Math.arc_sin(value: Float): Float
The angle, in radians, whose sine is value: the inverse of sin. The answer is between
-π/2 and π/2. A value outside -1 to 1 has no such angle, and gives NaN.
Math.arc_sin(1).to_degrees() # → 90.0Math.arc_sin(2).nan?() # → trueMath.arc_cos(value: Float): Float
The angle, in radians, whose cosine is value: the inverse of cos. The answer is between
0 and π. A value outside -1 to 1 gives NaN.
Math.arc_cos(0.5).to_degrees().round_to(2) # → 60.0Math.arc_tan(value: Float): Float
The angle, in radians, whose tangent is value: the inverse of tan. The answer is between
-π/2 and π/2.
Math.arc_tan(1).to_degrees() # → 45.0Math.arc_tan2(y: Float, x: Float): Float
The angle, in radians, from the positive x-axis to the point (x, y). Note that y comes
first. Unlike arc_tan(y / x), it uses the signs of both to tell which quarter of the plane
the point is in, so the answer runs all the way from -π to π. It is the usual way to find the
direction from one point to another, such as which way a game character should face.
Math.arc_tan2(1, 1).to_degrees() # → 45.0Math.arc_tan2(-1, -1).to_degrees() # → -135.0Logarithms and powers
Section titled “Logarithms and powers”Math.natural_log(value: Float): Float
The natural logarithm: the power that e must be raised to in order to get value. A negative
value gives NaN, and 0 gives -Infinity.
Math.natural_log(Math.e) # → 1.0Math.natural_log(1) # → 0.0Math.natural_log(0) # → -InfinityMath.log10(value: Float): Float
The logarithm with base 10: for a whole power of ten, how many zeros it has. A negative
value gives NaN, and 0 gives -Infinity.
Math.log10(1000) # → 3.0Math.log(value: Float, base: Float): Float
The logarithm with any base: the power that base must be raised to in order to get value.
A negative value, or a base that is zero, negative, or 1, gives NaN.
Math.log(8, 2) # → 3.0Math.log(81, 3) # → 4.0Math.power(base: Float, exponent: Float): Float
base raised to exponent, always as a Float. A negative base raised to a fractional
exponent gives NaN.
Math.power(2, 10) # → 1024.0Math.power(9, 0.5) # → 3.0The ** operator does the same and is usually easier to read: 2 ** 10 is 1024, and stays
an Int when both sides are.