[Oberon] Modulus on negative number
Jan de Kruyf
jan.de.kruyf at gmail.com
Wed May 17 08:12:47 CEST 2017
Wojciech,
read the book, chapter 16
----------------------------------------------
16.3. Floating-point arithmetic
The RISC uses the IEEE Standard for representing REAL (floating-point)
numbers with 32 bits.
The word is divided into 3 fields: s for the sign, e for the exponent, and
m for the mantissa. The
value is
x = (-1)s × 2e-127 × 1.m with 1.0 ≤ m < 2.0
(normalized form)
Numbers are represented in sign-magnitude form. This implies that for sign
inversion only the sign
bit must be inverted, and exponent and mantissa remain unchanged.
Zero is a special case represented by 32 0-bits, and therefore has to be
treated separately.
Furthermore, e = 255 denotes "not a number". It is generated in the case of
arithmetic overflow.
-------------------------------------
Traditionally oberon raised a trap on div by 0.
See also the Trap routine in System.Mod.
I have not checked yet how the O07 sources / compiler handle all this. I
will keep my eyes open while I am building this quick and dirty Scheme /
XML to Risc compiler.
Cheers,
J.
On Wed, May 17, 2017 at 2:11 AM, Skulski, Wojciech <
skulski at pas.rochester.edu> wrote:
> > x DIV 0 is undefined.
>
> I thought than every programming language will rise a trap on division by
> 0, either integer or floating point. I would be surprised if Oberon did not.
>
> The following two questions are somewhat related to this discussion.
>
> 1. Does Oberon support NaN? If not then maybe it should?
>
> 2. Could the "undefined" become "defined as NaN" in all numerical cases
> which are now undefined?
>
> Rationale: The presently "undefined" wasnot implemented for efficiency
> reasons. (Correct me if this statement is wrong.) If the result is truly
> undefined and left to the implementation, then wrong results can be quietly
> produced and passed to further processing. On the other hand, if the
> "undefined" becomes a NaN, then NaNs can be handled by the application code.
>
> Example: I know of at least one system (IGOR from Wavemetrics) where I can
> initialize the arrays with NaNs and then selectively fill them with good
> data. Whatever is not filled will stay NaN and then it is omitted from
> plotting. IGOR interprets NaNs as "do not care". It is a very effective
> solution in practice.
>
> Any thoughts?
>
> Wojtek
> ________________________________________
> From: Oberon [oberon-bounces at lists.inf.ethz.ch] on behalf of Jörg [
> joerg.straube at iaeth.ch]
> Sent: Tuesday, May 16, 2017 2:33 AM
> To: ETH Oberon and related systems
> Subject: Re: [Oberon] Modulus on negative number
>
> 0 MOD x is 0 for all x except 0. In Oberon only defined for positive x
>
> x DIV 0 is undefined. If you read the hitchhiker's guide to the galaxy the
> answer is 42 :-)
>
> br
> Jörg
>
> > Am 15.05.2017 um 20:45 schrieb Tomas Kral <thomas.kral at email.cz>:
> >
> > Hi,
> >
> > EDIT - the reason I ask, is that until now I thought that a remainder
> > is a computing synonym for the modulus.
> >
> > Also, I remember that one of the early
> > Intel processors had problems with zero division, but some
> > maths people argued that it was alright as zero division depends on the
> > rate of convergence.
> >
> > But Maths was never my cup of tea.
> >
> > At last I took some interest in this MOD'ulus discussion. I wonder how
> > it is (un)defined in Math / Oberon
> >
> > x DIV 0
> > 0 MOD x
> >
> > Tomas
> >
> > On Mon, 15 May 2017 09:57:08 +0200
> > Jörg <joerg.straube at iaeth.ch> wrote:
> >
> >> It is. MOD is defined as C x C -> C
> >> Jörg
> >>
> >>> Am 14.05.2017 um 20:17 schrieb Aubrey McIntosh
> >>> <aubrey.mcintosh at utexas.edu>:
> >>>
> >>> I have not checked my number theory book lately, but I don't think
> >>> (i.e., don't remember) that it is defined mathematically for
> >>> negative divisors either.
> >>>
> >>>> On Sat, May 13, 2017 at 3:12 AM, Peter Matthias
> >>>> <PeterMatthias at web.de> wrote: Agreed. However, -1 MOD -12 or 1 MOD
> >>>> -12 is not defined in Oberon.
> >>>>
> >>>>
> >>>>> Am 12.05.2017 um 22:43 schrieb Aubrey McIntosh:
> >>>>> for -1 MOD 12, the mathematically correct answers which are
> >>>>> consistent with the language report, are
> >>>>> q=-1, r=11.
> >>>>>
> >>>>> This definition works very well, for example, to implement wrap
> >>>>> around strip chart displays.
> >>>>>
> >>>>>
> >>>>> On Fri, May 12, 2017 at 2:28 PM, Peter Matthias
> >>>>> <PeterMatthias at web.de <mailto:PeterMatthias at web.de>> wrote:
> >>>>>
> >>>>> Warming up the thread to give supposedly correct answer:
> >>>>>
> >>>>> Am 16.02.2017 um 00:00 schrieb Peter Matthias:
> >>>>>
> >>>>>
> >>>>>
> >>>>> Am 15.02.2017 um 04:21 schrieb Srinivas Nayak:
> >>>>>
> >>>>> Dear All,
> >>>>>
> >>>>> Recently I come across modulus on a negative number.
> >>>>> Will it produce a negative number or positive?
> >>>>> Someone says both are correct!
> >>>>> https://urldefense.proofpoint.com/v2/url?u=http-3A__
> stackoverflow.com_a_4403556&d=DwIGaQ&c=kbmfwr1Yojg42sGEpaQh5ofMHBeTl9
> EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_BM9w1wVOuyqPwHi3XzJRa-ybV0&m=
> ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-VkduQrd87yJ_s&s=X-
> h4LxwGezYLCFsYvDAYoiGUS5kUt32UCwcp6a3YmWs&e=
> >>>>> <https://urldefense.proofpoint.com/v2/url?u=http-
> 3A__stackoverflow.com_a_4403556&d=DwIGaQ&c=kbmfwr1Yojg42sGEpaQh5ofMHBeTl9
> EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_BM9w1wVOuyqPwHi3XzJRa-ybV0&m=
> ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-VkduQrd87yJ_s&s=X-
> h4LxwGezYLCFsYvDAYoiGUS5kUt32UCwcp6a3YmWs&e= >
> >>>>> Which one is mathematically correct?
> >>>>> Surprisingly different languages calculate it
> >>>>> differently even! What is Oberon's way?
> >>>>>
> >>>>>
> >>>>> The theory was already answered. In practice, all compiler
> >>>>> implementations I used (native X86, Shark, MIPS), give
> >>>>> wrong result when
> >>>>> both, divident and divisor are negative. I fixed it just
> >>>>> yesterday for
> >>>>> all non x86 versions.
> >>>>>
> >>>>>
> >>>>> I should have read the language report before making such
> >>>>> claims.
> >>>>>
> >>>>> Oberon Report says:
> >>>>>
> >>>>> "The operators DIV and MOD apply to integer operands only.
> >>>>> They are related by the following formulas defined for any
> >>>>> dividend x and positive divisors y:
> >>>>> x = (x DIV y) * y + (x MOD y)
> >>>>> 0 ≤ (x MOD y) < y"
> >>>>>
> >>>>> Oberon07-Report says:
> >>>>>
> >>>>> "The operators DIV and MOD apply to integer operands only.
> >>>>> Let q = x DIV y, and r = x MOD y.
> >>>>> Then quotient q and remainder r are defined by the equation
> >>>>> x = q*y + r 0 <= r < y"
> >>>>>
> >>>>> Last statement obviously cannot be met if y is negative.
> >>>>>
> >>>>> So in short: Don't use DIV/MOD for negative divisors as the
> >>>>> result is not defined.
> >>>>>
> >>>>>> From the implemtation point of view this perfectly makes
> >>>>>> sense as
> >>>>> negative divisors are seldom used and correction for DIV of
> >>>>> the usually stupid hardware implementation only takes 3 additional
> >>>>> instructions compared to at least 6 for a complete definition.
> >>>>> Simple SHIFT/AND instructions for power of 2 divisors easily
> >>>>> outwight these 3 additional instructions.
> >>>>>
> >>>>>
> >>>>> Peter
> >>>>>
> >>>>> --
> >>>>> Oberon at lists.inf.ethz.ch <mailto:Oberon at lists.inf.ethz.ch>
> >>>>> mailing list for ETH Oberon and related systems
> >>>>> https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.
> inf.ethz.ch_mailman_listinfo_oberon&d=DwIGaQ&c=
> kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_
> BM9w1wVOuyqPwHi3XzJRa-ybV0&m=ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-
> VkduQrd87yJ_s&s=esLBXjxJsUPeCtzIcBqI4xjbU1MDc9qNKXrXL9_h5XM&e=
> >>>>> <https://urldefense.proofpoint.com/v2/url?u=https-
> 3A__lists.inf.ethz.ch_mailman_listinfo_oberon&d=DwIGaQ&c=
> kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_
> BM9w1wVOuyqPwHi3XzJRa-ybV0&m=ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-
> VkduQrd87yJ_s&s=esLBXjxJsUPeCtzIcBqI4xjbU1MDc9qNKXrXL9_h5XM&e= >
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>> --
> >>>>> Aubrey McIntosh, Ph.D.
> >>>>> 1502 Devon Circle
> >>>>> Austin TX 78723
> >>>>> (512) 348-7401
> >>>>>
> >>>>>
> >>>>>
> >>>>> --
> >>>>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related
> >>>>> systems https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.
> inf.ethz.ch_mailman_listinfo_oberon&d=DwIGaQ&c=
> kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_
> BM9w1wVOuyqPwHi3XzJRa-ybV0&m=ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-
> VkduQrd87yJ_s&s=esLBXjxJsUPeCtzIcBqI4xjbU1MDc9qNKXrXL9_h5XM&e=
> >>>>>
> >>>> --
> >>>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related
> >>>> systems https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.
> inf.ethz.ch_mailman_listinfo_oberon&d=DwIGaQ&c=
> kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_
> BM9w1wVOuyqPwHi3XzJRa-ybV0&m=ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-
> VkduQrd87yJ_s&s=esLBXjxJsUPeCtzIcBqI4xjbU1MDc9qNKXrXL9_h5XM&e=
> >>>
> >>>
> >>>
> >>> --
> >>> Aubrey McIntosh, Ph.D.
> >>> 1502 Devon Circle
> >>> Austin TX 78723
> >>> (512) 348-7401
> >>>
> >>> --
> >>> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related
> >>> systems https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.
> inf.ethz.ch_mailman_listinfo_oberon&d=DwIGaQ&c=
> kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_
> BM9w1wVOuyqPwHi3XzJRa-ybV0&m=ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-
> VkduQrd87yJ_s&s=esLBXjxJsUPeCtzIcBqI4xjbU1MDc9qNKXrXL9_h5XM&e=
> >
> >
> >
> > --
> > Tomas Kral <thomas.kral at email.cz>
> > --
> > Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.
> inf.ethz.ch_mailman_listinfo_oberon&d=DwIGaQ&c=
> kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_
> BM9w1wVOuyqPwHi3XzJRa-ybV0&m=ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-
> VkduQrd87yJ_s&s=esLBXjxJsUPeCtzIcBqI4xjbU1MDc9qNKXrXL9_h5XM&e=
> >
> >
> > --
> > Tomas Kral <thomas.kral at email.cz>
> > --
> > Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.
> inf.ethz.ch_mailman_listinfo_oberon&d=DwIGaQ&c=
> kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_
> BM9w1wVOuyqPwHi3XzJRa-ybV0&m=ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-
> VkduQrd87yJ_s&s=esLBXjxJsUPeCtzIcBqI4xjbU1MDc9qNKXrXL9_h5XM&e=
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.
> inf.ethz.ch_mailman_listinfo_oberon&d=DwIGaQ&c=
> kbmfwr1Yojg42sGEpaQh5ofMHBeTl9EI2eaqQZhHbOU&r=uUiA_zLpwaGJIlq-_
> BM9w1wVOuyqPwHi3XzJRa-ybV0&m=ps5mHvQsNtHNKZSV9DHVBm8g9m0ba-
> VkduQrd87yJ_s&s=esLBXjxJsUPeCtzIcBqI4xjbU1MDc9qNKXrXL9_h5XM&e=
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20170517/e75c1e5a/attachment.html>
More information about the Oberon
mailing list