6.11.110 logf Function
Calculates the natural logarithm of a single precision floating-point value.
Include
<math.h>
Prototype
float log(float x);
Argument
x
- any positive value for which to return the log
Return Value
Returns the natural (base-) logarithm of x
. If x
is 0, infinity is returned. If x
is a negative number, NaN is
returned.
Remarks
If x
< 0, a domain error will occur and errno
will be
set to EDOM
.
Example Output
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = 2.0F;
y = logf(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
errno = 0;
x = 0.0F;
y = logf(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
errno = 0;
x = -2.0F;
y = logf(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
}
Example Output
The natural logarithm of 2.000000 is 0.693147
The natural logarithm of 0.000000 is -inf
Error: domain error
The natural logarithm of -2.000000 is nan