• Create Account

### #Actuallemming77

Posted 19 July 2012 - 09:11 AM

Hi,

As I'm sure is standard practice, I have written a library to implement types like vectors, matrices, and so on, and it all works quite nicely. However I have hit a hurdle.

Using D3D9 for graphics, that seems to like having it's data in the form of single precision floats. Which is fair enough. But I'm feeling the need to do some intense physics simulation using double precision. So what I really want is some means of implementing both without duplicating my entire library.

My current implementation lets me switch between Float and Double types before compiling by means of a typedef instruction. But this doesn't quite give me the solution I need, as I'm stuck choosing one over the other.

[source lang="cpp"]// The library is called Util. Not a great name, I know. Here's a small excerpt// from Util.h. Define DOUBLE before compiling to use the double type.// Otherwise, use the float type.#ifdef DOUBLEtypedef double Real;#define NAMESPACE Maths::Double#elsetypedef float Real;#define NAMESPACE Maths#endifnamespace Util { namespace NAMESPACE { struct Angle; // Euler angle struct Matrix; // 4x4 Matrix struct Quaternion; struct Vector3; }}// An excerpt from Vector3.hnamespace util { namespace NAMESPACE { // Vector3, 3D coordinates struct Vector3 { public: Real x, y, z; Vector3(); // x=0 y=0 z=0 Vector3(Real); // x=a y=a z=a Vector3(Real, Real, Real); // x=a y=b z=c/// ... Etc. };}[/source]

Can anyone recommend a means to implement both types into the same library? Preferably while duplicating as little source code as possible.

I'm using Visual C++ 2010 Express. Thanks

### #1lemming77

Posted 19 July 2012 - 02:00 AM

Hi,

As I'm sure is standard practice, I have written a library to implement types like vectors, matrices, and so on, and it all works quite nicely. However I have hit a hurdle.

Using D3D9 for graphics, that seems to like having it's data in the form of single precision floats. Which is fair enough. But I'm feeling the need to do some intense physics simulation using double precision. So what I really want is some means of implementing both without duplicating my entire library.

My current implementation lets me switch between Float and Double types before compiling by means of a typedef instruction. But this doesn't quite give me the solution I need, as I'm stuck choosing one over the other.

[source lang="cpp"]// The library is called Util. Not a great name, I know. Here's a small excerpt// from Util.h. Define DOUBLE before compiling to use the double type.// Otherwise, use the float type.#ifdef DOUBLEtypedef double Real;#define NAMESPACE Maths::Double#elsetypedef float Real;#define NAMESPACE Maths#endifnamespace Util { namespace NAMESPACE { struct Angle; // Euler angle struct Matrix; // 4x4 Matrix struct Quaternion; struct Vector3; }}// An excerpt from Vector3.hnamespace util { namespace NAMESPACE { // Vector3, 3D coordinates struct Vector3 { public: Real x, y, z; Vector3(); // x=0 y=0 z=0 Vector3(Real); // x=a y=a z=a Vector3(Real, Real, Real); // x=a y=b z=c Vector3 Cross(const Vector3 &a); // Returns Cross Product of this and a Vector3 ComponentProduct(const Vector3 &a); // Multiply each component of the vector together Real Length(void); // Returns length of vector Real LengthSqr(void); // Returns length^2 of vector. Note that this is faster than Length() Real Dot(const Vector3 &a); void Normalize(void); // Converts vector to unit length Vector3 operator+(const Vector3 &a); Vector3 operator-(const Vector3 &a); Vector3 operator/(const Vector3 &a);/// ... Etc. };}[/source]

Can anyone recommend a means to implement both types into the same library? Preferably while duplicating as little source code as possible.

I'm using Visual C++ 2010 Express. Thanks

PARTNERS