|
Boost : |
Subject: Re: [boost] [xint] Boost.XInt formal review
From: Kim Barrett (kab.conundrums_at_[hidden])
Date: 2011-03-12 12:52:09
On Mar 12, 2011, at 7:37 AM, Thomas Klimpel wrote:
> I'm not so sure about whether "division" is a basic operation with respect to an integer. An "efficient" implementation of "division" might be required as a building block for "efficient" implementations of other algorithms (like GCD or LCM), but I wouldn't say that "division" is required to "define" an integer.
>
> An additional complication with respect to division is the "remainder". At least some algorithms for "division" will compute the "remainder" at the same time as the "quotient". So should the division be defined to yield both "remainder" and "quotient" at the same time? And how should "division" be defined exactly for negative numbers? Should the "remainder" always be a positive number, or should the absolute value of the "remainder" be as small as possible, or should the sign of the numbers be mostly ignored for division (that's what C/C++ try to do)?
I'm not entirely certain of this, but I think Thomas is alluding to C89's implementation-defined rounding behavior for division involving negative numbers, which C++98/2003 followed. Note that C99 tightened this up, requiring truncation toward zero behavior. The latest C++0x working draft follows C99 in this respect, also requiring truncation toward zero. I would hope (though haven't looked) that XInt similarly performs truncation toward zero.
Regarding quotient / remainder computation, I would think that an appropriate overload of std::div() would suffice. I haven't looked to see if XInt provides such an overload.
> The point how this all is related to the XInt review is that on the one hand, XInt can't be blamed if it tries to follow the example from C/C++ for consistency reasons, but on the other hand, C/C++ is probably a poor example in this specific domain. Even Ada, which nobody likes for good reasons unrelated to this domain, does a much better job than C/C++ here.
If XInt follows C99 / C++0x here, does that answer your complaint?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk