Boost logo

Boost :

Subject: Re: [boost] [xint] Boost.XInt formal review
From: Thomas Klimpel (Thomas.Klimpel_at_[hidden])
Date: 2011-03-13 10:45:56


Kim Barrett wrote:
> Thomas Klimpel wrote:
> > [snip]
> > 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.

To be honest, I wasn't even aware of this, so thanks for this interesting information. My goal was just to show the difficulties when trying to define "division" for integers.

> 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.

OK, that's at least a step forward from "implementation-defined", but the modulo operator ("%") is still awkward to use in practice (for negative numbers).

> 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.

What would you use as return type for this overload? A std::pair, a boost::tuple, or a specially defined struct (std::div_t) like the other overloads from std?

> > 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?

Probably not, because this was a complaint about C/C++, not about XInt. This complaint was in response to Steven asking "Are the basic operations that are provided not enough for some algorithm that you care about?". From my experience with C/C++, I could answer this question with a definitive "Yes, the basic operations provided by C/C++ for integer numbers are not enough for some algorithms that I care about!". I know that this is a review of XInt, but as I have much more experience with C/C++, it makes sense to answer this question for C/C++ instead, which XInt tries to follow for consistency reasons. I gave two specific example (I could give more, but I guess nobody wants to hear them, because it is not realistic to expect that this deficiency of C/C++ will ever be fixed), one directly related to the current review, and the other related to my own daily work.

Regards,
Thomas


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk