NEURON
math.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 /* a fake change */
3 /* /local/src/master/nrn/src/oc/math.cpp,v 1.6 1999/07/16 13:43:10 hines Exp */
4 
5 #include <math.h>
6 #include <errno.h>
7 #include <stdio.h>
8 #include "nrnmpiuse.h"
9 #include "ocfunc.h"
10 # include "hoc.h"
11 
12 
13 #define EPS hoc_epsilon
14 #define MAXERRCOUNT 5
15 
17 
18 #if _CRAY
19 #define log logl
20 #define log10 log10l
21 #define exp expl
22 #define sqrt sqrtl
23 #define pow powl
24 #endif
25 
26 static double errcheck(double, const char *);
27 
28 void hoc_atan2(void) {
29  double d;
30  d = atan2(*hoc_getarg(1), *hoc_getarg(2));
31  hoc_ret();
32  hoc_pushx(d);
33 }
34 
35 double Log(double x) {
36  return errcheck(log(x), "log");
37 }
38 
39 double Log10(double x) {
40  return errcheck(log10(x), "log10");
41 }
42 
43 /* used by nmodl and other c, c++ code */
44 extern "C" double hoc_Exp(double x) {
45  if (x < -700.) {
46  return 0.;
47  } else if (x > 700 && nrn_feenableexcept_ == 0) {
48  errno = ERANGE;
49  if (++hoc_errno_count < MAXERRCOUNT) {
50  fprintf(stderr, "exp(%g) out of range, returning exp(700)\n", x);
51  }
53  fprintf(stderr, "No more errno warnings during this execution\n");
54  }
55  return exp(700.);
56  }
57  return exp(x);
58 }
59 
60 /* used by interpreter */
61 double hoc1_Exp(double x) {
62  if (x < -700.) {
63  return 0.;
64  } else if (x > 700) {
65  errno = ERANGE;
66  return errcheck(exp(700.), "exp");
67  }
68  return errcheck(exp(x), "exp");
69 }
70 
71 double Sqrt(double x) {
72  return errcheck(sqrt(x), "sqrt");
73 }
74 
75 double Pow(double x, double y) {
76  return errcheck(pow(x, y), "exponentiation");
77 }
78 
79 double integer(double x) {
80  if (x < 0) {
81  return (double) (long) (x - EPS);
82  } else {
83  return (double) (long) (x + EPS);
84  }
85 }
86 
87 double errcheck(double d, const char *s) /* check result of library call */
88 {
89  if (errno == EDOM) {
90  errno = 0;
91  hoc_execerror(s, "argument out of domain");
92  } else if (errno == ERANGE) {
93  errno = 0;
94 #if 0
95  hoc_execerror(s, "result out of range");
96 #else
97  if (++hoc_errno_count > MAXERRCOUNT) {
98  } else {
99  hoc_warning(s, "result out of range");
100  if (hoc_errno_count == MAXERRCOUNT) {
101  fprintf(stderr, "No more errno warnings during this execution\n");
102  }
103  }
104 #endif
105  }
106  return d;
107 }
108 
109 int hoc_errno_check(void) {
110  int ierr;
111 #if LINDA
112  static parallel_eagain = 0;
113 #endif
114 
115 #if 1
116  errno = 0;
117  return 0;
118 #else
119  if (errno) {
120  if (errno == EAGAIN) {
121  /* Ubiquitous on many systems and it seems not to matter */
122  errno = 0;
123  return 0;
124  }
125 #if !defined(MAC) || defined(DARWIN)
126  if (errno == ENOENT) {
127  errno = 0;
128  return 0;
129  }
130 #endif
131  if (++hoc_errno_count > MAXERRCOUNT) {
132  errno = 0;
133  return 0;
134  }
135 #if defined(CYGWIN)
136  if (errno == EBUSY) {
137  errno = 0;
138  return 0;
139  }
140 #endif
141  switch (errno) {
142  case EDOM:
143 fprintf(stderr, "A math function was called with argument out of domain\n");
144  break;
145  case ERANGE:
146 fprintf(stderr, "A math function was called that returned an out of range value\n");
147  break;
148 #if LINDA
149 /* regularly set by eval() and perhaps other linda commands */
150  case EAGAIN:
151  if (parallel_eagain++ == 0) {
152  perror("oc");
153 fprintf(stderr, "oc: This error occurs often from LINDA and thus will not be further reported.\n");
154  }
155  break;
156 #endif
157  default:
158  perror("oc");
159  break;
160  }
161  if (hoc_errno_count == MAXERRCOUNT) {
162 fprintf(stderr, "No more errno warnings during this execution\n");
163  }
164  }
165  ierr = errno;
166  errno = 0;
167  return ierr;
168 #endif
169 }
170 
double Log10(double x)
Definition: math.cpp:39
double Pow(double x, double y)
Definition: math.cpp:75
double hoc1_Exp(double x)
Definition: math.cpp:61
double Log(double x)
Definition: math.cpp:35
log
Definition: extdef.h:3
void hoc_atan2(void)
Definition: math.cpp:28
int nrn_feenableexcept_
Definition: hoc.cpp:79
void hoc_ret()
long
Definition: netcvode.cpp:4792
log10
Definition: extdef.h:3
atan2
Definition: extdef.h:3
_CONST char * s
Definition: system.cpp:74
void hoc_warning(const char *, const char *)
int hoc_errno_check(void)
Definition: math.cpp:109
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:741
errno
Definition: system.cpp:98
pow
Definition: extdef.h:3
fprintf(stderr, "Don't know the location of params at %p\, pp)
exp
Definition: extdef.h:3
#define MAXERRCOUNT
Definition: math.cpp:14
#define EPS
Definition: math.cpp:13
double Sqrt(double x)
Definition: math.cpp:71
void hoc_pushx(double)
double integer(double x)
Definition: math.cpp:79
sqrt
Definition: extdef.h:3
static double errcheck(double, const char *)
Definition: math.cpp:87
int hoc_errno_count
Definition: math.cpp:16
double hoc_Exp(double x)
Definition: math.cpp:44