How to use math functions and constants in Ruby

1 Answer

0 votes
# Program Title: Ruby Math Functions Demonstration

# ============================
# Constants
# ============================
pi_value = Math::PI        # π constant
e_value  = Math::E         # Euler's number

puts "Constants:"
puts "  pi = #{pi_value}"
puts "  e  = #{e_value}"
puts


# ============================
# Core Functions
# ============================
num_x = 2.5                # sample value

puts "Core Functions:"
puts "  sqrt(2.5) = #{Math.sqrt(num_x)}"     # square root
puts "  exp(2.5)  = #{Math.exp(num_x)}"      # e^x
puts "  log(2.5)  = #{Math.log(num_x)}"      # natural log
puts "  log10(2.5)= #{Math.log10(num_x)}"    # base‑10 log
puts "  pow(2.5,3)= #{num_x ** 3}"           # x^3
puts


# ============================
# Trigonometric Functions
# ============================
angle = Math::PI / 4       # 45 degrees in radians

puts "Trigonometric Functions:"
puts "  sin(pi/4) = #{Math.sin(angle)}"      # sine
puts "  cos(pi/4) = #{Math.cos(angle)}"      # cosine
puts "  tan(pi/4) = #{Math.tan(angle)}"      # tangent
puts "  asin(0.5) = #{Math.asin(0.5)}"       # arcsine
puts "  acos(0.5) = #{Math.acos(0.5)}"       # arccosine
puts "  atan(1.0) = #{Math.atan(1.0)}"       # arctangent
puts


# ============================
# Hyperbolic Functions
# ============================
puts "Hyperbolic Functions:"
puts "  sinh(1) = #{Math.sinh(1)}"           # hyperbolic sine
puts "  cosh(1) = #{Math.cosh(1)}"           # hyperbolic cosine
puts "  tanh(1) = #{Math.tanh(1)}"           # hyperbolic tangent
puts


# ============================
# Rounding Functions
# ============================
num_y = -2.7               # negative number for rounding tests

puts "Rounding Functions:"
puts "  floor(-2.7) = #{num_y.floor}"        # round down
puts "  ceil(-2.7)  = #{num_y.ceil}"         # round up
puts "  round(-2.7) = #{num_y.round}"        # nearest integer
puts "  trunc(-2.7) = #{num_y.truncate}"     # remove decimals
puts


# ============================
# Min / Max / Clamp
# ============================
num_a = 10
num_b = 20
value_to_clamp = 15

puts "Min/Max/Clamp:"
puts "  min(10,20) = #{[num_a, num_b].min}"  # smaller of two
puts "  max(10,20) = #{[num_a, num_b].max}"  # larger of two

clamped = value_to_clamp.clamp(0, 10)        # clamp 15 to range 0–10
puts "  clamp(15,0,10) = #{clamped}"
puts


# ============================
# Bitwise Math (Integers)
# ============================
byte_x = 0xAA              # 10101010 in hex
byte_y = 0xCC              # 11001100 in hex

puts "Bitwise Math:"
puts "  X & Y = #{(byte_x & byte_y).to_s(16)}"   # AND
puts "  X | Y = #{(byte_x | byte_y).to_s(16)}"   # OR
puts "  X ^ Y = #{(byte_x ^ byte_y).to_s(16)}"   # XOR
puts "  ~X    = #{((~byte_x) & 0xFF).to_s(16)}"  # NOT (mask to 8 bits)
puts "  X << 2 = #{((byte_x << 2) & 0xFF).to_s(16)}" # left shift
puts "  Y >> 3 = #{(byte_y >> 3).to_s(16)}"          # right shift
puts


# ============================
# Additional Useful Math
# ============================
puts "Additional Math:"
puts "  abs(-3.14) = #{(-3.14).abs}"          # absolute value
puts "  fmod(10,3) = #{10 % 3}"               # remainder
puts "  hypot(3,4) = #{Math.hypot(3, 4)}"     # sqrt(x²+y²)
puts "  deg2rad(180) = #{Math::PI / 180 * 180}" # degrees → radians
puts "  rad2deg(pi) = #{Math::PI * 180 / Math::PI}" # radians → degrees



=begin
run:

Constants:
  pi = 3.141592653589793
  e  = 2.718281828459045

Core Functions:
  sqrt(2.5) = 1.5811388300841898
  exp(2.5)  = 12.182493960703473
  log(2.5)  = 0.9162907318741551
  log10(2.5)= 0.3979400086720376
  pow(2.5,3)= 15.625

Trigonometric Functions:
  sin(pi/4) = 0.7071067811865475
  cos(pi/4) = 0.7071067811865476
  tan(pi/4) = 0.9999999999999999
  asin(0.5) = 0.5235987755982989
  acos(0.5) = 1.0471975511965979
  atan(1.0) = 0.7853981633974483

Hyperbolic Functions:
  sinh(1) = 1.1752011936438014
  cosh(1) = 1.5430806348152437
  tanh(1) = 0.7615941559557649

Rounding Functions:
  floor(-2.7) = -3
  ceil(-2.7)  = -2
  round(-2.7) = -3
  trunc(-2.7) = -2

Min/Max/Clamp:
  min(10,20) = 10
  max(10,20) = 20
  clamp(15,0,10) = 10

Bitwise Math:
  X & Y = 88
  X | Y = ee
  X ^ Y = 66
  ~X    = 55
  X << 2 = a8
  Y >> 3 = 19

Additional Math:
  abs(-3.14) = 3.14
  fmod(10,3) = 1
  hypot(3,4) = 5.0
  deg2rad(180) = 3.141592653589793
  rad2deg(pi) = 180.0
  
=end

 



answered 1 day ago by avibootz
...