[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

This page is part of the web mail archives of SRFI 77 from before July 7th, 2015. The new archives for SRFI 77 contain all messages, not just those from before July 7th, 2015.

*To*: srfi-77@xxxxxxxxxxxxxxxxx*Subject*: Re: div and mod.*From*: Marcin 'Qrczak' Kowalczyk <qrczak@xxxxxxxxxx>*Date*: Sun, 19 Mar 2006 20:51:03 +0100*Delivered-to*: srfi-77@xxxxxxxxxxxxxxxxx*In-reply-to*: <Pine.LNX.4.58.0602212207370.24897@xxxxxxxxxxxxxx> (bear@xxxxxxxxx's message of "Tue, 21 Feb 2006 22:28:54 -0800 (PST)")*Mail-followup-to*: srfi-77@xxxxxxxxxxxxxxxxx*References*: <55047DAD-A9BE-4F10-9287-8BEC3DD88440@xxxxxxxxxxxxxxx> <Pine.LNX.4.58.0602212207370.24897@xxxxxxxxxxxxxx>*Sender*: Marcin 'Qrczak' Kowalczyk <qrczak@xxxxxxxxxx>*User-agent*: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

bear <bear@xxxxxxxxx> writes: > For what it's worth, here is the behavior that I as a programmer > expect from Div and Mod. > > I expect mod always to return a number between zero inclusive and the > modulus exclusive. I agree. Haskell has two pairs of functions, where functions in each pair are consistent with each other in the obvious way: quot & rem - quot is rounded towards zero, the sign of rem is the sign of the dividend div & mod - div is rounded towards negative infinity, the sign of mod is the sign of the divisor (Plus quotRem and divMod which compute both together.) | quot rem div mod -----------+------------------- 123 10 | 12 3 12 3 -123 10 | -12 -3 -13 7 123 -10 | -12 3 -13 -7 -123 -10 | 12 -3 12 -3 Both are primarily used with a positive divisor, in which case they agree on non-negative dividends and provide two most common variants on negative dividends. The div & mod variant is more regular mathematically. When the divisor is a power of 2, it corresponds to bit shifts and masks. Knuth warns about programming languages which understand div & mod differently than this way. The quot & rem variant is consistent with Intel processors and C99, absolute values of results are determined by absolute values of arguments. The GMP library provides the above two families plus a yet another one, rounding towards positive infinity. I dislike using an entirely different variant when the divisor is negative. If the variant with rounding to nearest is worth providing, it should be a separate pair of functions. It's not clear how to break ties: there are various subvariants possible. -- __("< Marcin Kowalczyk \__/ qrczak@xxxxxxxxxx ^^ http://qrnik.knm.org.pl/~qrczak/

**References**:**Questions about srfi-77 Generic Arithmetic***From:*Bradley Lucier

**div and mod.***From:*bear

- Prev by Date:
**miscellaneous request (last one)** - Next by Date:
**Optional base argument for LOG (and friends)** - Previous by thread:
**div and mod.** - Next by thread:
**Re: Questions about srfi-77 Generic Arithmetic** - Index(es):