• Mathematical correct division

    From none) (albert@21:1/5 to All on Fri Aug 19 13:06:48 2022
    [I can't find the post because the subjects are not always maintained, therefore this separate post.]

    Anton Erl corrected my post of how modern language use the
    correct (from a math viewpoint) division.
    Apparently not true for Python.

    At least the language go has it right:
    https://go.dev/ref/spec#Operators

    No ambiguous condition or used defined stuff.
    go even specifies what
    0x8000_0000_0000_0000 / -1
    should yield. Note that it can't be a 64-bit positive number.

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to albert@cherry. on Fri Aug 19 11:52:11 2022
    albert@cherry.(none) (albert) writes:
    At least the language go has it right:
    https://go.dev/ref/spec#Operators

    No ambiguous condition or used defined stuff.
    go even specifies what
    0x8000_0000_0000_0000 / -1
    should yield.

    It specifies that

    |if the dividend x is the most negative value for the int type of x,
    |the quotient q = x / -1 is equal to x (and r = 0)

    That's interesting, because it means that go has to do extra work on
    at least IA-32/AMD64 and Power CPUs to produce this result. If you do
    the extra work, why not produce a "run-time panic" like for division
    by zero?

    gforth (the debugging engine) produces an exception in that case,
    while gforth-fast does what the hardware does:

    Recent gforth everywhere:
    $8000000000000000 -1 / .
    *the terminal*:1:22: error: Result out of range
    $8000000000000000 -1 >>>/<<< .

    gforth-fast:

    AMD64:
    $8000000000000000 -1 / .
    *the terminal*:1:22: error: Division by zero
    $8000000000000000 -1 >>>/<<< .

    Aarch64:
    $8000000000000000 -1 / cr hex.
    $8000000000000000 ok

    RISC-V:
    $8000000000000000 -1 / cr hex.
    $8000000000000000 ok

    Power:
    $8000000000000000 -1 / cr hex.
    $0 ok

    Note that the exception you see on AMD64 is a different one in
    gforth-fast, because that exception is caused by a hardware exception
    (which generates the same exception for divide-by-zero and division
    overflow).

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to Anton Ertl on Fri Aug 19 16:45:24 2022
    In article <2022Aug19.135211@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
    albert@cherry.(none) (albert) writes:
    At least the language go has it right:
    https://go.dev/ref/spec#Operators

    No ambiguous condition or used defined stuff.
    go even specifies what
    0x8000_0000_0000_0000 / -1
    should yield.

    It specifies that

    |if the dividend x is the most negative value for the int type of x,
    |the quotient q = x / -1 is equal to x (and r = 0)

    That's interesting, because it means that go has to do extra work on
    at least IA-32/AMD64 and Power CPUs to produce this result. If you do
    the extra work, why not produce a "run-time panic" like for division
    by zero?

    The people at google defined a language. I can't imagine that
    the backward design of I86 of AMD machines should have influence on that.
    Then you're back at "user defined features" for a slim speed advantage.

    I made the remark to draw attention to how meticulously go is defined.

    Maybe we should add a line on the Forth standard:
    MIN-INT -1 /
    leads to an ambiguous condition.

    gforth (the debugging engine) produces an exception in that case,
    while gforth-fast does what the hardware does:

    Recent gforth everywhere:
    $8000000000000000 -1 / .
    *the terminal*:1:22: error: Result out of range
    $8000000000000000 -1 >>>/<<< .

    gforth-fast:

    AMD64:
    $8000000000000000 -1 / .
    *the terminal*:1:22: error: Division by zero
    $8000000000000000 -1 >>>/<<< .

    Aarch64:
    $8000000000000000 -1 / cr hex.
    $8000000000000000 ok

    RISC-V:
    $8000000000000000 -1 / cr hex.
    $8000000000000000 ok

    Power:
    $8000000000000000 -1 / cr hex.
    $0 ok

    Note that the exception you see on AMD64 is a different one in
    gforth-fast, because that exception is caused by a hardware exception
    (which generates the same exception for divide-by-zero and division >overflow).

    This is probably the situation, the designers of go wanted to
    prevent.


    - anton

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to albert@cherry. on Fri Aug 19 15:45:24 2022
    albert@cherry.(none) (albert) writes:
    In article <2022Aug19.135211@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
    albert@cherry.(none) (albert) writes:
    At least the language go has it right:
    https://go.dev/ref/spec#Operators

    No ambiguous condition or used defined stuff.
    go even specifies what
    0x8000_0000_0000_0000 / -1
    should yield.

    It specifies that

    |if the dividend x is the most negative value for the int type of x,
    |the quotient q = x / -1 is equal to x (and r = 0)

    That's interesting, because it means that go has to do extra work on
    at least IA-32/AMD64 and Power CPUs to produce this result. If you do
    the extra work, why not produce a "run-time panic" like for division
    by zero?

    The people at google defined a language. I can't imagine that
    the backward design of I86 of AMD machines should have influence on that.

    In this particular case, the AMD64 does not seem backwards to me at
    all.

    Then you're back at "user defined features" for a slim speed advantage.

    The speed advantage is not my point. My point is that if Go panics
    for division by zero, it should also panic on division overflow. Or
    if it delivers a defined result on division overflow, why not deliver
    a defined result on division by zero. Aarch64 and Power deliver 0 on division-by-zero, RISC-V delivers -1.

    I made the remark to draw attention to how meticulously go is defined.

    You could also draw attention to gforth (the debugging engine), which
    also covers this case in a machine-independent way (it throws in that
    case).

    Maybe we should add a line on the Forth standard:
    MIN-INT -1 /
    leads to an ambiguous condition.

    Looking at 3.2.2.1 and the various division words, this case is indeed
    not covered for single-by-single division words. For FM/MOD, SM/REM,
    UM/MOD, */, */MOD and M*/, result-out-of-range is an ambiguous
    condition.

    Note that the exception you see on AMD64 is a different one in
    gforth-fast, because that exception is caused by a hardware exception >>(which generates the same exception for divide-by-zero and division >>overflow).

    This is probably the situation, the designers of go wanted to
    prevent.

    Looking at the definition of "run-time panic", no, they did not:

    |The exact error values that represent distinct run-time error
    |conditions are unspecified.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Anton Ertl on Sat Aug 20 13:19:42 2022
    On 19/08/2022 21:52, Anton Ertl wrote:
    albert@cherry.(none) (albert) writes:
    At least the language go has it right:
    https://go.dev/ref/spec#Operators

    No ambiguous condition or used defined stuff.
    go even specifies what
    0x8000_0000_0000_0000 / -1
    should yield.

    It specifies that

    |if the dividend x is the most negative value for the int type of x,
    |the quotient q = x / -1 is equal to x (and r = 0)

    That's interesting, because it means that go has to do extra work on
    at least IA-32/AMD64 and Power CPUs to produce this result. If you do
    the extra work, why not produce a "run-time panic" like for division
    by zero?

    DX-Forth 4.51 2022-07-27

    -32768 -1 /mod ok 0 -32768 <

    OTOH I never (knowingly) planned for that. It's the consequence of my
    wanting to avoid hardware overflow and IDIV. Perhaps GO had similar
    notions and - like me - prepared to pay the price.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to dxforth on Sat Aug 20 10:16:41 2022
    dxforth <dxforth@gmail.com> writes:
    -32768 -1 /mod ok 0 -32768 <

    OTOH I never (knowingly) planned for that. It's the consequence of my >wanting to avoid hardware overflow and IDIV.

    So you do not use IDIV? What do you use?

    Perhaps GO had similar
    notions and - like me - prepared to pay the price.

    Go is specified to panic on division-by-0, so the notion seems not
    very similar.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Anton Ertl on Sat Aug 20 21:30:07 2022
    On 20/08/2022 20:16, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    -32768 -1 /mod ok 0 -32768 <

    OTOH I never (knowingly) planned for that. It's the consequence of my
    wanting to avoid hardware overflow and IDIV.

    So you do not use IDIV? What do you use?

    DIV (with checks for overflow). I read early implementations of Intel's
    IDIV had issues so I avoided it altogether.


    Perhaps GO had similar
    notions and - like me - prepared to pay the price.

    Go is specified to panic on division-by-0, so the notion seems not
    very similar.

    Can't speak as for why GO was happy to provide a numerical result for
    one ambiguous condition and not another.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to albert@cherry. on Sat Aug 20 09:07:48 2022
    albert@cherry.(none) (albert) writes:
    Anton Erl corrected my post of how modern language use the
    correct (from a math viewpoint) division.
    Apparently not true for Python.

    What do you mean about Python? Python uses arbitrary-precision
    integers and floored division. In Python 3, the integer division
    operation is // rather than / which might cause some confusion.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to no.email@nospam.invalid on Sat Aug 20 21:27:01 2022
    In article <87ilmmyk2z.fsf@nightsong.com>,
    Paul Rubin <no.email@nospam.invalid> wrote:
    albert@cherry.(none) (albert) writes:
    Anton Erl corrected my post of how modern language use the
    correct (from a math viewpoint) division.
    Apparently not true for Python.

    What do you mean about Python? Python uses arbitrary-precision
    integers and floored division. In Python 3, the integer division
    operation is // rather than / which might cause some confusion.

    What I meant was that the equivalence classes modulo need to be
    presented by a positive number.
    13%-3 needs to be 1, which is correct in golang but not in Python.
    At least not in version 2.7.
    Has that changed in version 3?

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to albert@cherry. on Sat Aug 20 20:01:35 2022
    albert@cherry.(none) (albert) writes:
    What I meant was that the equivalence classes modulo need to be
    presented by a positive number.
    13%-3 needs to be 1, which is correct in golang but not in Python.
    At least not in version 2.7.
    Has that changed in version 3?

    Python 3.9.2:
    >>> divmod(13,-3)
    (-5, -2)

    What do you want 13 // -3 to be? -5 is what you get from floored
    division, in which case the remainder has to be -2.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Paul Rubin on Sun Aug 21 17:16:20 2022
    On 21/08/2022 13:01, Paul Rubin wrote:
    albert@cherry.(none) (albert) writes:
    What I meant was that the equivalence classes modulo need to be
    presented by a positive number.
    13%-3 needs to be 1, which is correct in golang but not in Python.
    At least not in version 2.7.
    Has that changed in version 3?

    Python 3.9.2:
    >>> divmod(13,-3)
    (-5, -2)

    What do you want 13 // -3 to be? -5 is what you get from floored
    division, in which case the remainder has to be -2.

    Most users want to divide positive by positive and the result is the
    same in most languages. Less common (and the less users care) is
    when either input is negative. It's here where languages have scope
    to differ. Provided users can synthesize the behaviour they need, I
    don't see it matters what is the native division.

    \ Euclidean division using floored /MOD
    : E/MOD ( n1 n2 -- rem quot )
    dup >r /mod over 0< if
    1+ swap r@ - swap
    then r> drop ;

    34 5 e/mod . . ( 6 4 )
    34 -5 e/mod . . ( -6 4 )
    -34 5 e/mod . . ( -7 1 )
    -34 -5 e/mod . . ( 7 1 )

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to no.email@nospam.invalid on Sun Aug 21 12:25:11 2022
    In article <87edxaxptc.fsf@nightsong.com>,
    Paul Rubin <no.email@nospam.invalid> wrote:
    albert@cherry.(none) (albert) writes:
    What I meant was that the equivalence classes modulo need to be
    presented by a positive number.
    13%-3 needs to be 1, which is correct in golang but not in Python.
    At least not in version 2.7.
    Has that changed in version 3?

    Python 3.9.2:
    divmod(13,-3)
    (-5, -2)

    What do you want 13 // -3 to be? -5 is what you get from floored
    division, in which case the remainder has to be -2.

    It should be (-4, 1) .
    That is the result golang has.
    Read the quote about division and modulo from Anton Ertl.

    And that is the result lina 5.4.1 has
    80386 ciforth 5.4.1
    13 -3 /MOD . .
    -4 1 OK

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to dxforth on Sun Aug 21 14:07:21 2022
    dxforth <dxforth@gmail.com> writes:
    Most users want to divide positive by positive and the result is the
    same in most languages. Less common (and the less users care) is
    when either input is negative.

    Negative divisor is particularly rare. Didn't Forth, Inc. use to have signed-by-unsigned division words?

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Anton Ertl on Mon Aug 22 01:45:06 2022
    On 22/08/2022 00:07, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Most users want to divide positive by positive and the result is the
    same in most languages. Less common (and the less users care) is
    when either input is negative.

    Negative divisor is particularly rare. Didn't Forth, Inc. use to have signed-by-unsigned division words?

    All polyForth MOD words were unsigned. You may be thinking of this
    algorithm for floored which appeared in cmForth:

    : F/MOD ( n +n|u -- urem nquot )
    >r s>d dup 0< if r@ + then r> um/mod ;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to dxforth@gmail.com on Sun Aug 21 20:07:01 2022
    In article <tdtju1$125o$1@gioia.aioe.org>, dxforth <dxforth@gmail.com> wrote: >On 22/08/2022 00:07, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Most users want to divide positive by positive and the result is the
    same in most languages. Less common (and the less users care) is
    when either input is negative.

    Negative divisor is particularly rare. Didn't Forth, Inc. use to have
    signed-by-unsigned division words?

    All polyForth MOD words were unsigned. You may be thinking of this
    algorithm for floored which appeared in cmForth:

    : F/MOD ( n +n|u -- urem nquot )


    >r s>d dup 0< if r@ + then r> um/mod ;

    The stack diagram is misleading.
    It is ( n u -- urem nquot )

    Klarokay that is has to work with positive integers, being a subset
    with the same value and the same bitpatterm. as an unsigned integer.

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to no.email@nospam.invalid on Sun Aug 21 21:17:05 2022
    In article <87edxaxptc.fsf@nightsong.com>,
    Paul Rubin <no.email@nospam.invalid> wrote:
    albert@cherry.(none) (albert) writes:
    What I meant was that the equivalence classes modulo need to be
    presented by a positive number.
    13%-3 needs to be 1, which is correct in golang but not in Python.
    At least not in version 2.7.
    Has that changed in version 3?

    Python 3.9.2:
    divmod(13,-3)
    (-5, -2)

    What do you want 13 // -3 to be? -5 is what you get from floored
    division, in which case the remainder has to be -2.

    Mathematically I'd like all mod result positive and smaller than
    the absolute value of the divider.
    Apparently that is no option. So forget what I said in the
    original message, it is nonsense.
    floored and symmetric are the only games in town.

    So python 2..3 and gforth use floored division.
    That makes the most sense mathematically. As long as you are
    considering modulo a fixed positive (or negative) number,
    all your results represent a modulo class, and can be compared.

    golang uses symmetric division as is ciforth.
    So golang is "wrong" and I must change ciforth to floored,
    with the next major release. Guess what, I planned that
    already.
    "
    The mathematical correct MOD : floored is postponed to version 6.
    "

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to albert@cherry. on Sun Aug 21 18:42:27 2022
    albert@cherry.(none) (albert) writes:
    Mathematically I'd like all mod result positive and smaller than
    the absolute value of the divider.

    I guess that has some attractions but mathematically, what does modulo
    by a negative number even mean? When the modulus is a positive number,
    you are mapping the integers to a quotient ring, ok fine. But doing
    that with a negative number makes less sense, unless I'm missing
    something.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Paul Rubin on Mon Aug 22 13:21:28 2022
    On 22/08/2022 11:42, Paul Rubin wrote:
    albert@cherry.(none) (albert) writes:
    Mathematically I'd like all mod result positive and smaller than
    the absolute value of the divider.

    I guess that has some attractions but mathematically, what does modulo
    by a negative number even mean? When the modulus is a positive number,
    you are mapping the integers to a quotient ring, ok fine. But doing
    that with a negative number makes less sense, unless I'm missing
    something.

    A short paper is here:

    https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf

    It contains several references which presumably go into more detail.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to albert on Mon Aug 22 13:49:00 2022
    On 22/08/2022 04:07, albert wrote:
    In article <tdtju1$125o$1@gioia.aioe.org>, dxforth <dxforth@gmail.com> wrote:
    On 22/08/2022 00:07, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Most users want to divide positive by positive and the result is the
    same in most languages. Less common (and the less users care) is
    when either input is negative.

    Negative divisor is particularly rare. Didn't Forth, Inc. use to have
    signed-by-unsigned division words?

    All polyForth MOD words were unsigned. You may be thinking of this
    algorithm for floored which appeared in cmForth:

    : F/MOD ( n +n|u -- urem nquot )


    >r s>d dup 0< if r@ + then r> um/mod ;

    The stack diagram is misleading.
    It is ( n u -- urem nquot )

    Klarokay that is has to work with positive integers, being a subset
    with the same value and the same bitpatterm. as an unsigned integer.

    Here's eForth using the algorithm above and massaging it into FM/MOD:

    : FM/MOD ( d n -- r q ) ( 6.1.1561 ) ( floored )
    DUP 0< DUP >R IF NEGATE >R DNEGATE R> THEN
    >R DUP 0< IF R@ + THEN R> UM/MOD R> IF >R NEGATE R> THEN ;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to albert on Mon Aug 22 13:25:36 2022
    On 22/08/2022 05:17, albert wrote:
    In article <87edxaxptc.fsf@nightsong.com>,
    Paul Rubin <no.email@nospam.invalid> wrote:
    albert@cherry.(none) (albert) writes:
    What I meant was that the equivalence classes modulo need to be
    presented by a positive number.
    13%-3 needs to be 1, which is correct in golang but not in Python.
    At least not in version 2.7.
    Has that changed in version 3?

    Python 3.9.2:
    >>> divmod(13,-3)
    (-5, -2)

    What do you want 13 // -3 to be? -5 is what you get from floored
    division, in which case the remainder has to be -2.

    Mathematically I'd like all mod result positive and smaller than
    the absolute value of the divider.
    Apparently that is no option.

    That's what Euclidean division gives you. On the downside it requires
    more effort to synthesize and applications needing a negative divisor
    are rare as Anton noted.

    floored and symmetric are the only games in town.

    These are the most popular. Symmetric is the classical division.
    For a little more expenditure there is floored. It gives a positive
    mod for a negative dividend. That's useful in the real world and
    the motive for a number of languages (Forth-83 included) opting for
    floored. For their part, users of symmetric will argue they can
    synthesize floored should the need arise. Which is true. Rarely
    does one see languages offering Euclidean division 'out of the box'
    as (presumably) the cost/benefit isn't there.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to dxforth on Mon Aug 22 07:04:55 2022
    dxforth <dxforth@gmail.com> writes:
    Rarely
    does one see languages offering Euclidean division 'out of the box'
    as (presumably) the cost/benefit isn't there.

    The number of occurences in the table in <https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages>
    is:

    97 Truncated (symmetric)
    58 Floored
    12 Euclidean

    A number of languages have several. The Forth standard specified that
    /, mod etc. have to be either floored or symmetric, so if someone
    thinks that Euclidean is worth the extra effort, they should add that
    with separate names, e.g. /e, mode, em/mod (note that development
    Gforth has /f /s modf mods etc.).

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to no.email@nospam.invalid on Mon Aug 22 11:39:53 2022
    In article <87pmgtuk8s.fsf@nightsong.com>,
    Paul Rubin <no.email@nospam.invalid> wrote:
    albert@cherry.(none) (albert) writes:
    Mathematically I'd like all mod result positive and smaller than
    the absolute value of the divider.

    I guess that has some attractions but mathematically, what does modulo
    by a negative number even mean? When the modulus is a positive number,
    you are mapping the integers to a quotient ring, ok fine. But doing
    that with a negative number makes less sense, unless I'm missing
    something.

    the expression x(modulo 3) is in fact the infinite set
    { ... x-2*3 x-1*3 x x+1*3 x+2*3 ... }
    This set is the same for x(modulo -3)
    { ... x-2*(-3) x-1*(-3) x x+1*(-3) x+2*(-3) ... }

    x~2 (modulo 3) means that the infinite sets are the same for
    x and 2. E.g. for x=5.

    So it is nice if the expression
    x 3 MOD
    returns the same number iff the sets are the same.
    That is not the case for symmetric division.

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Anton Ertl on Mon Aug 22 20:57:14 2022
    On 22/08/2022 17:04, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Rarely
    does one see languages offering Euclidean division 'out of the box'
    as (presumably) the cost/benefit isn't there.

    The number of occurences in the table in <https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages>
    is:

    97 Truncated (symmetric)
    58 Floored
    12 Euclidean

    A number of languages have several. The Forth standard specified that
    /, mod etc. have to be either floored or symmetric, so if someone
    thinks that Euclidean is worth the extra effort, they should add that
    with separate names, e.g. /e, mode, em/mod (note that development
    Gforth has /f /s modf mods etc.).

    It suffices the majority of languages to have one. Why not Forth?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ron AARON@21:1/5 to dxforth on Tue Aug 23 09:06:58 2022
    On 22/08/2022 13:57, dxforth wrote:
    On 22/08/2022 17:04, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Rarely
    does one see languages offering Euclidean division 'out of the box'
    as (presumably) the cost/benefit isn't there.

    The number of occurences in the table in
    <https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages>
    is:

    97 Truncated (symmetric)
    58 Floored
    12 Euclidean

    A number of languages have several.  The Forth standard specified that
    /, mod etc. have to be either floored or symmetric, so if someone
    thinks that Euclidean is worth the extra effort, they should add that
    with separate names, e.g. /e, mode, em/mod (note that development
    Gforth has /f /s modf mods etc.).

    It suffices the majority of languages to have one.  Why not Forth?

    I've appreciated this discussion, and ended up reviewing how 8th does
    mod and /mod. It turns out there were discrepancies which I've fixed
    for the next release.

    In particular: there was a long time ago a discussion in 8th requesting
    that 'mod' always give a positive value (because it's often used to
    calculate an array offset or the like). Since it makes as much sense mathematically, I did that. But...

    ... I didn't make sure that the result of /mod obeyed the modulo
    relation, e.g. that "n m mod" producing "r q" on the stack such that
    n = (q*m) + r

    I fixed that; and then realized that the results of /mod were different
    (though all mathematically correct) across the different numeric types
    in 8th. So I've now fixed that.

    And to think I studied a lot of math back in the day...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Ron AARON on Wed Aug 24 15:40:07 2022
    On 23/08/2022 16:06, Ron AARON wrote:
    On 22/08/2022 13:57, dxforth wrote:
    On 22/08/2022 17:04, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Rarely
    does one see languages offering Euclidean division 'out of the box'
    as (presumably) the cost/benefit isn't there.

    The number of occurences in the table in
    <https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages> >>> is:

    97 Truncated (symmetric)
    58 Floored
    12 Euclidean

    A number of languages have several.  The Forth standard specified that
    /, mod etc. have to be either floored or symmetric, so if someone
    thinks that Euclidean is worth the extra effort, they should add that
    with separate names, e.g. /e, mode, em/mod (note that development
    Gforth has /f /s modf mods etc.).

    It suffices the majority of languages to have one.  Why not Forth?

    I've appreciated this discussion, and ended up reviewing how 8th does mod and /mod.  It turns out there were discrepancies which I've fixed for the next release.

    In particular: there was a long time ago a discussion in 8th requesting that 'mod' always give a positive value (because it's often used to calculate an array offset or the like). Since it makes as much sense mathematically, I did that. But...

    Not to dampen the enthusiasm of your users, here's an expert's somewhat
    more nuanced opinion:

    "As a mathematician I was asked about the arithmetic words. I did write
    some comments at the request of a Team member to assist his presentation
    -- and I also told the Team that, from a mathematicians view, it is more
    important that arithmetic operations act in a specific and uniform way
    across all systems than in the precise nature of action." - John Wavrik

    ... I didn't make sure that the result of /mod obeyed the modulo relation, e.g. that "n m mod" producing "r q" on the stack such that
     n = (q*m) + r

    I fixed that; and then realized that the results of /mod were different (though all mathematically correct) across the different numeric types in 8th. So I've now fixed that.

    And to think I studied a lot of math back in the day...

    It appears 8th could do with a 'Hayes tester' :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ron AARON@21:1/5 to dxforth on Wed Aug 24 08:44:15 2022
    On 24/08/2022 8:40, dxforth wrote:
    On 23/08/2022 16:06, Ron AARON wrote:
    On 22/08/2022 13:57, dxforth wrote:
    On 22/08/2022 17:04, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Rarely
    does one see languages offering Euclidean division 'out of the box'
    as (presumably) the cost/benefit isn't there.

    The number of occurences in the table in
    <https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages> >>>>
    is:

    97 Truncated (symmetric)
    58 Floored
    12 Euclidean

    A number of languages have several.  The Forth standard specified that >>>> /, mod etc. have to be either floored or symmetric, so if someone
    thinks that Euclidean is worth the extra effort, they should add that
    with separate names, e.g. /e, mode, em/mod (note that development
    Gforth has /f /s modf mods etc.).

    It suffices the majority of languages to have one.  Why not Forth?

    I've appreciated this discussion, and ended up reviewing how 8th does
    mod and /mod.  It turns out there were discrepancies which I've fixed
    for the next release.

    In particular: there was a long time ago a discussion in 8th
    requesting that 'mod' always give a positive value (because it's often
    used to calculate an array offset or the like). Since it makes as much
    sense mathematically, I did that. But...

    Not to dampen the enthusiasm of your users, here's an expert's somewhat
    more nuanced opinion:

     "As a mathematician I was asked about the arithmetic words. I did write
      some comments at the request of a Team member to assist his presentation
      -- and I also told the Team that, from a mathematicians view, it is more
      important that arithmetic operations act in a specific and uniform way
      across all systems than in the precise nature of action." - John Wavrik

    Well, yes. But mathematicians are a tiny, tiny, portion of (my) users.
    As long as the behavior is consistent and provably correct, everyone
    (should be) happy.


    ... I didn't make sure that the result of /mod obeyed the modulo
    relation, e.g. that "n m mod" producing "r q" on the stack such that
      n = (q*m) + r

    typo correction: "/mod", not "mod"... but you knew that :)


    I fixed that; and then realized that the results of /mod were
    different (though all mathematically correct) across the different
    numeric types in 8th. So I've now fixed that.

    And to think I studied a lot of math back in the day...

    It appears 8th could do with a 'Hayes tester' :)


    Well, it does have a test suite, which gets expanded with every such,
    ehem, "issue" found...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Ron AARON on Wed Aug 24 16:42:14 2022
    On 24/08/2022 15:44, Ron AARON wrote:


    On 24/08/2022 8:40, dxforth wrote:
    On 23/08/2022 16:06, Ron AARON wrote:
    On 22/08/2022 13:57, dxforth wrote:
    On 22/08/2022 17:04, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Rarely
    does one see languages offering Euclidean division 'out of the box' >>>>>> as (presumably) the cost/benefit isn't there.

    The number of occurences in the table in
    <https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages> >>>>> is:

    97 Truncated (symmetric)
    58 Floored
    12 Euclidean

    A number of languages have several.  The Forth standard specified that >>>>> /, mod etc. have to be either floored or symmetric, so if someone
    thinks that Euclidean is worth the extra effort, they should add that >>>>> with separate names, e.g. /e, mode, em/mod (note that development
    Gforth has /f /s modf mods etc.).

    It suffices the majority of languages to have one.  Why not Forth?

    I've appreciated this discussion, and ended up reviewing how 8th does mod and /mod.  It turns out there were discrepancies which I've fixed for the next release.

    In particular: there was a long time ago a discussion in 8th requesting that 'mod' always give a positive value (because it's often used to calculate an array offset or the like). Since it makes as much sense mathematically, I did that. But...

    Not to dampen the enthusiasm of your users, here's an expert's somewhat
    more nuanced opinion:

      "As a mathematician I was asked about the arithmetic words. I did write >>    some comments at the request of a Team member to assist his presentation
       -- and I also told the Team that, from a mathematicians view, it is more
       important that arithmetic operations act in a specific and uniform way >>    across all systems than in the precise nature of action." - John Wavrik

    Well, yes. But mathematicians are a tiny, tiny, portion of (my) users. As long as the behavior is consistent and provably correct, everyone (should be) happy.

    Not everyone was happy with Forth-83 (or ANS). 8th users must be of a different
    breed :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ron AARON@21:1/5 to dxforth on Wed Aug 24 10:13:44 2022
    On 24/08/2022 9:42, dxforth wrote:
    On 24/08/2022 15:44, Ron AARON wrote:


    On 24/08/2022 8:40, dxforth wrote:
    On 23/08/2022 16:06, Ron AARON wrote:
    On 22/08/2022 13:57, dxforth wrote:
    On 22/08/2022 17:04, Anton Ertl wrote:
    dxforth <dxforth@gmail.com> writes:
    Rarely
    does one see languages offering Euclidean division 'out of the box' >>>>>>> as (presumably) the cost/benefit isn't there.

    The number of occurences in the table in
    <https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages>

    is:

    97 Truncated (symmetric)
    58 Floored
    12 Euclidean

    A number of languages have several.  The Forth standard specified >>>>>> that
    /, mod etc. have to be either floored or symmetric, so if someone
    thinks that Euclidean is worth the extra effort, they should add that >>>>>> with separate names, e.g. /e, mode, em/mod (note that development
    Gforth has /f /s modf mods etc.).

    It suffices the majority of languages to have one.  Why not Forth?

    I've appreciated this discussion, and ended up reviewing how 8th
    does mod and /mod.  It turns out there were discrepancies which I've
    fixed for the next release.

    In particular: there was a long time ago a discussion in 8th
    requesting that 'mod' always give a positive value (because it's
    often used to calculate an array offset or the like). Since it makes
    as much sense mathematically, I did that. But...

    Not to dampen the enthusiasm of your users, here's an expert's somewhat
    more nuanced opinion:

      "As a mathematician I was asked about the arithmetic words. I did
    write
       some comments at the request of a Team member to assist his
    presentation
       -- and I also told the Team that, from a mathematicians view, it
    is more
       important that arithmetic operations act in a specific and uniform
    way
       across all systems than in the precise nature of action." - John
    Wavrik

    Well, yes. But mathematicians are a tiny, tiny, portion of (my) users.
    As long as the behavior is consistent and provably correct, everyone
    (should be) happy.

    Not everyone was happy with Forth-83 (or ANS).  8th users must be of a different
    breed :)

    Well, I'm usually quite responsive to complaints when there are any.
    Sometimes though I just have to say "it's not going to change".

    That's an advantage to the "committee of one"...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to albert@cherry. on Sat Aug 27 13:55:54 2022
    albert@cherry.(none) (albert) writes:
    the expression x(modulo 3) is in fact the infinite set
    { ... x-2*3 x-1*3 x x+1*3 x+2*3 ... }

    That is an interesting way to define the mod function, but it means that
    x mod 0 is the singleton {x}. What should happen on X 0 MOD ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to no.email@nospam.invalid on Sun Aug 28 01:55:16 2022
    In article <87pmglz9r9.fsf@nightsong.com>,
    Paul Rubin <no.email@nospam.invalid> wrote:
    albert@cherry.(none) (albert) writes:
    the expression x(modulo 3) is in fact the infinite set
    { ... x-2*3 x-1*3 x x+1*3 x+2*3 ... }

    That is an interesting way to define the mod function, but it means that
    x mod 0 is the singleton {x}. What should happen on X 0 MOD ?

    mod 0 is "verboten" similar to division by 0.

    I showed the definition to stress that modulo sets are not functions.

    It is good that the expression x%y is used, in c family.
    That is an operator that yield a result that is of the same type
    of x and y, meaning the least representant r such that
    r is equivalent to x with respect to the modulus y,
    and that 0<=r<=y .

    I don't like the use mod in pascal/modula and Forth, because
    it is used as an infix operator.
    Problems like "what is 2^100 (modulo 5) " actually ask for the
    remainder if 2^100 is divided by 5, not for the infinite set.
    So the mathematicians are themselves sloppy.

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to albert@cherry. on Sun Aug 28 12:41:32 2022
    albert@cherry.(none) (albert) writes:
    In article <87pmglz9r9.fsf@nightsong.com>,
    Paul Rubin <no.email@nospam.invalid> wrote:
    albert@cherry.(none) (albert) writes:
    the expression x(modulo 3) is in fact the infinite set
    { ... x-2*3 x-1*3 x x+1*3 x+2*3 ... }
    ...
    I showed the definition to stress that modulo sets are not functions.

    The notation I learned in school is neither a function nor a set, it's
    an equivalence relation:

    a ≡ b (mod N)

    spoken "a is congruent to b modulo N". In terms of sets, there are N
    sets modulo N, and if a and b are from the same set, they are
    congruent modulo N. These N sets can be represented by, e.g., the
    numbers 0..N-1, or the numbers -N+1..0, or any other numbers. For
    checking congruence, it does not matter, as long as you use only one
    number to represent the set. For other uses, it makes a difference.

    BTW, if you define that modulo is an operation that has a set as a
    result, it is a function, just as if you define it with a number as
    result.

    It is good that the expression x%y is used, in c family.
    That is an operator that yield a result that is of the same type
    of x and y, meaning the least representant r such that
    r is equivalent to x with respect to the modulus y,
    and that 0<=r<=y .

    C99 specifies symmetric division for / and %, so the result of % can
    be negative.

    I don't like the use mod in pascal/modula and Forth, because
    it is used as an infix operator.
    Problems like "what is 2^100 (modulo 5) " actually ask for the
    remainder if 2^100 is divided by 5, not for the infinite set.
    So the mathematicians are themselves sloppy.

    Algebra is often defined through equivalence relations, where
    equivalent sets are represented by the same symbols. A natural number represents all sets with the same cardinality. E.g., 3 represents the
    set of three apples as well as the set of three oranges.

    Likewise, you can represent the set of natural or integer numbers that
    are congruent modulo N by one number, which happens to be the
    remainder of integer division.

    With that desired property, symmetric MOD is not ok (it produces two
    different results that represent the same set for a given N), while
    floored and "Euclidean" are ok.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to Anton Ertl on Sun Aug 28 15:49:57 2022
    In article <2022Aug28.144132@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
    albert@cherry.(none) (albert) writes:
    In article <87pmglz9r9.fsf@nightsong.com>,
    Paul Rubin <no.email@nospam.invalid> wrote:
    albert@cherry.(none) (albert) writes:
    the expression x(modulo 3) is in fact the infinite set
    { ... x-2*3 x-1*3 x x+1*3 x+2*3 ... }
    ...
    I showed the definition to stress that modulo sets are not functions.

    The notation I learned in school is neither a function nor a set, it's
    an equivalence relation:

    a ≡ b (mod N)

    spoken "a is congruent to b modulo N". In terms of sets, there are N
    sets modulo N, and if a and b are from the same set, they are
    congruent modulo N. These N sets can be represented by, e.g., the
    numbers 0..N-1, or the numbers -N+1..0, or any other numbers. For
    checking congruence, it does not matter, as long as you use only one
    number to represent the set. For other uses, it makes a difference.

    Why do you paraphrase what I said?


    BTW, if you define that modulo is an operation that has a set as a
    result, it is a function, just as if you define it with a number as
    result.

    And get the conclusion wrong?
    This was the conclusion: (modulo p) is not an operation, it is a
    part of a textual presentation of a math definition.

    <SNIP>
    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html >comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marcel Hendrix@21:1/5 to Anton Ertl on Mon Aug 29 09:44:15 2022
    On Sunday, August 28, 2022 at 3:10:02 PM UTC+2, Anton Ertl wrote:
    [..]
    The notation I learned in school is neither a function nor a set, it's
    an equivalence relation:

    a ≡ b (mod N)

    spoken "a is congruent to b modulo N". In terms of sets, there are N
    sets modulo N, and if a and b are from the same set, they are
    congruent modulo N. These N sets can be represented by, e.g., the
    numbers 0..N-1, or the numbers -N+1..0, or any other numbers. For
    checking congruence, it does not matter, as long as you use only one
    number to represent the set. For other uses, it makes a difference.

    [..]

    Likewise, you can represent the set of natural or integer numbers that
    are congruent modulo N by one number, which happens to be the
    remainder of integer division.

    With that desired property, symmetric MOD is not ok (it produces two different results that represent the same set for a given N), while
    floored and "Euclidean" are ok.

    I encountered both the set idea and the (working around) division with
    negative remainders in this exponentiation algorithm:

    https://cp-algorithms.com/algebra/factoring-exp.html

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)