Practical Math Examples
Mathematical Function Categories
graph LR
A[Math Functions] --> B[Trigonometric]
A --> C[Exponential]
A --> D[Rounding]
A --> E[Statistical]
Trigonometric Functions
Sine and Cosine Calculation
#include <stdio.h>
#include <math.h>
int main() {
double angle = M_PI / 4; // 45 degrees
printf("Sin(45ยฐ): %.2f\n", sin(angle));
printf("Cos(45ยฐ): %.2f\n", cos(angle));
return 0;
}
Exponential and Logarithmic Operations
Power and Logarithm Example
#include <stdio.h>
#include <math.h>
int main() {
double base = 2.0;
double exponent = 3.0;
printf("Power: %.2f^%.2f = %.2f\n", base, exponent, pow(base, exponent));
printf("Natural Log: log(%.2f) = %.2f\n", base, log(base));
printf("Base 10 Log: log10(%.2f) = %.2f\n", base, log10(base));
return 0;
}
Rounding Functions
Rounding Techniques
#include <stdio.h>
#include <math.h>
int main() {
double number = 3.7;
printf("Ceiling: %.2f -> %.2f\n", number, ceil(number));
printf("Floor: %.2f -> %.2f\n", number, floor(number));
printf("Round: %.2f -> %.2f\n", number, round(number));
return 0;
}
Statistical Calculations
Standard Deviation Example
#include <stdio.h>
#include <math.h>
double calculate_std_deviation(double data[], int size) {
double sum = 0.0, mean, variance = 0.0;
// Calculate mean
for (int i = 0; i < size; i++) {
sum += data[i];
}
mean = sum / size;
// Calculate variance
for (int i = 0; i < size; i++) {
variance += pow(data[i] - mean, 2);
}
variance /= size;
return sqrt(variance);
}
int main() {
double data[] = {2, 4, 4, 4, 5, 5, 7, 9};
int size = sizeof(data) / sizeof(data[0]);
printf("Standard Deviation: %.2f\n",
calculate_std_deviation(data, size));
return 0;
}
Mathematical Function Reference
Function |
Description |
Example |
sin() |
Sine calculation |
sin(M_PI/2) |
cos() |
Cosine calculation |
cos(M_PI) |
pow() |
Power operation |
pow(2, 3) |
sqrt() |
Square root |
sqrt(16) |
log() |
Natural logarithm |
log(10) |
LabEx Learning Approach
At LabEx, we recommend practicing these examples and exploring various mathematical scenarios to build a comprehensive understanding of math functions.
Error Handling Considerations
- Check for domain errors
- Handle potential overflow
- Use appropriate data types
- Validate input ranges
Compilation Reminder
Remember to compile with the math library:
gcc -o math_example math_example.c -lm