How to use math functions and constants in Go

1 Answer

0 votes
// Program Title: Go Math Functions Demonstration

package main

import (
    "fmt"
    "math"
)

func main() {

    // ============================
    // Constants
    // ============================
    piValue := math.Pi        // π constant
    eValue := math.E          // Euler's number

    fmt.Println("Constants:")
    fmt.Println("  pi =", piValue)
    fmt.Println("  e  =", eValue)
    fmt.Println()

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

    fmt.Println("Core Functions:")
    fmt.Println("  sqrt(2.5) =", math.Sqrt(numX))   // square root
    fmt.Println("  exp(2.5)  =", math.Exp(numX))    // e^x
    fmt.Println("  log(2.5)  =", math.Log(numX))    // natural log
    fmt.Println("  log10(2.5)=", math.Log10(numX))  // base-10 log
    fmt.Println("  pow(2.5,3)=", math.Pow(numX, 3)) // x^3
    fmt.Println()

    // ============================
    // Trigonometric Functions
    // ============================
    angle := math.Pi / 4 // 45 degrees in radians

    fmt.Println("Trigonometric Functions:")
    fmt.Println("  sin(pi/4) =", math.Sin(angle)) // sine
    fmt.Println("  cos(pi/4) =", math.Cos(angle)) // cosine
    fmt.Println("  tan(pi/4) =", math.Tan(angle)) // tangent
    fmt.Println("  asin(0.5) =", math.Asin(0.5))  // arcsine
    fmt.Println("  acos(0.5) =", math.Acos(0.5))  // arccosine
    fmt.Println("  atan(1.0) =", math.Atan(1.0))  // arctangent
    fmt.Println()

    // ============================
    // Hyperbolic Functions
    // ============================
    fmt.Println("Hyperbolic Functions:")
    fmt.Println("  sinh(1) =", math.Sinh(1)) // hyperbolic sine
    fmt.Println("  cosh(1) =", math.Cosh(1)) // hyperbolic cosine
    fmt.Println("  tanh(1) =", math.Tanh(1)) // hyperbolic tangent
    fmt.Println()

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

    fmt.Println("Rounding Functions:")
    fmt.Println("  floor(-2.7) =", math.Floor(numY)) // round down
    fmt.Println("  ceil(-2.7)  =", math.Ceil(numY))  // round up
    fmt.Println("  round(-2.7) =", math.Round(numY)) // nearest integer
    fmt.Println("  trunc(-2.7) =", math.Trunc(numY)) // remove decimals
    fmt.Println()

    // ============================
    // Min / Max / Clamp
    // ============================
    numA := 10.0
    numB := 20.0
    valueToClamp := 15.0

    fmt.Println("Min/Max/Clamp:")
    fmt.Println("  min(10,20) =", math.Min(numA, numB)) // smaller of two
    fmt.Println("  max(10,20) =", math.Max(numA, numB)) // larger of two

    // clamp manually
    clamped := math.Max(0, math.Min(10, valueToClamp)) // clamp 15 to range 0–10
    fmt.Println("  clamp(15,0,10) =", clamped)
    fmt.Println()

    // ============================
    // Bitwise Math (Integers)
    // ============================
    byteA := 0xAA // 10101010 in hex
    byteB := 0xCC // 11001100 in hex

    fmt.Println("Bitwise Math:")
    fmt.Printf("  A & B = %X\n", byteA&byteB)        // AND
    fmt.Printf("  A | B = %X\n", byteA|byteB)        // OR
    fmt.Printf("  A ^ B = %X\n", byteA^byteB)        // XOR
    fmt.Printf("  ~A    = %X\n", ^byteA&0xFF)        // NOT (mask to 8 bits)
    fmt.Printf("  A << 2 = %X\n", (byteA<<2)&0xFF)   // left shift
    fmt.Printf("  B >> 3 = %X\n", byteB>>3)          // right shift
    fmt.Println()

    // ============================
    // Additional Useful Math
    // ============================
    fmt.Println("Additional Math:")
    fmt.Println("  abs(-3.14) =", math.Abs(-3.14))         // absolute value
    fmt.Println("  mod(10,3)  =", math.Mod(10, 3))         // remainder
    fmt.Println("  hypot(3,4) =", math.Hypot(3, 4))        // sqrt(x²+y²)
    fmt.Println("  deg2rad(180) =", 180*(math.Pi/180))     // degrees → radians
    fmt.Println("  rad2deg(pi) =", math.Pi*(180/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) = 1
  asin(0.5) = 0.5235987755982989
  acos(0.5) = 1.0471975511965976
  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 = 88
  A | B = EE
  A ^ B = 66
  ~A    = 55
  A << 2 = A8
  B >> 3 = 19

Additional Math:
  abs(-3.14) = 3.14
  mod(10,3)  = 1
  hypot(3,4) = 5
  deg2rad(180) = 3.141592653589793
  rad2deg(pi) = 180
  
*/


 



answered Apr 30 by avibootz
...