Boost logo

Boost :

Subject: Re: [boost] [rational] Over/underflow checked update ready for comments
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-09-05 01:03:55


Le 05/09/12 01:04, Dan Searles a écrit :
>
>>>> Quick overview of the update: the original usage and
>>>> code of the template class remains mostly as before, ie
>>>> boost::rational<int> RationalVar; should work as
>>>> in the original version. Adding 'true' as a second
>>>> template parameter, however, allows an exception to
>>>> be raised if an operation with a class variable results
>>>> in a value that can't accurately be represented
>>> I haven't looked at anything else, but I dislike using true/false as the template argument. I'd prefer meaningful names.
>>>
>> Replaced the bool template parameter with an enum:
>> enum RationalChecktype {RationalNoChecking, RationalCheckforOverflow};
> For those interested in the rational number class, see:
> https://svn.boost.org/svn/boost/sandbox/rational
>
> Requesting comments on the code, and review for
> inclusion in the next release.
>
>

Hi,

the enum type could follow the one defined in
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3352.html

"When an overflow does occur, the desirable behavior depends on the
application, so programmers may specify the overflow mode with a value
of type |enum class overflow|. The possible values are:

|impossible|
    Programmer analysis of the program has determined that overflow
    cannot occur. Uses of this mode should be accompanied by an argument
    supporting the conclusion.
|undefined|
    Programmers are willing to accept undefined behavior in the event of
    an overflow.
|modulus|
    The assigned value is the dynamic value mod the range of the
    variable. This mode makes sense only with unsigned numbers. It is
    useful for angular measures.
|saturate|
    If the dynamic value exceeds the range of the variable, assign the
    nearest representable value.
|exception|
    If the dynamic value exceeds the range of the variable, throw an
    exception of type |std::overflow_error|.
"

BTW, I have no access to the link for the examples and the test.

Could you update the History section?

In order to avoid breaking the existing interface, you could define a
class basic_rational that has the new template and define the current
rational using this basic_rational.

template <typename Integer, overflow Ov> class basic_rational;

template <typename Integer> class rational :
basic_rational<Integer,overflow::undefined> {...}

Best,
Vicente

P.S A link to a standard proposal
* http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3363.html.


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