Math.sqrt / log / sin / cos / Math::PI
Mathematical functions and constants provided by the Math module. Use these for calculations involving square roots, logarithms, trigonometric functions, and more.
Syntax
# Returns the square root. Math.sqrt(number) # Returns the natural log, common log, or base-2 log. Math.log(number) # Natural logarithm (base e) Math.log10(number) # Common logarithm (base 10) Math.log2(number) # Base-2 logarithm # Trigonometric functions (argument in radians). Math.sin(radians) Math.cos(radians) Math.tan(radians) # Inverse trigonometric functions (return value in radians). Math.asin(value) Math.acos(value) Math.atan(value) Math.atan2(y, x) # Two-argument version (handles all quadrants) # Mathematical constants. Math::PI # Pi π (3.141592653589793) Math::E # Base of the natural logarithm e (2.718281828459045)
Method List
| Method / Constant | Description |
|---|---|
| Math.sqrt(x) | Returns the square root of x as a floating-point number. Raises an exception if x is negative. |
| Math.log(x) | Returns the natural logarithm (base e) of x. x must be a positive number. |
| Math.log10(x) | Returns the common logarithm (base 10) of x. |
| Math.log2(x) | Returns the base-2 logarithm of x. |
| Math.sin(x) | Returns the sine of x radians. |
| Math.cos(x) | Returns the cosine of x radians. |
| Math.tan(x) | Returns the tangent of x radians. |
| Math.atan2(y, x) | Returns the angle (in radians) of the point (x, y). Correctly determines the quadrant. |
| Math::PI | Approximation of π (3.141592653589793). |
| Math::E | Approximation of the base of the natural logarithm e (2.718281828459045). |
Sample Code
# sqrt: Calculate the square root.
puts Math.sqrt(9) # 3.0
puts Math.sqrt(2) # 1.4142135623730951
# log: Calculate logarithms.
puts Math.log(Math::E) # 1.0 (natural logarithm)
puts Math.log10(100) # 2.0 (10 squared = 100)
puts Math.log2(8) # 3.0 (2 cubed = 8)
# Trigonometric functions: arguments are in radians.
# To convert degrees to radians, multiply by π/180.
deg_to_rad = Math::PI / 180
puts Math.sin(90 * deg_to_rad).round(10) # 1.0 (sin 90°)
puts Math.cos(0).round(10) # 1.0 (cos 0°)
puts Math.sin(30 * deg_to_rad).round(10) # 0.5 (sin 30°)
# Calculate the area of a circle (radius 5).
radius = 5
area = Math::PI * radius ** 2
puts "Area: #{area.round(2)}" # Area: 78.54
# Find the hypotenuse of a right triangle (Pythagorean theorem).
a = 3
b = 4
hypotenuse = Math.sqrt(a**2 + b**2)
puts "Hypotenuse: #{hypotenuse}" # Hypotenuse: 5.0
Overview
Math is a built-in Ruby module that provides functions and constants for mathematical calculations. All functions return floating-point numbers.
All trigonometric function arguments and return values are in radians. To work in degrees, convert using degrees * Math::PI / 180 before passing the value.
Calling Math.sqrt with a negative number raises a Math::DomainError. If you need the square root of a complex number, use the Complex class instead (e.g., Complex(-1, 0).sqrt).
For rounding numbers, see Number.abs / round / ceil / floor. For integer iteration, see Number.times / upto / downto.
If you find any errors or copyright issues, please contact us.