473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to tackle this problem???

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<longrnum;
};

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

Aug 4 '06 #1
5 1395
"Paulo Matos" <po******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
: 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.

How does your class help with this?
What kind of implicit conversions does it provide?
When a rational is multiplied by a double, I think that
there isn't a good choice to provide an exact result.

: Initial approach was something like:
: class number {
: public:
: ...
:
: private:
: enum type {INT, FLOAT, RAT};
:
: union {
: long inum;
: double fnum;
: rational<longrnum;
: };
:
: type t;
: ....
: };
:
: Problem is rational has constructors, which are disallowed in union.
: [....] 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.

If run-time execution speed is a concern, you'll probably be better
off using "double" everywhere, rather than a wrapper class. Or -
depending on the intended usage - use template functions that accept
parameters of either type.

If you really needed to implement the above design using
boost::rational and without any memory indirection, it would be
possible to store a dummy type within the union, and to manually
construct/destruct the "rational" field in-place (using placement-
new and explicit destructor calls). But is it worth the effort?
The alternative being to re-implement "rational" within your class...
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 4 '06 #2

Ivan Vecerina wrote:
"Paulo Matos" <po******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
: 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.

How does your class help with this?
What kind of implicit conversions does it provide?
When a rational is multiplied by a double, I think that
there isn't a good choice to provide an exact result.
Although I admit such class is general not necessary, or even
recommendable, it's highly interesting in my case where I want to
provide true abstraction between number types without worrying about
what these numbers are.
: Initial approach was something like:
: class number {
: public:
: ...
:
: private:
: enum type {INT, FLOAT, RAT};
:
: union {
: long inum;
: double fnum;
: rational<longrnum;
: };
:
: type t;
: ....
: };
:
: Problem is rational has constructors, which are disallowed in union.
: [....] 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.

If run-time execution speed is a concern, you'll probably be better
off using "double" everywhere, rather than a wrapper class. Or -
depending on the intended usage - use template functions that accept
parameters of either type.
double is definetely not good, although I admit it's good
_performance_wise_. That's what I am using. However, the need for exact
rationals and the common use of integers just made think this class
could be worth it!
If you really needed to implement the above design using
boost::rational and without any memory indirection, it would be
possible to store a dummy type within the union, and to manually
construct/destruct the "rational" field in-place (using placement-
new and explicit destructor calls). But is it worth the effort?
The alternative being to re-implement "rational" within your class...
What do you mean 'construct/destruch the "rational" field in-place'. It
seems I didn't quite get it!
>
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 4 '06 #3
Paulo Matos wrote:
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<longrnum;
};

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.
Memory is cheap. Do you really care that you're wasting space by
having the three datatypes outside a union? Remember, the simplest
solution is often the best one. Try it that way and see if it serves
your purposes.

Good luck.

Best regards,

Tom

Aug 4 '06 #4
On 3 Aug 2006 17:51:32 -0700, "Paulo Matos" <po******@gmail.com>
wrote:
>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<longrnum;
};

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.
If you are already using a boost library, you may consider using
boost::variant instead of a union

Zara
Aug 4 '06 #5
"Paulo Matos" <po******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
: If you really needed to implement the above design using
: boost::rational and without any memory indirection, it would be
: possible to store a dummy type within the union, and to manually
: construct/destruct the "rational" field in-place (using placement-
: new and explicit destructor calls). But is it worth the effort?
: The alternative being to re-implement "rational" within your class...
: >
:
: What do you mean 'construct/destruch the "rational" field in-place'. It
: seems I didn't quite get it!
Basic idea (using a local variable instead of a data member):
#include <new// for standard placement-new operator
typedef rational<longT; // for this demo
unsigned char storage[ sizeof(T) ] //KV: check alignment too
T& var = * new (storage) T; // constructs a T in the storage array

... var is a reference to a valid rational, can be used as such
//KV: if an exception is thrown, ~T() is not called automatically

var.~T(); // explicitly call destructor.

I do not remember the implementation of boost::variant: it will be
using a similar technique if it shares storage among possible
"members". But it might be using indirection (pointer to
heap-allocated instance) instead.

Again, this is too complex to be worth the effort IMO.

Cheers,
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.