P
Paulo Matos
Hi all,
I'm doing some numerical computations with integers, rationals (from
boost), floating point numbers, etc. I'm defining ints as long int,
rationals (boost class), and floating point are doubles. I'm creating
an interface to these types called just 'number', so that I don't need
to worry with conversions, exactness, etc.
Initial approach was something like:
class number {
public:
....
private:
enum type {INT, FLOAT, RAT};
union {
long inum;
double fnum;
rational<long> rnum;
};
type t;
.....
};
Problem is rational has constructors, which are disallowed in union.
Another way would be to have number as abstract base class and then
have 3 derived classes, one for each type but that seems too cumbersome
and I would like to avoid inheritance in this case. I could also have a
pointer to a rational instead of a rational but during thousands and
thousands of computations I guess having a pointer to a rational and
constantly mallocing and deleting it is a performance nightmare. Yet
another solution would be to forget the union and to waste space by
having all three datatypes outside an union but at any time using only
the one to which the type refers to.
I'm open to ideas, suggestions and comments.
Thanks in advance,
Paulo Matos
I'm doing some numerical computations with integers, rationals (from
boost), floating point numbers, etc. I'm defining ints as long int,
rationals (boost class), and floating point are doubles. I'm creating
an interface to these types called just 'number', so that I don't need
to worry with conversions, exactness, etc.
Initial approach was something like:
class number {
public:
....
private:
enum type {INT, FLOAT, RAT};
union {
long inum;
double fnum;
rational<long> rnum;
};
type t;
.....
};
Problem is rational has constructors, which are disallowed in union.
Another way would be to have number as abstract base class and then
have 3 derived classes, one for each type but that seems too cumbersome
and I would like to avoid inheritance in this case. I could also have a
pointer to a rational instead of a rational but during thousands and
thousands of computations I guess having a pointer to a rational and
constantly mallocing and deleting it is a performance nightmare. Yet
another solution would be to forget the union and to waste space by
having all three datatypes outside an union but at any time using only
the one to which the type refers to.
I'm open to ideas, suggestions and comments.
Thanks in advance,
Paulo Matos