C Programming - Beginner to Brilliance / Math & Algorithm Support

Trigonometric & Logarithmic Functions

🔺 Trigonometric Functions in C

Trigonometric functions help us deal with angles and measurements in geometry, physics, graphics, and signal processing.

In C, these are provided by the math.h library. They use radians instead of degrees (important!).

✅ Header:

#include <math.h>

🎯 Why radians?

Because C’s math functions are modeled on calculus-based mathematics where radian measure is standard.

📐 Formula:

radians = degrees × (π / 180)

📏 Common Trigonometric Functions

Function

Meaning

Input

Output

sin(x)

Sine

radians

float/double

cos(x)

Cosine

radians

float/double

tan(x)

Tangent

radians

float/double

asin(x)

Arc Sine (inverse)

value between -1 to 1

angle in radians

acos(x)

Arc Cosine (inverse)

value between -1 to 1

angle in radians

atan(x)

Arc Tangent (inverse)

any real value

angle in radians


✅ Example:

#include <stdio.h>
#include <math.h>

int main() {
    double degrees = 30.0;
    double radians = degrees * (M_PI / 180.0);  // Convert to radians

    printf("sin(30°) = %.2f\\n", sin(radians)); // Should be ~0.5
    printf("cos(30°) = %.2f\\n", cos(radians)); // ~0.866
    printf("tan(30°) = %.2f\\n", tan(radians)); // ~0.577

    return 0;
}

🧠 Did You Know?

  • M_PI is defined in math.h (on GNU/Linux or with some compilers). If not, you can define it:

#define M_PI 3.14159265358979323846

⚠️ Warning:

  • tan(x) becomes undefined at certain angles like π/2 (90°) or 3π/2 (270°) — it will give huge values or cause runtime errors due to division by zero.


🔍 Inverse Trigonometric Functions

These return the angle when you know the ratio.

Function

Returns angle in radians

asin(x)

sin⁻¹(x), x ∈ [-1,1]

acos(x)

cos⁻¹(x), x ∈ [-1,1]

atan(x)

tan⁻¹(x), x ∈ ℝ


✅ Example:

#include <stdio.h>
#include <math.h>

int main() {
    double value = 0.5;

    printf("asin(0.5) = %.2f radians\\n", asin(value)); // ≈ 0.52
    printf("acos(0.5) = %.2f radians\\n", acos(value)); // ≈ 1.04
    printf("atan(1) = %.2f radians\\n", atan(1));       // ≈ 0.785

    return 0;
}

📈 Logarithmic Functions in C

Used in:

  • Scientific calculations

  • Complexity analysis

  • Signal strength analysis

  • Growth/decay modeling

All logarithmic functions return double and are declared in <math.h>.


🔣 Types of Logarithmic Functions

Function

Description

Example

log(x)

Natural log (base e)

log(2.718) ≈ 1.0

log10(x)

Log base 10

log10(1000) ≈ 3.0

log2(x)

Log base 2 (not always available)

log2(8) ≈ 3.0

exp(x)

e^x (exponential)

exp(1) ≈ 2.718

✅ Example:

#include <stdio.h>
#include <math.h>

int main() {
    double num = 1000;

    printf("log10(1000) = %.2f\\n", log10(num));   // 3.0
    printf("log(2.718) = %.2f\\n", log(2.718));    // ~1.0
    printf("exp(2) = %.2f\\n", exp(2));            // ~7.38

    return 0;
}

💥 Common Use-Cases

Application

Function

Compute compound interest

pow() + log()

Find number of digits in a number

floor(log10(n)) + 1

Solve exponent equations

log() or log10()

Growth modeling

exp()