Borderbasix

Arith.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of BORDERBASIX software. *
3 * (C) B. Mourrain, INRIA *
4 **********************************************************************
5 History:
6 $Id: Arith.hpp,v 1.1.1.1 2006/10/06 08:01:40 trebuche Exp $
7 **********************************************************************/
8 #ifndef _Arith_H_
9 #define _Arith_H_
10 
11 #include <stdlib.h>
12 
17 template <class C> struct CST {
18  const static C Zero;
19  const static C Un;
20 };
21 
22 template <class C> const C CST<C>::Zero=0;
23 template <class C> const C CST<C>::Un=1;
24 
25 template <class C> inline void convert (C &, char *);
26 template <class C> inline bool IsNull (const C);
27 template <class C> inline bool IsDivide (const C, const C);
28 template <class C> inline C Gcd (const C, const C);
29 
30 //----------------------------------------------------------------------
31 // Generic implementation
32 //----------------------------------------------------------------------
34 template <class C> inline bool IsNull(const C c) {
35  return c==CST<C>::Zero;
36 }
37 
38 //----------------------------------------------------------------------
39 // Specialization for int
40 //----------------------------------------------------------------------
41 template <> const int CST<int>::Zero = 0;
42 template <> const int CST<int>::Un = 1;
43 
44 template <>
45 inline void convert(int &i, char *s) {
46  i = atoi(s);
47 }
48 
50 template <> inline bool IsNull(const int i) {
51  return i==0;
52 }
53 
54 template <>
55 inline bool IsDivide (int a, int b) {
56  return b != 0 && a % b == 0;
57 }
58 
59 template <>
60 inline int Gcd (int a, int b) {
61  int r;
62  while ((r = a % b) != 0)
63  {
64  a = b;
65  b = r;
66  }
67  return b;
68 }
69 
70 
71 inline double factorial (int n) {
72  double res=1;
73  for (int i = n; i > 1; --i)
74  res *= i;
75  return res;
76 }
77 
78 //----------------------------------------------------------------------
79 // specification for double
80 //----------------------------------------------------------------------
81 template <> const double CST<double>::Zero = 0.0;
82 template <> const double CST<double>::Un = 1.0;
83 template <>
84 inline void convert(double & d,char *s) {
85  d = atof(s);
86 }
87 template<typename T>
88 inline void convert (T & dd, char *s)
89 {
90  T tmp=0;
91  long ttmp;
92  int i=0;
93  T comp=1;
94  char *reslook,sign;
95  ttmp=atoll(s);
96  tmp+=(T)ttmp;
97  sign=(*s=='-')?-1:1;
98  for(;(tmp/comp)>(T)1;i++) comp*=10;
99  comp=10;
100  //strchr(s,'.');
101  if (strchr(s,'.')!=NULL)
102  {
103  i=strchr(s,'.')-s;
104  i++;
105  //cout<<"s["<<i<<"]"<<s[i]<<endl;
106  for(;;i++)
107  {
108  switch(s[i]) {
109  case '1':{tmp+=sign*1/comp;comp*=10;break;}
110  case '2':{tmp+=sign*2/comp;comp*=10;break;}
111  case '3':{tmp+=sign*3/comp;comp*=10;break;}
112  case '4':{tmp+=sign*4/comp;comp*=10;break;}
113  case '5':{tmp+=sign*5/comp;comp*=10;break;}
114  case '6':{tmp+=sign*6/comp;comp*=10;break;}
115  case '7':{tmp+=sign*7/comp;comp*=10;break;}
116  case '8':{tmp+=sign*8/comp;comp*=10;break;}
117  case '9':{tmp+=sign*9/comp;comp*=10;break;}
118  case '0':
119  {
120  comp*=10;
121  break;
122  }
123  default:goto end;
124  }
125  }
126  }
127  end:dd=tmp;
128  reslook=strchr(s,'e');
129  if(reslook!=NULL)
130  {
131  comp=atoi(reslook+1);
132  for (int k=0;(T)k<comp;k++)
133  dd*=10;
134  for (int k=0;(T)k>comp;k--)
135  dd/=(T)10;
136  }
137  //cout<<"valeur de retour "<<dd<<endl;
138  return;
139 }
141 template <> inline bool IsNull(const double d) {
142  return d ==0;
143 }
144 template <>
145 bool IsDivide (double a, double b) {
146  return b != 0;
147 }
148 
149 template <>
150 double Gcd (double a, double b) {
151  return 1;
152 }
153 
154 #endif // //_Arith_H_
bool IsNull(const C)
Generic equality test to zero.
Definition: Arith.hpp:34
Definition of constants.
Definition: Arith.hpp:17
int sign(const Scl< T > &b)
Definition: BC.hpp:307
MSKint32t k
Definition: mosek.h:2713
void convert(C &, char *)
static const C Zero
Definition: Arith.hpp:18
bool IsDivide(const C, const C)
static const C Un
Definition: Arith.hpp:19
MSKrealt * c
Definition: mosek.h:2678
MSKint32t MSKint32t MSKint32t i
Definition: mosek.h:2278
MSKrescodee r
Definition: mosek.h:2321
MSKstreamtypee MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t a
Definition: mosek.h:3833
C Gcd(const C, const C)
double factorial(int n)
Definition: Arith.hpp:71
Home  |  Download & InstallContributions