Nп/п : 84 из 100
 От   : Anton Ertl                          2:5075/128        30 сен 23 10:04:10
 К    : Hugh Aguilar                                          30 сен 23 13:20:02
 Тема : Division (was: Hugh Aguilar - TESTRA - What really happened there?)
----------------------------------------------------------------------------------
                                                                                 
@MSGID: <2023Sep30.120410@mips.complang.tuwien.ac.at>
8b1a0b5b
@REPLY:
<c2cbeb05-8191-4cd5-a015-825bf8624a15n@googlegroups.com> 7b2a57b8
@REPLYADDR Anton Ertl
<anton@mips.complang.tuwien.ac.at>
@REPLYTO 2:5075/128 Anton Ertl
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID:
<2023Sep30.120410@mips.complang.tuwien.ac.at>
@RFC-References:
<6363a6bb-7a1a-4289-9883-d4e9b5569a3e@googlegroups.com> 1@dont-email.me>
<72540e7d-0866-4a1c-9067-84305c171250n@googlegroups.com> 1@dont-email.me>
<c1f5a937-56e5-4677-bee5-5724a30676d7n@googlegroups.com> 1@dont-email.me>
<4a00e294-d5a1-43e1-a359-1f4d6ae092d1n@googlegroups.com> <d1147752-dccd-421e-bc64-b92bd85d4687n@googlegroups.com>
<7626edf3-1a44-4fb6-b648-1360da7e2a84n@googlegroups.com> <c2cbeb05-8191-4cd5-a015-825bf8624a15n@googlegroups.com>
@TZUTC: 0000
@TID: FIDOGATE-5.12-ge4e8b94
Hugh Aguilar <hughaguilar96@gmail.com> writes:
>SAFE-UM/MOD would unsigned divide a double on the double stack
>by a number on the single stack, then push the double-precision
>remainder and quotient to the double stack.
>etc.
...
>Note also that SAFE-UM/MOD can`t be written in ANS-Forth,
>despite the fact that it is used internally by # for pictured numbers.

Gforth has:

: ud/mod ( ud1 u2 -- urem udquot ) \\ gforth
    \\G divide unsigned double @i{ud1} by @i{u2}, resulting in a unsigned double
    \\G quotient @i{udquot} and a single remainder @i{urem}.
    over 0= if nip u/mod 0 exit then
    dup >r u/mod r> swap >r um/mod r> ;

The first line is just an optimization for the common case where ud1
actually fits into a single cell, and you can delete it.

With a single-cell divisor, the remainder is guaranteed to fit into a
single cell.

>ANS-Forth provides UM/MOD (6.1.2370) but says:
>"An ambiguous condition exists if [the denominator] is zero or if=20
>the quotient lies outside the range of a single-cell unsigned integer."
>What are you supposed to do if your quotient overflows, which will
>happen if you have a small denominator?

UD/MOD first divides the upper cell of ud1 by u2, then the remainder
of that division and the lower cell by u2.  Both divisions do not
overflow.  Of course, division by 0 is still an ambiguous condition;
you can check for that before performing the division.

>What are you supposed to do if your remainder overflows, which will
>happen if you have a large denominator?

The remainder is always less than the denominator.  Therefore, with a
single denominator, the remainder cannot 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 2023: https://euro.theforth.net/2023
--- xrn 10.11
 * Origin: Institut fuer Computersprachen, Technische Universitaet (2:5075/128)
SEEN-BY: 5001/100 5005/49 5015/255 5019/40 5020/715
848 1042 4441 12000
SEEN-BY: 5030/49 1081 5058/104 5075/128
@PATH: 5075/128 5020/1042 4441



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх