log() Calculate Natural Logarithm
#include <math.h>
double log(x);
double x; Floating-point value
log() returns the natural logarithm of 'x'.
Returns: Natural logarithm of 'x'. If 'x' is negative, matherr()
is called with a DOMAIN error, a DOMAIN error message is
printed to 'stderr', 'errno' (defined in <stdlib.h>) is
set to EDOM (defined in <math.h>), and the value negative
HUGE (defined in <math.h>) is returned. If 'x' is 0.0,
matherr() is called with a SING (singularity) error, a
SING error message is printed to 'stderr', 'errno' is set
to EDOM, and the value negative HUGE is returned.
Notes: Error handling can be modified by using the matherr()
routine.
-------------------------------- Example ---------------------------------
The following statements calculate the natural logarithm of several
values in an array and report any DOMAIN (negative arguments) or SING
(0.0 argument) errors. A matherr() function is used to distinguish
between the two error types, since the same errno value is returned
for both errors.
#include <math.h> /* for log(), SING, DOMAIN and struct exception */
#include <stdio.h> /* for printf() */
/* 0.0 and -15.0 cause errors */
double value[5] = {47.3, 1.0e300, 0.0, 0.1e-20, -15.0};
int merrno; /* holds type of last math error */
int matherr(e)
struct exception *e;
{
merrno = e->type;
return (0);
}
main()
{
double nat_log;
int i;
for (i = 0; i < 5; i++) {
merrno = 0;
nat_log = log(value[i]);
/* on an error, an error message is also printed to stderr*/
if (merrno == SING)
printf("SING error in log()\n");
else if (merrno == DOMAIN)
printf("DOMAIN error in log()\n");
printf("natural logarithm of %e = %e\n", value[i], nat_log);
}
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster