Jump to content
  • Advertisement
Sign in to follow this  
PolyVox

Design of maths classes

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I've been writing a maths library and have a simple design question. If you have a simple class, say a vector, and you want to implement an Add() method, do you have the object modify the original object or return a new one? The most natural thing seems to be to create a new vector and return that, as this is how it works for the primative data types such as int. It is also what overloaded operators are supposed to do in C++ (though I'm actually using Java). But the problem is it involves calling 'new' to allocate some memory; this seems like an expensive thing to do in something like a vector 'add' method. The alternative is that vector1.add(vector2) could overwrite the original value of vector 1. The problem is that if you want to keep the original value you have to make a copy and you code starts to get a little messy. So what do you guys think and what approach do you take? David

Share this post


Link to post
Share on other sites
Advertisement
Generally you would return it by value, thus it could be created on the stack, don't create it on the heap.

Share this post


Link to post
Share on other sites
Quote:
Original post by esuvs
The most natural thing seems to be to create a new vector and return that, as this is how it works for the primative data types such as int. It is also what overloaded operators are supposed to do in C++ (though I'm actually using Java). But the problem is it involves calling 'new' to allocate some memory; this seems like an expensive thing to do in something like a vector 'add' method.


I would:

1. Provide a function like this:

tVector3 operator+(const tVector3& lhs, const tVector3& rhs);

since it's nice and convenient for writing expressions like a + b. However, it does create a temporary object (on the stack, NOT using new), which is overhead. The compiler is allowed to optimise it away, but I wouldn't really trust that in performance critical code, so in addition I'd:

2. Provide a function like this:

void AddVector3Vector3(tVector3& result, const tVector3& lhs, const tVector3& rhs);

(or a template function called just Add)

This avoids all copies, and if inline will avoid all overhead.

In addition there are some expressions that come up a lot - for example transforming a vector with a rotation and a translation - that can be combined into one function call to avoid the overhead of creating temporaries.

Incidently, your vector1.add(vector3) is just the same as operator+=

Share this post


Link to post
Share on other sites
what you seem to be searching for is to make a function that turns two vectors into three without allocating any extra memory...its not possible.

as you say, there are two ways to do this (pulling out the java syntax...)

1) public static vector3 add(vector3 vec1, vector3 vec2) {}

and

2) public void add(vector3 vec) {}

(static version: add(vector3 vec1, vector3 vec2) {} //result in vec1)

#1 creates and returns a new vector, #2 overwrites the calling vector. as you say, you dont want to be allocating a new vector every time you add, so 2 is really the better choice. you make the user of the library *explicitly* create a copy when/if they need to keep it...just make an easy to use copy method and it shouldnt be a problem.

incidentally, theres another way, less convenient and even more explicit for the user...

3) public static void add(vector3 src1, vector3 src2, vector3 result) {}

where the user supplies the output variable.

as a final option, though this is treading into some seriously un-threadsafe territory, you can have a static variable internal to the vector class you can return containing the result, but that can get really ugly even without synchronization issues.

generally number 2 and in some circumstances number 3 are your best options.

Share this post


Link to post
Share on other sites
Quote:
Original post by justo
as a final option, though this is treading into some seriously un-threadsafe territory, you can have a static variable internal to the vector class you can return containing the result, but that can get really ugly even without synchronization issues.


This is never a good solution. It shouldn't even be mentioned. For starters, it would make (a+b)==(c+d) always true regardless of the values of a,b,c and d.

Share this post


Link to post
Share on other sites
Ok, thanks guys. Seems the best thing to do cover all my options. I'll provide two functions for each case; for 'adding' I'll have an 'add' function and a 'plus' function.

a = b.plus(c); //doesn't modify b.

b.add(c); //does modify b

Must say, I love Java but do miss C++'s operator overloading!

Quote:
Original post by Fruny
Quote:
Original post by justo
as a final option, though this is treading into some seriously un-threadsafe territory, you can have a static variable internal to the vector class you can return containing the result, but that can get really ugly even without synchronization issues.


This is never a good solution. It shouldn't even be mentioned. For starters, it would make (a+b)==(c+d) always true regardless of the values of a,b,c and d.


Hmmm... can you clarify the problem? Within my Vector class I do have a static instance which I use as a temporary variable. I never return it though (espesially as Java does everything by reference), just use it in calculations. Thats perfectly safe in single-threaded code, right? And I would have thought it could be made thread safe fairly easily but i'd have to check how java handles threading.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!