How to use math functions and constants in Java

1 Answer

0 votes
/** Program Title: C# Math Functions Demonstration */

using System;

class Program
{
    static void Main()
    {
        /** ============================
           Constants
           ============================ */
        double pi = Math.PI;        // π constant
        double e  = Math.E;         // Euler's number

        Console.WriteLine("Constants:");
        Console.WriteLine("  pi = " + pi);
        Console.WriteLine("  e  = " + e);
        Console.WriteLine();


        /** ============================
           Core Functions
           ============================ */
        double x = 2.5;             // sample value

        Console.WriteLine("Core Functions:");
        Console.WriteLine("  Sqrt(2.5) = " + Math.Sqrt(x));   // square root
        Console.WriteLine("  Exp(2.5)  = " + Math.Exp(x));    // e^x
        Console.WriteLine("  Log(2.5)  = " + Math.Log(x));    // natural log
        Console.WriteLine("  Log10(2.5)= " + Math.Log10(x));  // base-10 log
        Console.WriteLine("  Pow(2.5,3)= " + Math.Pow(x, 3)); // x^3
        Console.WriteLine();


        /** ============================
           Trigonometric Functions
           ============================ */
        double angle = Math.PI / 4; // 45 degrees in radians

        Console.WriteLine("Trigonometric Functions:");
        Console.WriteLine("  Sin(pi/4) = " + Math.Sin(angle)); // sine
        Console.WriteLine("  Cos(pi/4) = " + Math.Cos(angle)); // cosine
        Console.WriteLine("  Tan(pi/4) = " + Math.Tan(angle)); // tangent
        Console.WriteLine("  Asin(0.5) = " + Math.Asin(0.5));  // arcsine
        Console.WriteLine("  Acos(0.5) = " + Math.Acos(0.5));  // arccosine
        Console.WriteLine("  Atan(1.0) = " + Math.Atan(1.0));  // arctangent
        Console.WriteLine();


        /** ============================
           Hyperbolic Functions
           ============================ */
        Console.WriteLine("Hyperbolic Functions:");
        Console.WriteLine("  Sinh(1) = " + Math.Sinh(1));     // hyperbolic sine
        Console.WriteLine("  Cosh(1) = " + Math.Cosh(1));     // hyperbolic cosine
        Console.WriteLine("  Tanh(1) = " + Math.Tanh(1));     // hyperbolic tangent
        Console.WriteLine();


        /** ============================
           Rounding Functions
           ============================ */
        double y = -2.7;            // negative number for rounding tests

        Console.WriteLine("Rounding Functions:");
        Console.WriteLine("  Floor(-2.7) = " + Math.Floor(y));   // round down
        Console.WriteLine("  Ceiling(-2.7)= " + Math.Ceiling(y)); // round up
        Console.WriteLine("  Round(-2.7) = " + Math.Round(y));    // nearest integer
        Console.WriteLine("  Truncate(-2.7) = " + Math.Truncate(y)); // remove decimals
        Console.WriteLine();


        /** ============================
           Min / Max / Clamp
           ============================ */
        double a = 10;
        double b = 20;
        double value = 15;

        Console.WriteLine("Min/Max/Clamp:");
        Console.WriteLine("  Min(10,20) = " + Math.Min(a, b)); // smaller of two
        Console.WriteLine("  Max(10,20) = " + Math.Max(a, b)); // larger of two

        // clamp using Math.Min/Max
        double clamped = Math.Max(0, Math.Min(10, value)); // clamp 15 to range 0–10
        Console.WriteLine("  Clamp(15,0,10) = " + clamped);
        Console.WriteLine();


        /** ============================
           Bitwise Math (Integers)
           ============================ */
        byte A = 0b10101010;        // binary literal
        byte B = 0b11001100;        // binary literal

        Console.WriteLine("Bitwise Math:");
        Console.WriteLine("  A & B = 0x" + (A & B).ToString("X2")); // AND
        Console.WriteLine("  A | B = 0x" + (A | B).ToString("X2")); // OR
        Console.WriteLine("  A ^ B = 0x" + (A ^ B).ToString("X2")); // XOR
        Console.WriteLine("  ~A    = 0x" + ((byte)~A).ToString("X2")); // NOT
        Console.WriteLine("  A << 2 = 0x" + (A << 2).ToString("X2")); // left shift
        Console.WriteLine("  B >> 3 = 0x" + (B >> 3).ToString("X2")); // right shift
        Console.WriteLine();


        /** ============================
           Additional Useful Math
           ============================ */
        Console.WriteLine("Additional Math:");
        Console.WriteLine("  Abs(-3.14) = " + Math.Abs(-3.14)); // absolute value
        Console.WriteLine("  IEEERemainder(10,3) = " + Math.IEEERemainder(10, 3)); // IEEE remainder
        Console.WriteLine("  Sqrt(3^2 + 4^2) = " + Math.Sqrt(3 * 3 + 4 * 4)); // manual hypot
        Console.WriteLine("  Exp(4) = " + Math.Exp(4)); // e^4
    }
}



/* 
run:

Constants:
  pi = 3.14159265358979
  e  = 2.71828182845905

Core Functions:
  Sqrt(2.5) = 1.58113883008419
  Exp(2.5)  = 12.1824939607035
  Log(2.5)  = 0.916290731874155
  Log10(2.5)= 0.397940008672038
  Pow(2.5,3)= 15.625

Trigonometric Functions:
  Sin(pi/4) = 0.707106781186547
  Cos(pi/4) = 0.707106781186548
  Tan(pi/4) = 1
  Asin(0.5) = 0.523598775598299
  Acos(0.5) = 1.0471975511966
  Atan(1.0) = 0.785398163397448

Hyperbolic Functions:
  Sinh(1) = 1.1752011936438
  Cosh(1) = 1.54308063481524
  Tanh(1) = 0.761594155955765

Rounding Functions:
  Floor(-2.7) = -3
  Ceiling(-2.7)= -2
  Round(-2.7) = -3
  Truncate(-2.7) = -2

Min/Max/Clamp:
  Min(10,20) = 10
  Max(10,20) = 20
  Clamp(15,0,10) = 10

Bitwise Math:
  A & B = 0x88
  A | B = 0xEE
  A ^ B = 0x66
  ~A    = 0x55
  A << 2 = 0x2A8
  B >> 3 = 0x19

Additional Math:
  Abs(-3.14) = 3.14
  IEEERemainder(10,3) = 1
  Sqrt(3^2 + 4^2) = 5
  Exp(4) = 54.5981500331442
  
*/

 



answered 2 days ago by avibootz
edited 1 day ago by avibootz
...