質問編集履歴
1
加算以外の減算・乗算・除算への応用
test
CHANGED
File without changes
|
test
CHANGED
@@ -144,6 +144,154 @@
|
|
144
144
|
|
145
145
|
|
146
146
|
|
147
|
+
|
148
|
+
|
149
|
+
##追記
|
150
|
+
|
151
|
+
無事、加算については解決いたしました。ありがとうございました。
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
追加の質問なのですが、減算、乗算については同様に行えたのですが、除算が以下のようになってしまいます。解決方法がございましたら、教えていただけると幸いです。
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
### 発生している問題・エラーメッセージ(追記)
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
2 / one_half
|
166
|
+
|
167
|
+
---------------------------------------------------------------------------
|
168
|
+
|
169
|
+
RuntimeError Traceback (most recent call last)
|
170
|
+
|
171
|
+
<ipython-input-47-5bb4201c0f77> in <module>()
|
172
|
+
|
173
|
+
----> 1 2 / one_half
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
47
|
178
|
+
|
179
|
+
48 def __rdiv__(self,other):
|
180
|
+
|
181
|
+
---> 49 return 1 / (self / other)
|
182
|
+
|
183
|
+
RuntimeError: maximum recursion depth exceeded while calling a Python object
|
184
|
+
|
185
|
+
```
|
186
|
+
|
187
|
+
### 該当のソースコード(追記)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
```python
|
192
|
+
|
193
|
+
class Rat:
|
194
|
+
|
195
|
+
n = 0
|
196
|
+
|
197
|
+
d = 0
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
def __init__(self, n, d=1):
|
202
|
+
|
203
|
+
self.n = n
|
204
|
+
|
205
|
+
self.d = d
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
def __repr__(self):
|
210
|
+
|
211
|
+
return '{}/{}'.format(self.n, self.d)
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def __add__(self,other):
|
216
|
+
|
217
|
+
if isinstance(other,Rat) == False:
|
218
|
+
|
219
|
+
other= Rat(other,1)
|
220
|
+
|
221
|
+
else:
|
222
|
+
|
223
|
+
pass
|
224
|
+
|
225
|
+
return Rat((self.n * other.d) + (other.n * self.d), self.d * other.d)
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
def __radd__(self,other):
|
230
|
+
|
231
|
+
return self + other
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
def __sub__(self,other):
|
236
|
+
|
237
|
+
if isinstance(other,Rat) == False:
|
238
|
+
|
239
|
+
other= Rat(other,1)
|
240
|
+
|
241
|
+
else:
|
242
|
+
|
243
|
+
pass
|
244
|
+
|
245
|
+
return Rat((self.n * other.d) - (other.n * self.d), self.d * other.d)
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
def __rsub__(self,other):
|
250
|
+
|
251
|
+
return (self - other) * (-1)
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
def __mul__(self,other):
|
256
|
+
|
257
|
+
if isinstance(other,Rat) == False:
|
258
|
+
|
259
|
+
other= Rat(other,1)
|
260
|
+
|
261
|
+
else:
|
262
|
+
|
263
|
+
pass
|
264
|
+
|
265
|
+
return Rat(self.n * other.n, self.d * other.d)
|
266
|
+
|
267
|
+
def __rmul__(self,other):
|
268
|
+
|
269
|
+
return self * other
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
def __div__(self,other):
|
274
|
+
|
275
|
+
if isinstance(other,Rat) == False:
|
276
|
+
|
277
|
+
other= Rat(other,1)
|
278
|
+
|
279
|
+
else:
|
280
|
+
|
281
|
+
pass
|
282
|
+
|
283
|
+
return Rat(self.n * other.d, self.d * other.n)
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
def __rdiv__(self,other):
|
288
|
+
|
289
|
+
a = self / other
|
290
|
+
|
291
|
+
return 1 / a
|
292
|
+
|
293
|
+
```
|
294
|
+
|
147
295
|
### 補足情報(FW/ツールのバージョンなど)
|
148
296
|
|
149
297
|
anaconda-4.0.0
|