Debian Codegraph @

contrib/ms-sbvar/utilities_dw/math/dw_math.c

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "dw_math.h"
19 #include "dw_std.h"
20 #include <math.h>
21 #include <ctype.h>
22
23
24 /*
25 Returns ln(exp(a) + exp(b)) computed to avoid overflow. If
26 a = ln(c) and b = ln(d), as is usually the case, then the
27 routine returns ln(c + d).
28
29 */
30 PRECISION AddLogs(PRECISION a, PRECISION b)
31 {
32 return (a > b) ? a + log(1.0 + exp(b-a)) : b + log(exp(a-b) + 1.0);
33 }
34
35 /*
36 Returns ln(x*exp(a) + y*exp(b)) computed to avoid overflow. If a = ln(c) and
37 b = ln(d), as is usually the case, then the routine returns ln(x*c + y*d).
38 Both x and y must be non-negative. If either x or y is negative, it is
39 treated as if it were zero.
40 */
41 PRECISION AddScaledLogs(PRECISION x, PRECISION a, PRECISION y, PRECISION b)
42 {
43 if (x > 0)
44 if (y > 0)
45 return (a > b) ? a + log(x + y*exp(b-a)) : b + log(x*exp(a-b) + y);
46 else
47 return log(x) + a;
48 else
49 return (y > 0) ? log(y) + b : MINUS_INFINITY;
50 }
51
52 /*
53 A floating point number is of the form
54
55 [white space][+/-]digits[.[digits]][E/e[+/-]digits]white space/null character
56
57 or
58
59 [white space][+/-].digits[E/e[+/-]digits]white space/null character
60
61 where characters in square brackets are optional.
62
63 Returns one if valid floating point number and zero otherwise.
64 */
65 int dw_IsFloat(const char *buffer)
66 {
67 int i=0;
68
69 if (!buffer) return 0;
70
71 /* Strip leading white space */
72 while (isspace(buffer[i])) i++;
73
74 /* Mantissa OK? */
75 if ((buffer[i] == '+') || (buffer[i] == '-')) i++;
76 if (isdigit(buffer[i]))
77 {
78 while (isdigit(buffer[++i]));
79 if ((buffer[i] == '.'))
80 while (isdigit(buffer[++i]));
81 }
82 else
83 if ((buffer[i] == '.'))
84 if (isdigit(buffer[++i]))
85 while (isdigit(buffer[++i]));
86 else
87 return 0;
88 else
89 return 0;
90
91 /* Is exponent OK? */
92 if ((buffer[i] == 'e') || (buffer[i] == 'E'))
93 {
94 if ((buffer[++i] == '+') || (buffer[i] == '-')) i++;
95 if (isdigit(buffer[i]))
96 while (isdigit(buffer[++i]));
97 else
98 return 0;
99 }
100
101 /* Is end of string or trailing white space */
102 if (buffer[i] && !isspace(buffer[i])) return 0;
103
104 return 1;
105 }
106
107 /*
108 Integers are of the form
109
110 [white space][+/-]digits[.]white space/null character
111
112 where characters in square brackets are optional.
113
114 Returns one if valid integer and zero otherwise.
115 */
116 int dw_IsInteger(const char *buffer)
117 {
118 int i=0;
119
120 if (!buffer) return 0;
121
122 /* Strip leading white space */
123 while (isspace(buffer[i])) i++;
124
125 /* Leading sign */
126 if ((buffer[i] == '+') || (buffer[i] == '-')) i++;
127
128 /* At least one digits possibly followed by decimal point */
129 if (isdigit(buffer[i]))
130 {
131 while (isdigit(buffer[++i]));
132 if ((buffer[i] == '.')) i++;
133 }
134 else
135 return 0;
136
137 /* Is end of string or trailing white space */
138 if (buffer[i] && !isspace(buffer[i])) return 0;
139
140 return 1;
141 }