1 | |
---|
2 | /* @(#)w_log.c 5.1 93/09/24 */ |
---|
3 | /* |
---|
4 | * ==================================================== |
---|
5 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
---|
6 | * |
---|
7 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
---|
8 | * Permission to use, copy, modify, and distribute this |
---|
9 | * software is freely granted, provided that this notice |
---|
10 | * is preserved. |
---|
11 | * ==================================================== |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | FUNCTION |
---|
16 | <<log>>, <<logf>>---natural logarithms |
---|
17 | |
---|
18 | INDEX |
---|
19 | log |
---|
20 | INDEX |
---|
21 | logf |
---|
22 | |
---|
23 | SYNOPSIS |
---|
24 | #include <math.h> |
---|
25 | double log(double <[x]>); |
---|
26 | float logf(float <[x]>); |
---|
27 | |
---|
28 | DESCRIPTION |
---|
29 | Return the natural logarithm of <[x]>, that is, its logarithm base e |
---|
30 | (where e is the base of the natural system of logarithms, 2.71828@dots{}). |
---|
31 | <<log>> and <<logf>> are identical save for the return and argument types. |
---|
32 | |
---|
33 | You can use the (non-ANSI) function <<matherr>> to specify error |
---|
34 | handling for these functions. |
---|
35 | |
---|
36 | RETURNS |
---|
37 | Normally, returns the calculated value. When <[x]> is zero, the |
---|
38 | returned value is <<-HUGE_VAL>> and <<errno>> is set to <<ERANGE>>. |
---|
39 | When <[x]> is negative, the returned value is NaN (not a number) and |
---|
40 | <<errno>> is set to <<EDOM>>. You can control the error behavior via |
---|
41 | <<matherr>>. |
---|
42 | |
---|
43 | PORTABILITY |
---|
44 | <<log>> is ANSI. <<logf>> is an extension. |
---|
45 | */ |
---|
46 | |
---|
47 | /* |
---|
48 | * wrapper log(x) |
---|
49 | */ |
---|
50 | |
---|
51 | #include "fdlibm.h" |
---|
52 | #include <errno.h> |
---|
53 | |
---|
54 | #ifndef _DOUBLE_IS_32BITS |
---|
55 | |
---|
56 | #ifdef __STDC__ |
---|
57 | double log(double x) /* wrapper log */ |
---|
58 | #else |
---|
59 | double log(x) /* wrapper log */ |
---|
60 | double x; |
---|
61 | #endif |
---|
62 | { |
---|
63 | #ifdef _IEEE_LIBM |
---|
64 | return __ieee754_log(x); |
---|
65 | #else |
---|
66 | double z; |
---|
67 | struct exception exc; |
---|
68 | z = __ieee754_log(x); |
---|
69 | if(_LIB_VERSION == _IEEE_ || isnan(x) || x > 0.0) return z; |
---|
70 | #ifndef HUGE_VAL |
---|
71 | #define HUGE_VAL inf |
---|
72 | double inf = 0.0; |
---|
73 | |
---|
74 | SET_HIGH_WORD(inf,0x7ff00000); /* set inf to infinite */ |
---|
75 | #endif |
---|
76 | exc.name = "log"; |
---|
77 | exc.err = 0; |
---|
78 | exc.arg1 = x; |
---|
79 | exc.arg2 = x; |
---|
80 | if (_LIB_VERSION == _SVID_) |
---|
81 | exc.retval = -HUGE; |
---|
82 | else |
---|
83 | exc.retval = -HUGE_VAL; |
---|
84 | if(x==0.0) { |
---|
85 | /* log(0) */ |
---|
86 | exc.type = SING; |
---|
87 | if (_LIB_VERSION == _POSIX_) |
---|
88 | errno = ERANGE; |
---|
89 | else if (!matherr(&exc)) { |
---|
90 | errno = ERANGE; |
---|
91 | } |
---|
92 | } else { |
---|
93 | /* log(x<0) */ |
---|
94 | exc.type = DOMAIN; |
---|
95 | if (_LIB_VERSION == _POSIX_) |
---|
96 | errno = EDOM; |
---|
97 | else if (!matherr(&exc)) { |
---|
98 | errno = EDOM; |
---|
99 | } |
---|
100 | exc.retval = nan(""); |
---|
101 | } |
---|
102 | if (exc.err != 0) |
---|
103 | errno = exc.err; |
---|
104 | return exc.retval; |
---|
105 | #endif |
---|
106 | } |
---|
107 | |
---|
108 | #endif /* defined(_DOUBLE_IS_32BITS) */ |
---|