🔺 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 |
|---|---|---|---|
| Sine | radians | float/double |
| Cosine | radians | float/double |
| Tangent | radians | float/double |
| Arc Sine (inverse) | value between -1 to 1 | angle in radians |
| Arc Cosine (inverse) | value between -1 to 1 | angle in radians |
| 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_PIis defined inmath.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°) or3π/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 |
|---|---|
| sin⁻¹(x), x ∈ [-1,1] |
| cos⁻¹(x), x ∈ [-1,1] |
| 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 |
|---|---|---|
| Natural log (base e) |
|
| Log base 10 |
|
| Log base 2 (not always available) |
|
| e^x (exponential) |
|
✅ 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 |
|
Find number of digits in a number |
|
Solve exponent equations |
|
Growth modeling |
|