My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
real.cpp
Go to the documentation of this file.
1 /*
2  * real.cpp - some real valued function implementations
3  *
4  * Copyright (C) 2008 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id: real.cpp 1825 2011-03-11 20:42:14Z ela $
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <math.h>
30 #include <assert.h>
31 
32 #include "consts.h"
33 #include "real.h"
34 
35 #ifndef HAVE_ROUND
36 nr_double_t round (const nr_double_t arg) {
37  return (arg > 0) ? floor (arg + 0.5) : ceil (arg - 0.5);
38 }
39 #endif /* HAVE_ROUND */
40 
41 #ifndef HAVE_TRUNC
42 nr_double_t trunc (const double arg) {
43  return arg > 0 ? floor (arg) : floor (arg + 1);
44 }
45 #endif /* HAVE_TRUNC */
46 
47 #ifndef HAVE_ACOSH
48 nr_double_t acosh (const double arg) {
49  return log (arg + sqrt (arg * arg - 1.0));
50 }
51 #endif /* HAVE_ACOSH */
52 
53 #ifndef HAVE_ASINH
54 nr_double_t asinh (const double arg) {
55  return log (arg + sqrt (arg * arg + 1.0));
56 }
57 #endif /* HAVE_ASINH */
58 
62 unsigned int
63 factorial (unsigned int n) {
64  unsigned int result = 1;
65 
66  /* 13! > 2^32 */
67  assert (n < 13);
68 
69  if (n == 0)
70  return 1;
71 
72  for (; n > 1; n--)
73  result = result * n;
74 
75  return result;
76 }
77 
84 nr_double_t real (const nr_double_t r) {
85  return r;
86 }
87 
94 nr_double_t imag (const nr_double_t r) {
95  return 0.0;
96 }
97 
105 nr_double_t norm (const nr_double_t r) {
106  return r * r;
107 }
108 
115 nr_double_t abs (const nr_double_t r) {
116  return fabs (r);
117 }
118 
125 nr_double_t conj (const nr_double_t r) {
126  return r;
127 }
128 
146 nr_double_t limexp (const nr_double_t r) {
147  return r < M_LIMEXP ? exp (r) : exp (M_LIMEXP) * (1.0 + (r - M_LIMEXP));
148 }
149 
164 nr_double_t signum (const nr_double_t d) {
165  if (d == 0) return 0;
166  return d < 0 ? -1 : 1;
167 }
168 
182 nr_double_t sign (const nr_double_t d) {
183  return d < 0 ? -1 : 1;
184 }
185 
197 nr_double_t xhypot (const nr_double_t a, const nr_double_t b) {
198  nr_double_t c = fabs (a);
199  nr_double_t d = fabs (b);
200  if (c > d) {
201  nr_double_t e = d / c;
202  return c * sqrt (1 + e * e);
203  }
204  else if (d == 0)
205  return 0;
206  else {
207  nr_double_t e = c / d;
208  return d * sqrt (1 + e * e);
209  }
210 }
211 
219 nr_double_t sinc (const nr_double_t d) {
220  if (d == 0) return 1;
221  return sin (d) / d;
222 }
223 
238 nr_double_t fix (const nr_double_t d) {
239  return (d > 0) ? floor (d) : ceil (d);
240 }
241 
251 nr_double_t step (const nr_double_t d) {
252  nr_double_t x = d;
253  if (x < 0.0)
254  x = 0.0;
255  else if (x > 0.0)
256  x = 1.0;
257  else
258  x = 0.5;
259  return x;
260 }