How to use math functions and constants in Swift

1 Answer

0 votes
// Program Title: Swift Math Functions Demonstration

import Foundation   // Needed for hyperbolic functions

// ============================
// Constants
// ============================
let piValue = Double.pi        // π constant
let eValue  = M_E              // Euler's number

print("Constants:")
print("  pi = \(piValue)")
print("  e  = \(eValue)")
print()


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

print("Core Functions:")
print("  sqrt(2.5) = \(sqrt(numX))")       // square root
print("  exp(2.5)  = \(exp(numX))")        // e^x
print("  log(2.5)  = \(log(numX))")        // natural log
print("  log10(2.5)= \(log10(numX))")      // base‑10 log
print("  pow(2.5,3)= \(pow(numX, 3))")     // x^3
print()


// ============================
// Trigonometric Functions
// ============================
let angle = Double.pi / 4      // 45 degrees in radians

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


// ============================
// Hyperbolic Functions
// ============================
print("Hyperbolic Functions:")
print("  sinh(1) = \(sinh(1.0))")          // hyperbolic sine
print("  cosh(1) = \(cosh(1.0))")          // hyperbolic cosine
print("  tanh(1) = \(tanh(1.0))")          // hyperbolic tangent
print()


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

print("Rounding Functions:")
print("  floor(-2.7) = \(floor(numY))")    // round down
print("  ceil(-2.7)  = \(ceil(numY))")     // round up
print("  round(-2.7) = \(numY.rounded())") // nearest integer
print("  trunc(-2.7) = \(numY.rounded(.towardZero))") // remove decimals
print()


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

print("Min/Max/Clamp:")
print("  min(10,20) = \(min(numA, numB))") // smaller of two
print("  max(10,20) = \(max(numA, numB))") // larger of two

let clamped = max(0.0, min(10.0, valueToClamp)) // clamp 15 to range 0–10
print("  clamp(15,0,10) = \(clamped)")
print()


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

print("Bitwise Math:")
print("  X & Y = \(String(byteX & byteY, radix: 16))")      // AND
print("  X | Y = \(String(byteX | byteY, radix: 16))")      // OR
print("  X ^ Y = \(String(byteX ^ byteY, radix: 16))")      // XOR
print("  ~X    = \(String(~byteX & 0xFF, radix: 16))")      // NOT (mask to 8 bits)
print("  X << 2 = \(String((byteX << 2) & 0xFF, radix: 16))") // left shift
print("  Y >> 3 = \(String(byteY >> 3, radix: 16))")          // right shift
print()


// ============================
// Additional Useful Math
// ============================
print("Additional Math:")
print("  abs(-3.14) = \(abs(-3.14))")          // absolute value
print("  fmod(10,3) = \(10.0.truncatingRemainder(dividingBy: 3.0))") // remainder
print("  hypot(3,4) = \(hypot(3.0, 4.0))")     // sqrt(x²+y²)
print("  deg2rad(180) = \(180.0 * .pi / 180.0)") // degrees -> radians
print("  rad2deg(pi) = \(Double.pi * 180.0 / .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.5430806348152437
  tanh(1) = 0.7615941559557649

Rounding Functions:
  floor(-2.7) = -3.0
  ceil(-2.7)  = -2.0
  round(-2.7) = -3.0
  trunc(-2.7) = -2.0

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

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.0
  hypot(3,4) = 5.0
  deg2rad(180) = 3.141592653589793
  rad2deg(pi) = 180.0
  
*/

 



answered Apr 30 by avibootz
...