Re: [AD] C++ fix math

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


> But the result it prints is:
> 
> 0.999695
> 
> By just not including allegro.h, the result becomes:
> 
> 0.540302
> (and a warning..)

C++ is really amazing!

> The problem is, allegro's fix class will somehow auto-convert the
> float and use the fixed float.. (as i said, i'm not sure how it
> works)

The constructors

   fix(const int x)                          { v = itofix(x); }
   fix(const long x)                         { v = itofix(x); }
   fix(const unsigned int x)                 { v = itofix(x); }
   fix(const unsigned long x)                { v = itofix(x); }
   fix(const float x)                        { v = ftofix(x); }
   fix(const double x)                       { v = ftofix(x); }

are used as conversion operators behind your back, i.e 

    float c = cos(a);

is expanded as:

   float c = float(cos (fix(a)));

> Including <math.h> solves the problem of course. But considering
> there is no warning about it, my proposed fix would be adding #include
> <math.h> into fix.h, it would make cos always work i think.

I'd rather mark the constructors as explicit:

   explicit fix(const int x)                          { v = itofix(x); }
   explicit fix(const long x)                         { v = itofix(x); }
   explicit fix(const unsigned int x)                 { v = itofix(x); }
   explicit fix(const unsigned long x)                { v = itofix(x); }
   explicit fix(const float x)                        { v = ftofix(x); }
   explicit fix(const double x)                       { v = ftofix(x); }

Does anyone see a problem with this approach ?

- Eric



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