How to use math functions and constants in C

1 Answer

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

#include <stdio.h>      // printf
#include <math.h>       // math functions
#include <stdint.h>     // fixed-size integers
#include <limits.h>     // integer limits

int main(void) {

    /* ============================
       Constants
       ============================ */
    double pi = M_PI;        // π constant
    double e  = M_E;         // Euler's number

    printf("Constants:\n");
    printf("  pi = %f\n", pi);
    printf("  e  = %f\n\n", e);


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

    printf("Core Functions:\n");
    printf("  sqrt(2.5) = %f\n", sqrt(x));     // square root
    printf("  cbrt(2.5) = %f\n", cbrt(x));     // cube root
    printf("  exp(2.5)  = %f\n", exp(x));      // e^x
    printf("  log(2.5)  = %f\n", log(x));      // natural log
    printf("  log10(2.5)= %f\n", log10(x));    // base-10 log
    printf("  pow(2.5,3)= %f\n\n", pow(x,3));  // x^3


    /* ============================
       Trigonometric Functions
       ============================ */
    double angle = M_PI / 4; // 45 degrees in radians

    printf("Trigonometric Functions:\n");
    printf("  sin(pi/4) = %f\n", sin(angle));  // sine
    printf("  cos(pi/4) = %f\n", cos(angle));  // cosine
    printf("  tan(pi/4) = %f\n", tan(angle));  // tangent
    printf("  asin(0.5) = %f\n", asin(0.5));   // arcsine
    printf("  acos(0.5) = %f\n", acos(0.5));   // arccosine
    printf("  atan(1.0) = %f\n\n", atan(1.0)); // arctangent


    /* ============================
       Hyperbolic Functions
       ============================ */
    printf("Hyperbolic Functions:\n");
    printf("  sinh(1) = %f\n", sinh(1));       // hyperbolic sine
    printf("  cosh(1) = %f\n", cosh(1));       // hyperbolic cosine
    printf("  tanh(1) = %f\n", tanh(1));       // hyperbolic tangent
    printf("\n");


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

    printf("Rounding Functions:\n");
    printf("  floor(-2.7) = %f\n", floor(y));  // round down
    printf("  ceil(-2.7)  = %f\n", ceil(y));   // round up
    printf("  round(-2.7) = %f\n", round(y));  // nearest integer
    printf("  trunc(-2.7) = %f\n", trunc(y));  // remove decimals
    printf("\n");


    /* ============================
       Min / Max / Clamp
       ============================ */
    double a = 10, b = 20;   // values for min/max
    double value = 15;       // value to clamp
    double minV = 0, maxV = 10;

    double clamp = value < minV ? minV : (value > maxV ? maxV : value); // clamp logic

    printf("Min/Max/Clamp:\n");
    printf("  fmin(10,20) = %f\n", fmin(a,b)); // smaller of two
    printf("  fmax(10,20) = %f\n", fmax(a,b)); // larger of two
    printf("  clamp(15,0,10) = %f\n\n", clamp); // clamp result


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

    printf("Bitwise Math:\n");
    printf("  A & B = 0x%02X\n", A & B);       // bitwise AND
    printf("  A | B = 0x%02X\n", A | B);       // bitwise OR
    printf("  A ^ B = 0x%02X\n", A ^ B);       // bitwise XOR
    printf("  ~A    = 0x%02X\n", (uint8_t)~A); // bitwise NOT
    printf("  A << 2 = 0x%02X\n", A << 2);     // left shift
    printf("  B >> 3 = 0x%02X\n", B >> 3);     // right shift
    printf("\n");


    /* ============================
       Additional Useful Math
       ============================ */
    printf("Additional Math:\n");
    printf("  fabs(-3.14) = %f\n", fabs(-3.14)); // absolute value
    printf("  fmod(10,3)  = %f\n", fmod(10,3));  // remainder
    printf("  hypot(3,4)  = %f\n", hypot(3,4));  // sqrt(x²+y²)
    printf("  ldexp(1,4)  = %f\n", ldexp(1,4));  // 1 * 2^4
    printf("\n");

    return 0;
}


/* 
run:

onstants:
  pi = 3.141593
  e  = 2.718282

Core Functions:
  sqrt(2.5) = 1.581139
  cbrt(2.5) = 1.357209
  exp(2.5)  = 12.182494
  log(2.5)  = 0.916291
  log10(2.5)= 0.397940
  pow(2.5,3)= 15.625000

Trigonometric Functions:
  sin(pi/4) = 0.707107
  cos(pi/4) = 0.707107
  tan(pi/4) = 1.000000
  asin(0.5) = 0.523599
  acos(0.5) = 1.047198
  atan(1.0) = 0.785398

Hyperbolic Functions:
  sinh(1) = 1.175201
  cosh(1) = 1.543081
  tanh(1) = 0.761594

Rounding Functions:
  floor(-2.7) = -3.000000
  ceil(-2.7)  = -2.000000
  round(-2.7) = -3.000000
  trunc(-2.7) = -2.000000

Min/Max/Clamp:
  fmin(10,20) = 10.000000
  fmax(10,20) = 20.000000
  clamp(15,0,10) = 10.000000

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

Additional Math:
  fabs(-3.14) = 3.140000
  fmod(10,3)  = 1.000000
  hypot(3,4)  = 5.000000
  ldexp(1,4)  = 16.000000
  
*/

 



answered Apr 30 by avibootz
edited Apr 30 by avibootz
...