File: number.h

package info (click to toggle)
python-scipy 0.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,464 kB
  • ctags: 79,406
  • sloc: python: 143,495; cpp: 89,357; fortran: 81,650; ansic: 79,778; makefile: 364; sh: 265
file content (187 lines) | stat: -rw-r--r-- 5,147 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/******************************************** 
  copyright 1999 McMillan Enterprises, Inc.
  www.mcmillan-inc.com

  modified for weave by eric jones
*********************************************/
#if !defined(NUMBER_H_INCLUDED_)
#define NUMBER_H_INCLUDED_

#include "object.h"
#include "sequence.h"

namespace py {
    
class number : public object
{
public:
  number() : object() {};
  number(int i) : object(i) {};
  number(long i) : object(i) {};
  number(unsigned long i) : object(i) {};
  number(double d) : object(d) {}
  number(std::complex<double>& d) : object(d) {};

  number(const number& other) : object(other) {};
  number(PyObject* obj) : object(obj) {
    _violentTypeCheck();
  };
  virtual ~number() {};

  virtual number& operator=(const number& other) {
    grab_ref(other);
    return *this;
  };
  /*virtual*/ number& operator=(const object& other) {
    grab_ref(other);
    _violentTypeCheck();
    return *this;
  };
  virtual void _violentTypeCheck() {
    if (!PyNumber_Check(_obj)) {
      grab_ref(0);
      fail(PyExc_TypeError, "Not a number");
    }
  };
  //PyNumber_Absolute
  number abs() const {
    PyObject* rslt = PyNumber_Absolute(_obj);
    if (rslt==0)
      fail(PyExc_TypeError, "failed to get absolute value");
    return lose_ref(rslt);
  };
  //PyNumber_Add
  number operator+(const number& rhs) const {
    PyObject*  rslt = PyNumber_Add(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for +");
    return lose_ref(rslt);
  };
  //PyNumber_And
  number operator&(const number& rhs) const {
    PyObject*  rslt = PyNumber_And(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for &");
    return lose_ref(rslt);
  };
  //PyNumber_Coerce
  //PyNumber_Divide
  number operator/(const number& rhs) const {
    PyObject*  rslt = PyNumber_Divide(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for /");
    return lose_ref(rslt);
  };
  //PyNumber_Divmod
  sequence divmod(const number& rhs) const {
    PyObject*  rslt = PyNumber_Divmod(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for divmod");
    return lose_ref(rslt);
  };
  //PyNumber_Float
    operator double () const {
  PyObject*  F = PyNumber_Float(_obj);
  if (F==0)
      fail(PyExc_TypeError, "Cannot convert to double");
  double r = PyFloat_AS_DOUBLE(F);
  Py_DECREF(F);
  return r;
    };
  operator float () const {
    double rslt = (double) *this;
    //if (rslt > INT_MAX)
    //  fail(PyExc_TypeError, "Cannot convert to a float");
    return (float) rslt;
  };
  //PyNumber_Int
    operator long () const {
  PyObject*  Int = PyNumber_Int(_obj);
  if (Int==0)
      fail(PyExc_TypeError, "Cannot convert to long");
  long r = PyInt_AS_LONG(Int);
  Py_DECREF(Int);
  return r;
    };
  operator int () const {
    long rslt = (long) *this;
    if (rslt > INT_MAX)
      fail(PyExc_TypeError, "Cannot convert to an int");
    return (int) rslt;
  };
  //PyNumber_Invert
  number operator~ () const {
    PyObject* rslt = PyNumber_Invert(_obj);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper type for ~");
    return lose_ref(rslt);
  };
  //PyNumber_Long
  //PyNumber_Lshift
  number operator<<(const number& rhs) const {
    PyObject*  rslt = PyNumber_Lshift(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for <<");
    return lose_ref(rslt);
  };
  //PyNumber_Multiply
  number operator*(const number& rhs) const {
    PyObject*  rslt = PyNumber_Multiply(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for *");
    return lose_ref(rslt);
  };
  //PyNumber_Negative
  number operator- () const {
    PyObject* rslt = PyNumber_Negative(_obj);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper type for unary -");
    return lose_ref(rslt);
  };
  //PyNumber_Or
  number operator|(const number& rhs) const {
    PyObject*  rslt = PyNumber_Or(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for |");
    return lose_ref(rslt);
  };
  //PyNumber_Positive
  number operator+ () const {
    PyObject* rslt = PyNumber_Positive(_obj);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper type for unary +");
    return lose_ref(rslt);
  };
  //PyNumber_Remainder
  number operator%(const number& rhs) const {
    PyObject*  rslt = PyNumber_Remainder(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for %");
    return lose_ref(rslt);
  };
  //PyNumber_Rshift
  number operator>>(const number& rhs) const {
    PyObject*  rslt = PyNumber_Rshift(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for >>");
    return lose_ref(rslt);
  };
  //PyNumber_Subtract
  number operator-(const number& rhs) const {
    PyObject*  rslt = PyNumber_Subtract(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for -");
    return lose_ref(rslt);
  };
  //PyNumber_Xor
  number operator^(const number& rhs) const {
    PyObject*  rslt = PyNumber_Xor(_obj, rhs);
    if (rslt==0)
      fail(PyExc_TypeError, "Improper rhs for ^");
    return lose_ref(rslt);
  };
};

} // namespace py

#endif //NUMBER_H_INCLUDED_