Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #58075

Re: mixed double and single signed division

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: mixed double and single signed division
Date 2017-04-25 08:40 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2017Apr25.104007@mips.complang.tuwien.ac.at> (permalink)
References (1 earlier) <odh5gb$1j9o$1@gioia.aioe.org> <zY2dneeIGsT8u2HFnZ2dnUU7-V-dnZ2d@supernews.com> <f8a33236-78f6-462e-9bc1-67e2ca5c92b7@googlegroups.com> <2017Apr24.151206@mips.complang.tuwien.ac.at> <57830405-b5fe-4bf5-88dd-88a3838f1341@googlegroups.com>

Show all headers | View raw


krishna.myneni@ccreweb.org writes:
>On Monday, April 24, 2017 at 8:23:17 AM UTC-5, Anton Ertl wrote:
>> krishna.myneni@ccreweb.org writes:
>...
>> >It's also worth noting that FM/MOD and SM/REM have undefined behavior ("=
>ambiguous condition") when the quotient overflows a single length integer.
>>=20
>> Note that an ambiguous condition in the Forth standard is more like
>> "implementation-defined" behaviour than "undefined behaviour" in the C
>> standard.  A standard system is required to document what happens on
>> undefined behaviour, and time-travel is not part of the Forth standard
>> (but it is part of the C standard).
>>=20
>
>I'm having trouble seeing the practical difference between "implementation-=
>defined" behavior and "undefined behavior" -- the behavior of a given imple=
>mentation for the condition will be specifiable.

One might think so, but the C standards lawyers and the gcc
maintainers make a big thing of the difference.  In particular, they
treat undefined behaviour as an invitation to unleash their favourite
pets, the nasal demons, but not (yet?) for implementation-defined
behaviour.

> Perhaps =
>there are reasons why garbage answers should be allowed to propagate in the=
> subsequent computations on an embedded system, but I have difficulty seein=
>g how that could be benign.

Actually, I find the postscript behaviour of terminating on every
error pretty annoying: I need the data for all the non-erroneous cases
to come out, and for the erroneous case, nothing should be shown.
Postscript does not have something like CATCH, so I have to do input
checking on a lot of operations.

>This brings up the question of other arithmetic overflow. For the case of a=
>ddition and subtraction on two's complement arithmetic, there is wrapping b=
>ehavior, without a processor exception, and this behavior is widely expecte=
>d and maybe even utilized. However, for multiplication, say of two single l=
>ength integers, we could easily get an arithmetic overflow condition which =
>also does not generate an exception but wraps, e.g.
>
>-1 1 lshift 1 rshift constant MAX_N
>
>MAX_N 4 *=20
>
>On a 32-bit system, MAX_N is 2147483647 and multiplying by 4 gives -4 for a=
> single length result. This is clearly wrapping behavior, as a result of ta=
>king only the lower dword of the product. The x86 IMUL instruction does set=
> the carry and overflow flags, so it's easily to know when an arithmetic ov=
>erflow occurred. Nevertheless, I'm guessing most Forth systems do not bothe=
>r checking for an overflow on this condition. It seems to me like this cond=
>ition should be important to flag to the programmer/user.=20
>
>We could be safe in the above example by using M* , which means converting =
>the first operand to a double signed integer, so that the product is a doub=
>le length integer.

M* has the stack effect ( n1 n2 -- d )

> Then, the subsequent computations would have to be done =
>on doubles,

If you just want to catch the overflow case, you can do it as follows:

: n* ( n1 n2 -- n )
  m* >r s>d r> <> -11 and throw ;

: u* ( u1 u2 -- u )
  um* 0<> -11 and throw ;

and then use these words in your program.

>It seems better, in m=
>y opinion, to have the Forth system generate an exception (throw an error) =
>for the overflow condition involving * .

* works on signed and on unsigned numbers, and whether something is an
overflow depends on what kinds of numbers you work on, so your idea
cannot work for *.

Moreover, the current draft for the next standard specifies that *
(among others) ignores overflow and uses modulo arithmetics.

- 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: http://www.forth200x.org/forth200x.html
   EuroForth 2017: http://www.euroforth.org/ef17/

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-22 08:21 -0700
  Re: mixed double and single signed division NN <november.nihal@gmail.com> - 2017-04-22 09:14 -0700
    Re: mixed double and single signed division NN <november.nihal@gmail.com> - 2017-04-22 09:22 -0700
      Re: mixed double and single signed division Coos Haak <htrofhc@gmail.com> - 2017-04-22 23:31 +0200
        Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-23 06:44 -0700
          Re: mixed double and single signed division hughaguilar96@gmail.com - 2017-04-23 13:24 -0700
      Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-23 06:41 -0700
        Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-23 13:29 -0700
  Re: mixed double and single signed division Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-22 11:40 -0500
    Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-23 06:52 -0700
  Re: mixed double and single signed division "HAA" <someone@microsoft.com> - 2017-04-23 13:02 +1000
    Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-22 17:58 -1000
      Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-23 13:36 -0700
        Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-23 16:33 -0700
          Re: mixed double and single signed division anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-24 13:23 +0000
            Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-24 18:49 -0700
        Re: mixed double and single signed division anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-24 13:12 +0000
          Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-24 18:35 -0700
            Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-24 15:52 -1000
              Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-25 02:18 -0700
              Re: mixed double and single signed division Paul Rubin <no.email@nospam.invalid> - 2017-04-25 02:25 -0700
                Re: mixed double and single signed division anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-25 11:30 +0000
                Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-25 08:08 -1000
                Re: mixed double and single signed division anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-26 14:48 +0000
            Re: mixed double and single signed division anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-25 08:40 +0000
              Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-25 02:25 -0700
                Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-25 03:00 -0700
                Re: mixed double and single signed division anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-25 11:37 +0000
                Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-25 05:27 -0700
                Re: mixed double and single signed division anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-04-25 13:34 +0000
                Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-25 15:29 -0700
                Re: mixed double and single signed division "HAA" <someone@microsoft.com> - 2017-04-27 14:02 +1000
                Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-27 16:00 -0700
                Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-27 19:20 -1000
                Re: mixed double and single signed division "HAA" <someone@microsoft.com> - 2017-04-28 22:41 +1000
            Re: mixed double and single signed division Coos Haak <htrofhc@gmail.com> - 2017-04-25 15:32 +0200
              Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-25 15:22 -0700
    Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-23 06:55 -0700
      Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-23 13:30 -1000
        Re: mixed double and single signed division krishna.myneni@ccreweb.org - 2017-04-23 16:45 -0700
        Re: mixed double and single signed division "HAA" <someone@microsoft.com> - 2017-04-24 10:56 +1000
          Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-23 15:06 -1000
            Re: mixed double and single signed division hughaguilar96@gmail.com - 2017-04-23 19:10 -0700
            Re: mixed double and single signed division Paul Rubin <no.email@nospam.invalid> - 2017-04-23 19:12 -0700
              Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-23 16:31 -1000
                Re: mixed double and single signed division hughaguilar96@gmail.com - 2017-04-23 21:17 -0700
                Re: mixed double and single signed division Paul Rubin <no.email@nospam.invalid> - 2017-04-23 21:47 -0700
                Re: mixed double and single signed division hughaguilar96@gmail.com - 2017-04-23 22:58 -0700
                Re: mixed double and single signed division hughaguilar96@gmail.com - 2017-04-23 23:22 -0700
                Re: mixed double and single signed division Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-04-24 04:46 -0500
            Re: mixed double and single signed division "HAA" <someone@microsoft.com> - 2017-04-24 12:59 +1000
              Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-23 18:03 -1000
                Re: mixed double and single signed division hughaguilar96@gmail.com - 2017-04-23 21:22 -0700
                Re: mixed double and single signed division "HAA" <someone@microsoft.com> - 2017-04-25 12:53 +1000
                Re: mixed double and single signed division "Elizabeth D. Rather" <erather@forth.com> - 2017-04-24 21:55 -1000
                Re: mixed double and single signed division "HAA" <someone@microsoft.com> - 2017-04-25 20:00 +1000

csiph-web