On Sat, 27 Mar 2010 13:32:24 +0200, Robert Clipsham <[email protected]> wrote:

On 27/03/10 10:20, so wrote:
In C++!

I have a type defined in the core library like..
typedef float scalar;
//typedef double scalar; // <-- whole framework is now double precision

alias float scalar;
//alias double scalar;


Next i instantiate vectors, matrices etc... from templates.
typedef vector_t<scalar, 3> vector;
typedef matrix_t<scalar, 3, 3> matrix;

alias Vector!(scalar, 3) vector; // Presuming you have defined
                                  // the Vector!() template somewhere
alias Matrix!(scalar, 3, 3) matrix; // Presuming Matrix!() is defined

Until now everything cool, here pain comes...

const scalar a = scalar(0.2) * math::consts<scalar>::pi; // can't drop
cast, since 0.2 is double

const scalar a = 0.2 * PI; // PI is defined in std.math

const scalar b = a * scalar(0.9) + scalar(5); // " "

const scalar b = a * 0.9 * 5.0;

const vector v = vector(8.0) * scalar(3.0); // can't drop cast, error

const vector v = vector(8.0) * 3.0;

...

Since D is superb, i like to know how you do it in D.
If you got a better idea in C++, i would like to hear that too!

Thanks!


There are a few vector implementations for D out there, or you can roll your own. I'm pretty sure there's matrices out there too, I haven't checked though (I'm pretty sure D's built in arrays will do the trick, I'm not an expert though).

Even the example looks/sounds simple, i am not that new in C++/D programming :) If one comes to D from C++, he is most likely pushing limits of the C++ templates/numerics. PI was just an example, i could find another constant, but then you would point me to a SI unit implementation. :)

For :
const scalar a = 0.2 * PI; // both double implicit cast to scalar, when it is float, this must produce a warning?
const scalar b = a * 0.9 * 5.0; // same
const vector v = vector(8.0) * 3.0; // 3.0 is a double, when scalar is float, this line must not compile

I think you should try it first, DMD should also give warnings/errors here.

Thanks!

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Reply via email to