log() returns the natural logarithm of a positive value, using base e. Its argument must be greater than zero for an ordinary real-valued result.
double log(double x);
#include <math.h>
#include <stdio.h>
int main(void) {
double value = 10.0;
printf("log(10) = %.6f\n", log(value));
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
double e_squared = exp(2.0);
printf("%.2f\n", log(e_squared));
return 0;
}
#include <math.h>
#include <stdio.h>
double log_base(double value, double base) {
return log(value) / log(base);
}
int main(void) {
double value = 8.0;
double base = 2.0;
if (value <= 0.0 || base <= 0.0 || base == 1.0) {
printf("Invalid logarithm input.\n");
return 1;
}
printf("log base %.0f of %.0f = %.2f\n",
base, value, log_base(value, base));
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
double values[] = {1000.0, 0.0, -5.0};
size_t count = sizeof values / sizeof values[0];
for (size_t i = 0; i < count; i++) {
if (values[i] <= 0.0) {
printf("log(%.1f) is outside the real-number domain.\n",
values[i]);
} else {
printf("log(%.1f) = %.6f\n",
values[i], log(values[i]));
}
}
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.