janet-lang / janet-lang.org

Website for janet
https://janet-lang.org
MIT License
90 stars 59 forks source link

Bytecode reference table lacks entry for divf #194

Closed sogaiu closed 11 months ago

sogaiu commented 1 year ago

At the time of this writing, the reference table for bytecodes lacks a row for the new bytecode instruction, divf, added in https://github.com/janet-lang/janet/commit/c83f3ec09757eb48bf7d48f3063b39e4d8bd9345.

Perhaps inserting an appropriate row between these two rows:

   ['div '(div dest lhs rhs) "$dest = $lhs / $rhs"]
   ['divim '(divim dest lhs im) "$dest = $lhs / im"]

is sufficient to address this issue.

I wonder what to use to indicate the operator in the rightmost column:

 ['divf '(divf dest lhs rhs) "$dest = $lhs ??? $rhs"]

One candidate for ??? might be div because:

$ janet
Janet 1.29.1-58d29736 linux/x64/gcc - '(doc)' for help
repl:1:> (disasm (fn [] (div 2 3)) :bytecode)
@[(ldi 1 2) (ldi 2 3) (divf 0 1 2) (ret 0)]

However, this might be confusing because the row above it would presumably be this:

['div '(div dest lhs rhs) "$dest = $lhs / $rhs"]

That is, the instruction's name in that row is div.

May be using // makes sense?

sogaiu commented 1 year ago

In support of using //, in Python, it appears that:

$ python
Python 2.7.18 (default, Jul  1 2022, 10:30:50) 
[GCC 11.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> -7 // 5
-2

..and with a recent janet (see earlier comment for info about bytecode being divf), we have:

$ janet
Janet 1.29.1-db0abfde linux/x64/gcc - '(doc)' for help
repl:1:> (div -7 5)
-2

On a terminological note, there appears to be a lack of consensus about what "integer division" refers to:

Names and symbols used for integer division include div, /, \, and %. Definitions vary regarding integer division when the dividend or the divisor is negative: rounding may be toward zero (so called T-division) or toward −∞ (F-division); rarer styles can occur – see modulo operation for the details.

via: https://en.wikipedia.org/wiki/Division_(mathematics)#Of_integers

Don't Confuse Integer Division with Floor Division

via: https://marcelkliemannel.com/articles/2021/dont-confuse-integer-division-with-floor-division/