Header file math.h is for mathematical functions like cos,sin, tan.. But how to write the ln function and not log?
How can you use Ln function in C programing?
double ln(double x) { return log(x); }
Or in a pinch
#define ln(x) log(x)
double ln(double x) {
// ln(1 - y) = sum of -(y ^ k) / k from k = 1 to n but only if 0 < y < 1
// taylor expansion formula of ln(x): https://furthermathematicst.blogspot.com/2011/06/71-taylor-polynomial-part-1.html
int exponent = 0; // map the value x between 0 and 1
while (x > 1.0) {x /= 2.0; exponent++;}
while (x < 0.0) {x *= 2.0; exponent--;}
int k, n = 1000; // iteration
double y = 1 - x; // ln(1 - y) = ln(x) >> ln(y) = ln(1 - x)
double initial_y = y;
double result = 0;
for (k = 1; k <= n; k++) { // sum of -(y ^ k) / k from k = 1 to n
result += y / k;
y *= initial_y; // y ^ k
}
return -1 * result + exponent * LN2;
}
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. –
Bendite
© 2022 - 2025 — McMap. All rights reserved.
log
. What you're thinking of as log (log base 10) islog10
in the library. – Notebooklog
computes log base e. – Notebook