On Sun, 3 Apr 2005 12:33:22 -0500, me22 wrote
On Apr 3, 2005 5:08 AM, Jedediah Smith
wrote: I need a type equivelant to rational<int> but supporting two special values, +inf and -inf which compare greater and less (respectively) to all other values besides themselves. I don't need to perform any arithmetic on the inf values so those operations can be undefined.
What is the best strategy for creating such a type? Would it be better to subclass rational or create an underlying integer type with the inf values and use rational based on that underlying type? If the former option, how exactly would I do it?
I expect that subclassing wouldn't be a good idea since none of the critical functions are virtual ( for good -- speed -- reason ). The way that immediatly jumps to mind is copying rational<> and changing it to allow a zero denominator to represent infinities, although this violates OnceAndOnlyOnce, so perhaps a class that has a rational as a member would be a more maintainable option, but I expect it would be to run and more annoying to code.
A with_inf<> wrapper for a numeric type seems to me to be the best option as it would be straight-foreward and usable in other contexts. The only problems I see with that is your denominator would also get overhead from checking for infs ( when it should never have them ) and that there's no simple way for a rational< with_inf<> > divide-by-zero to return the appropriate +/- inf value.
FYI, Boost date-time has an int_adpater class that provides infinities and NAN values to a pure integer type. It's not perfectly suited for rational as-is since it has a few date-time specific things, but they could be easily factored out. And yes, it adds overhead to operators since the special values need to be checked. http://www.boost.org/boost/date_time/int_adapter.hpp Jeff