“Fixing” the rules of division
alexwlchan.net
alexwlchan.net
In CPython, because monkey-patching like this isn't supported, the byte compiler turns a "0/2" into a "0.0", as you can see with:
>>> def f():
... return 0 / 2
...
>>> import dis
>>> dis.dis(f)
2 0 LOAD_CONST 1 (0.0)
2 RETURN_VALUE
[1] EDIT: except through something like whole-program analysis.it is because most "OOP" languages are not really "object" languages. Ruby is a "proper" everthing is an object language, which is why everyone loves it so much even if they do not think about it.
x = 5
dir(x)
Output: ['__abs__', '__add__', '__and__', '__bool__', ...Integers, strings, and other class objects defined in C are closed for modification.
>>> x = 5
>>> x.__add__ = lambda x, y: x*y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object attribute '__add__' is read-only
In that sense they are "special" (or class objects defined in Python are special).But that doesn't make a language with closed classes non-OO.
Well, I suspect it has something to do with writing out the process of 5772 / 37 as
156
______
37 | 5772
37
---
207
185
----
222
222
---
0
because it a) reverses the order of dividend and divisor for no good reason; b) the result goes up instead of to the right or down. Why not write it like this 5772 | 37
37 +----
--- 156
207
185
----
222
222
---
0
instead? In fact, I think we could replace the corner with just normal slash, "/", write = to the right of the divisor and spell out the digits of the result to the right of it; the substraction chain will still go down the page.That's exactly the way I was taught long division (in Germany).
5772.00000000 | 37
also polynomial division works better the other way too, i think.
5772 / 36 = 160,33...
36
---
217
216
----
120
108
----
120
108
----
...