How to use math functions and constants in Scala

1 Answer

0 votes
/// Program Title: Scala Math Functions Demonstration

object MathDemo extends App {

  // ============================
  // Constants
  // ============================
  val piValue = Math.PI        // π constant
  val eValue  = Math.E         // Euler's number

  println("Constants:")
  println(s"  pi = $piValue")
  println(s"  e  = $eValue")
  println()


  // ============================
  // Core Functions
  // ============================
  val numX = 2.5               // sample value

  println("Core Functions:")
  println(s"  sqrt(2.5) = ${Math.sqrt(numX)}")     // square root
  println(s"  exp(2.5)  = ${Math.exp(numX)}")      // e^x
  println(s"  log(2.5)  = ${Math.log(numX)}")      // natural log
  println(s"  log10(2.5)= ${Math.log10(numX)}")    // base‑10 log
  println(s"  pow(2.5,3)= ${Math.pow(numX, 3)}")   // x^3
  println()


  // ============================
  // Trigonometric Functions
  // ============================
  val angle = Math.PI / 4      // 45 degrees in radians

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


  // ============================
  // Hyperbolic Functions
  // ============================
  println("Hyperbolic Functions:")
  println(s"  sinh(1) = ${Math.sinh(1)}")          // hyperbolic sine
  println(s"  cosh(1) = ${Math.cosh(1)}")          // hyperbolic cosine
  println(s"  tanh(1) = ${Math.tanh(1)}")          // hyperbolic tangent
  println()


  // ============================
  // Rounding Functions
  // ============================
  val numY = -2.7             // negative number for rounding tests

  println("Rounding Functions:")
  println(s"  floor(-2.7) = ${Math.floor(numY)}")  // round down
  println(s"  ceil(-2.7)  = ${Math.ceil(numY)}")   // round up
  println(s"  round(-2.7) = ${Math.round(numY)}")  // nearest integer
  println(s"  trunc(-2.7) = ${numY.toInt}")        // remove decimals
  println()


  // ============================
  // Min / Max / Clamp
  // ============================
  val numA = 10
  val numB = 20
  val valueToClamp = 15

  println("Min/Max/Clamp:")
  println(s"  min(10,20) = ${Math.min(numA, numB)}") // smaller of two
  println(s"  max(10,20) = ${Math.max(numA, numB)}") // larger of two

  // clamp manually
  val clamped = valueToClamp.max(0).min(10)          // clamp 15 to range 0–10
  println(s"  clamp(15,0,10) = $clamped")
  println()


  // ============================
  // Bitwise Math (Integers)
  // ============================
  val byteX = 0xAA        // 10101010 in hex
  val byteY = 0xCC        // 11001100 in hex

  println("Bitwise Math:")
  println(f"  X & Y = ${byteX & byteY}%X")          // AND
  println(f"  X | Y = ${byteX | byteY}%X")          // OR
  println(f"  X ^ Y = ${byteX ^ byteY}%X")          // XOR
  println(f"  ~X    = ${~byteX & 0xFF}%X")          // NOT (mask to 8 bits)
  println(f"  X << 2 = ${(byteX << 2) & 0xFF}%X")   // left shift
  println(f"  Y >> 3 = ${byteY >> 3}%X")            // right shift
  println()


  // ============================
  // Additional Useful Math
  // ============================
  println("Additional Math:")
  println(s"  abs(-3.14) = ${Math.abs(-3.14)}")     // absolute value
  println(s"  fmod(10,3) = ${10 % 3}")              // remainder
  println(s"  hypot(3,4) = ${Math.hypot(3, 4)}")    // sqrt(x²+y²)
  println(s"  deg2rad(180) = ${Math.toRadians(180)}") // degrees -> radians
  println(s"  rad2deg(pi) = ${Math.toDegrees(Math.PI)}") // radians -> degrees
}


/* 
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.543080634815244
  tanh(1) = 0.7615941559557649

Rounding Functions:
  floor(-2.7) = -3.0
  ceil(-2.7)  = -2.0
  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
  
*/

 



answered Apr 30 by avibootz
...