[AD] small bugfix for the fix class

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


Hi !

I've just found a small bug in the fixed class when 'int' is divided by 'fix'.
The current one (version 3.9.33, current cvs not checked) is definitively 
wrong:
( x / fixtoi(y.v)  will lose the fixbase at all !)

There are more solutions:

1.) this one is a good mix of speed and precision

inline friend fix operator /  (const int x, const fix y)  { fix t;  t.v = 
fdiv(itofix(x), y.v));  return t; }

2.) this one is faster, but simulates an integer-division (for the 
fixed-value too), so it's not very accurate

inline friend fix operator /  (const int x, const fix y)  { fix t;  t.v = 
itofix(x / fixtoi(y.v));  return t; }

3.) this one is the most exact one, but slowest, not really the meaning of 
'fixed'

inline friend fix operator /  (const int x, const fix y)  { fix t;  t.v = 
ftofix((double)x / fixtof(y.v));  return t; }

Perhaps someone with CVS-access could change this one line (and check my 
bugfix, used solution 1, don't know if I made the dif-file the right way, 
it's Shawn's old way :))

Then:
IMHO there is something more to do with this wrapper (could be done in a 
later WIP)
So i.e. I believe the return-value of the comparison functions should be 
bool, not int.
In some trials I had a lot of ambiguos warnings, so bool() and short() should 
be added, and perhaps some more operator functions.  Especially ^ could be 
important for sign-tests (seen in a lot of geometric routines), and therefore 
& and | too (for masking the MSB).

The class could be enhanced *and* reduced in source size, if the explicit 
specialization templates would work, but either I've done it absolutely wrong 
(like in my first trial posted some weeks ago to AL-list), or there is 
something to do in DJGPP. At all I couldn't implement all samples of a good 
reference book. So better we'll wait until this will be standard, or (in case 
it was my fault) I would need someone to help with --- if it's of interest at 
all, (I'm not sure that anyone uses this class because the bug is older ;))

If anyone will check my current enlarged version of the class, which is 
running good for me (perhaps for the port on other compilers too), please 
mail me privately. I will not disturb the making of 3.9.34 with this new 
theme.

Thanks
  Andy
diff -r current/alinline.h previous/alinline.h
1013c1013
<    inline friend fix operator /  (const int x, const fix y)    { fix t;  t.v = fdiv(itofix(x), y.v);     return t; }
---
>    inline friend fix operator /  (const int x, const fix y)    { fix t;  t.v = x / fixtoi(y.v);          return t; }
1015c1015
<    inline friend fix operator /  (const long x, const fix y)   { fix t;  t.v = fdiv(itofix(x), y.v);     return t; }
---
>    inline friend fix operator /  (const long x, const fix y)   { fix t;  t.v = x / fixtoi(y.v);          return t; }


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