How to use math functions and constants in TypeScript

1 Answer

0 votes
// Program Title: TypeScript Math Functions Demonstration

// ============================
// Constants
// ============================
const piValue: number = Math.PI;   // π constant
const eValue: number = Math.E;     // Euler's number

console.log("Constants:");
console.log("  pi =", piValue);
console.log("  e  =", eValue);
console.log();


// ============================
// Core Functions
// ============================
const numX: number = 2.5;          // sample value

console.log("Core Functions:");
console.log("  sqrt(2.5) =", Math.sqrt(numX));     // square root
console.log("  exp(2.5)  =", Math.exp(numX));      // e^x
console.log("  log(2.5)  =", Math.log(numX));      // natural log
console.log("  log10(2.5)=", Math.log10(numX));    // base-10 log
console.log("  pow(2.5,3)=", Math.pow(numX, 3));   // x^3
console.log();


// ============================
// Trigonometric Functions
// ============================
const angle: number = Math.PI / 4; // 45 degrees in radians

console.log("Trigonometric Functions:");
console.log("  sin(pi/4) =", Math.sin(angle));     // sine
console.log("  cos(pi/4) =", Math.cos(angle));     // cosine
console.log("  tan(pi/4) =", Math.tan(angle));     // tangent
console.log("  asin(0.5) =", Math.asin(0.5));      // arcsine
console.log("  acos(0.5) =", Math.acos(0.5));      // arccosine
console.log("  atan(1.0) =", Math.atan(1.0));      // arctangent
console.log();


// ============================
// Hyperbolic Functions
// ============================
console.log("Hyperbolic Functions:");
console.log("  sinh(1) =", Math.sinh(1));          // hyperbolic sine
console.log("  cosh(1) =", Math.cosh(1));          // hyperbolic cosine
console.log("  tanh(1) =", Math.tanh(1));          // hyperbolic tangent
console.log();


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

console.log("Rounding Functions:");
console.log("  floor(-2.7) =", Math.floor(numY));  // round down
console.log("  ceil(-2.7)  =", Math.ceil(numY));   // round up
console.log("  round(-2.7) =", Math.round(numY));  // nearest integer
console.log("  trunc(-2.7) =", Math.trunc(numY));  // remove decimals
console.log();


// ============================
// Min / Max / Clamp
// ============================
const numA: number = 10;
const numB: number = 20;
const valueToClamp: number = 15;

console.log("Min/Max/Clamp:");
console.log("  min(10,20) =", Math.min(numA, numB)); // smaller of two
console.log("  max(10,20) =", Math.max(numA, numB)); // larger of two

// clamp manually
const clamped: number = Math.max(0, Math.min(10, valueToClamp)); // clamp 15 to range 0–10
console.log("  clamp(15,0,10) =", clamped);
console.log();


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

console.log("Bitwise Math:");
console.log("  A & B =", (byteA & byteB).toString(16)); // AND
console.log("  A | B =", (byteA | byteB).toString(16)); // OR
console.log("  A ^ B =", (byteA ^ byteB).toString(16)); // XOR
console.log("  ~A    =", (~byteA & 0xFF).toString(16)); // NOT (mask to 8 bits)
console.log("  A << 2 =", ((byteA << 2) & 0xFF).toString(16)); // left shift
console.log("  B >> 3 =", (byteB >> 3).toString(16));          // right shift
console.log();


// ============================
// Additional Useful Math
// ============================
console.log("Additional Math:");
console.log("  abs(-3.14) =", Math.abs(-3.14));          // absolute value
console.log("  fmod(10,3) =", 10 % 3);                   // remainder
console.log("  hypot(3,4) =", Math.hypot(3, 4));         // sqrt(x²+y²)
console.log("  deg2rad(180) =", 180 * (Math.PI / 180));  // degrees → radians
console.log("  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) = 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 = 88
  A | B = ee
  A ^ B = 66
  ~A    = 55
  A << 2 = a8
  B >> 3 = 19

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

 



answered 1 day ago by avibootz
...