Home >Backend Development >C#.Net Tutorial >How to express natural logarithm ln in C language
In C, the natural logarithm (base e) is represented by the mathematical function log(), which accepts a positive real number and returns its natural logarithm.
Representation of natural logarithm ln in C language
In C language, natural logarithm (with e (base) is represented by the mathematical function log()
. This function returns a double-precision floating-point number that is the natural logarithm of the given argument.
Syntax:
<code class="c">double log(double x);</code>
Parameters:
x
- To calculate the natural logarithm of positive real numbers. Return value: The natural logarithm of
x
, that is, ln(x)
. Example:
<code class="c">#include <stdio.h> #include <math.h> int main() { double x = 2.71828; double ln_x = log(x); printf("e 的自然对数 (ln(e)) = %lf\n", ln_x); return 0; }</code>
Output:
<code>e 的自然对数 (ln(e)) = 1.000000</code>
Note:
log()
The function only accepts positive real numbers as parameters. Passing a negative number or zero will result in undefined behavior. log()
The function returns a double precision floating point number, so the results may vary slightly due to rounding errors in floating point operations. The above is the detailed content of How to express natural logarithm ln in C language. For more information, please follow other related articles on the PHP Chinese website!