How to use math functions and constants in Python

1 Answer

0 votes
# Program Title: Python Math Functions Demonstration

import math  # math module contains most math functions

# ============================
# Constants
# ============================
pi_value = math.pi          # π constant
e_value = math.e            # Euler's number

print("Constants:")
print("  pi =", pi_value)
print("  e  =", e_value)
print()


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

print("Core Functions:")
print("  sqrt(2.5) =", math.sqrt(num_x))     # square root
print("  exp(2.5)  =", math.exp(num_x))      # e^x
print("  log(2.5)  =", math.log(num_x))      # natural log
print("  log10(2.5)=", math.log10(num_x))    # base-10 log
print("  pow(2.5,3)=", math.pow(num_x, 3))   # x^3
print()


# ============================
# Trigonometric Functions
# ============================
angle = math.pi / 4  # 45 degrees in radians

print("Trigonometric Functions:")
print("  sin(pi/4) =", math.sin(angle))      # sine
print("  cos(pi/4) =", math.cos(angle))      # cosine
print("  tan(pi/4) =", math.tan(angle))      # tangent
print("  asin(0.5) =", math.asin(0.5))       # arcsine
print("  acos(0.5) =", math.acos(0.5))       # arccosine
print("  atan(1.0) =", math.atan(1.0))       # arctangent
print()


# ============================
# Hyperbolic Functions
# ============================
print("Hyperbolic Functions:")
print("  sinh(1) =", math.sinh(1))           # hyperbolic sine
print("  cosh(1) =", math.cosh(1))           # hyperbolic cosine
print("  tanh(1) =", math.tanh(1))           # hyperbolic tangent
print()


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

print("Rounding Functions:")
print("  floor(-2.7) =", math.floor(num_y))  # round down
print("  ceil(-2.7)  =", math.ceil(num_y))   # round up
print("  round(-2.7) =", round(num_y))       # nearest integer
print("  trunc(-2.7) =", math.trunc(num_y))  # remove decimals
print()


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

print("Min/Max/Clamp:")
print("  min(10,20) =", min(num_a, num_b))   # smaller of two
print("  max(10,20) =", max(num_a, num_b))   # larger of two

# clamp manually
clamped = max(0, min(10, value_to_clamp))    # clamp 15 to range 0–10
print("  clamp(15,0,10) =", clamped)
print()


# ============================
# Bitwise Math (Integers)
# ============================
byte_a = 0xAA  # 10101010 in hex
byte_b = 0xCC  # 11001100 in hex

print("Bitwise Math:")
print("  A & B =", hex(byte_a & byte_b))     # AND
print("  A | B =", hex(byte_a | byte_b))     # OR
print("  A ^ B =", hex(byte_a ^ byte_b))     # XOR
print("  ~A    =", hex(~byte_a & 0xFF))      # NOT (mask to 8 bits)
print("  A << 2 =", hex((byte_a << 2) & 0xFF))  # left shift
print("  B >> 3 =", hex(byte_b >> 3))        # right shift
print()


# ============================
# Additional Useful Math
# ============================
print("Additional Math:")
print("  abs(-3.14) =", abs(-3.14))          # absolute value
print("  fmod(10,3) =", math.fmod(10, 3))    # remainder
print("  hypot(3,4) =", math.hypot(3, 4))    # sqrt(x²+y²)
print("  degrees(pi) =", math.degrees(math.pi))  # radians → degrees
print("  radians(180) =", math.radians(180))     # degrees → radians


"""
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:
  A & B = 0x88
  A | B = 0xee
  A ^ B = 0x66
  ~A    = 0x55
  A << 2 = 0xa8
  B >> 3 = 0x19

Additional Math:
  abs(-3.14) = 3.14
  fmod(10,3) = 1.0
  hypot(3,4) = 5.0
  degrees(pi) = 180.0
  radians(180) = 3.141592653589793

"""

 



answered 1 day ago by avibootz
...