S
Saeed Amrollahi
Hi
I began to define a pilot project by myself. It is design and
implementation of
runtime Rational number.
I decided to take the complex numbers in library as a model, because
I thought the design of complex numbers is similar to rational numbers
at least
from point of class design view. Up until now, I experienced a few
problem,
but I want to know your opinion and some guideline about the most
subtle one.
As you know the complex numbers are DefaultConstructible:
template<class T>
class complex {
public:
complex(const T& = T(), const T& = T()); // constructor
// ...
};
with above constructor, you can create Zero (without parameter),
real numbers (with one parameter) and complex number (two parameters).
It is obvious, the rational numbers should be DefaultConstructible,
so like complex numbers I tries to define the rational number like
this:
template<class T>
class rational { // generic rational number
public:
rational(const T& = T(), const T& = /* the representation of "One"
in T */); // constructor
// ...
};
The problem is: How to represent One for Denominator in a Generic
manner?
Of course some libraries like Boost.Rational make the assumption
the template parameter is integral type. But it is not generic, I want
to have rational<bool>, rationa<int>, rational<float>,
rational<double>
, rational<complex>, rational<rational> and ...
Please throw light on the problem.
Also, what is your opinion about multiple constructors vs. single
constructor with default argument:
Rational(const T& = T(), const T& = /* the representation of "One"
in T */);
and
Rational();
Rational(const T&);
Rational(const T&, const T&);
Regards,
-- Saeed Amrollahi
P.S. Is it good idea to send the post to comp.lang.c++.moderated too.
I began to define a pilot project by myself. It is design and
implementation of
runtime Rational number.
I decided to take the complex numbers in library as a model, because
I thought the design of complex numbers is similar to rational numbers
at least
from point of class design view. Up until now, I experienced a few
problem,
but I want to know your opinion and some guideline about the most
subtle one.
As you know the complex numbers are DefaultConstructible:
template<class T>
class complex {
public:
complex(const T& = T(), const T& = T()); // constructor
// ...
};
with above constructor, you can create Zero (without parameter),
real numbers (with one parameter) and complex number (two parameters).
It is obvious, the rational numbers should be DefaultConstructible,
so like complex numbers I tries to define the rational number like
this:
template<class T>
class rational { // generic rational number
public:
rational(const T& = T(), const T& = /* the representation of "One"
in T */); // constructor
// ...
};
The problem is: How to represent One for Denominator in a Generic
manner?
Of course some libraries like Boost.Rational make the assumption
the template parameter is integral type. But it is not generic, I want
to have rational<bool>, rationa<int>, rational<float>,
rational<double>
, rational<complex>, rational<rational> and ...
Please throw light on the problem.
Also, what is your opinion about multiple constructors vs. single
constructor with default argument:
Rational(const T& = T(), const T& = /* the representation of "One"
in T */);
and
Rational();
Rational(const T&);
Rational(const T&, const T&);
Regards,
-- Saeed Amrollahi
P.S. Is it good idea to send the post to comp.lang.c++.moderated too.