[Fwd: Re: [AD] C++ fix math]

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Elias Pschernig wrote:

> I.e., Allegro defines its very own cos function, just with fix types
> instead of double. As a C programmer, I don't like this.. but I guess in
> C++ it's standard to define all math operators/functions for every
> mathematical type. Allegro does not only that though, but apparently

No it's not standard to do that.  I personally would consider that bad
programming practice to define a function for a class outside the class,
rather than as a method.  I find it doubly bad style that it overloads a
function not in the standard namespace.  Note that because of that
Allegro won't work as expected on ISO-C++ 98 compilers as the real
function is now std::cos and not cos, so there will be a ::cos(fix x)
and a std::cos(double) and a std::cos(float).

IMHO the proper technique is to have cos as a method in fix, so that the
method is fix::cos, and the invocation is as follows:

Fix theta( 3.1415926 );
Fix x = theta.cos();

Although, I do agree that syntax looks strange when compared to the
usual "cos( theta );" I don't think it's appropriate to place cos(Fix)
outside of the class as that violates encapsulation, and splits up the
class into various parts which leads to confusing code (as we've seen).

Another method is to make cos a static public function of Fix:

Fix theta( 3.1415926 );
Fix x = Fix::cos( theta );

Which may be a more appealing method from a readability standpoint, but
still enforces encapsulation of operations on the data.

I also may bring this up now as a point of discussion possibly for
Allegro 5: The fix class is the only part of Allegro that's in C++, so
it's kind of out of place.  Using the Fix class (at least on old GCCs a
few years ago), is MUCH slower than using floats or doubles, making the
class pointless when compared to the C functions (new compilers might be
able to optimize out most of that overhead, but also one must consider
that the FPU units on the new CPUs can deal much more quickly with
floating point than before).  So I guess I'm wondering why the Fix class
is even there, and what benefit does it give Allegro, esp with it being
so out of place?

Gillius




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/