How to use math functions and constants in Kotlin

1 Answer

0 votes
// Program Title: Kotlin Math Functions Demonstration

import kotlin.math.*

fun main() {

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

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


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

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


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

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


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


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

    println("Rounding Functions:")
    println("  floor(-2.7) = ${floor(numY)}")   // round down
    println("  ceil(-2.7)  = ${ceil(numY)}")    // round up
    println("  round(-2.7) = ${numY.roundToInt()}") // nearest integer
    println("  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("  min(10,20) = ${min(numA, numB)}") // smaller of two
    println("  max(10,20) = ${max(numA, numB)}") // larger of two

    val clamped = valueToClamp.coerceIn(0, 10)   // clamp 15 to range 0–10
    println("  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("  X & Y = ${(byteX and byteY).toString(16)}") // AND
    println("  X | Y = ${(byteX or byteY).toString(16)}")  // OR
    println("  X ^ Y = ${(byteX xor byteY).toString(16)}") // XOR
    println("  ~X    = ${(byteX.inv() and 0xFF).toString(16)}") // NOT (mask to 8 bits)
    println("  X << 2 = ${((byteX shl 2) and 0xFF).toString(16)}") // left shift
    println("  Y >> 3 = ${(byteY shr 3).toString(16)}")            // right shift
    println()


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


/* 
run:

Constants:
  pi = 3.141592653589793
  e  = 2.718281828459045

Core Functions:
  sqrt(2.5) = 1.5811388300841898
  exp(2.5)  = 12.182493960703473
  ln(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
...