Hacker News new | past | comments | ask | show | jobs | submit login
“Fixing” the rules of division (alexwlchan.net)
13 points by memorable on Nov 11, 2022 | hide | past | favorite | 10 comments



Ahh, so Ruby doesn't - and can't[1] - support constant folding.

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.


> Ahh, so Ruby doesn't - and can't[1] - support constant folding.

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.


Huh? Python is an everything-is-an-object language too. Supporting monkey-patching is a separate thing.


I didn't realize this. I thought things like int's were special. I just tried

  x = 5
  dir(x)
Output:

  ['__abs__', '__add__', '__and__', '__bool__', ...


Some Python objects are open for modification, and some are closed. Closed objects don't support this sort of monkey-patching.

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.


> Long division makes my head hurt!

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.


> 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).


same this is how i learned it in Canada public school


you can't extend the numerator to get more places like this unless you do it upfront

5772.00000000 | 37

also polynomial division works better the other way too, i think.


Sure you can:

    5772 / 36 = 160,33...
    36   
    ---    
    217
    216
    ----
      120
      108
      ----
       120
       108
       ----
        ...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: