Re: [AD] C++ fix math

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday 11 August 2002 20:42, Eric Botcazou wrote:
> Given that the trigonometric functions for the 'fixed' type never
> deal with radians, these are not IMHO the most useful ones. What
> about
>
>     fixed radtofix(float x);
>     float fixtorad(fixed x);
>
> instead ?

OK, two patches attached. One implements the above two methods and their 
documentation, the other adds two methods to the 'fix' class to do much 
the same thing (depending on the C implementations, of course).

Hope they are of some use. It should provide more of a reminder that 
radians don't directly convert to fixed point values, which is perhaps 
more important than any convenience factors.

Bye for now,
- -- 
Laurence Withers,  lwithers@xxxxxxxxxx
(GnuPG 04A646EA) http://www.lwithers.demon.co.uk/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9VsksUdhclgSmRuoRAux5AKCjEHYY1g4uGUrFYJkPEchZfb9PrACgj2Oy
u4Rprojyl9V/XLx6S9eU0E0=
=uzVJ
-----END PGP SIGNATURE-----
diff -ruN old-allegro/include/allegro/fix.h allegro/include/allegro/fix.h
--- old-allegro/include/allegro/fix.h	Sun Aug 11 21:00:45 2002
+++ allegro/include/allegro/fix.h	Sun Aug 11 21:15:20 2002
@@ -199,6 +199,9 @@
    inline friend fix asin(fix x);
    inline friend fix atan(fix x);
    inline friend fix atan2(fix x, fix y);
+
+   double fixtorad() const                   { return fixtorad(v); }
+   static fix radtofix(double x)             { fix t; t.v = radtofix(x); return t; }
 };
 
 #include "inline/fix.inl"
diff -ruN old-allegro/include/allegro/inline/fmaths.inl allegro/include/allegro/inline/fmaths.inl
--- old-allegro/include/allegro/inline/fmaths.inl	Sun Aug 11 21:00:45 2002
+++ allegro/include/allegro/inline/fmaths.inl	Sun Aug 11 21:08:00 2002
@@ -52,6 +52,20 @@
 })
 
 
+AL_INLINE(fixed, radtofix, (double x),
+{
+   /* the constant comes from the 16.16 representation of 256/2pi */
+   return fixmul(ftofix(x), (fixed)2670177) & 0xFFFF;
+})
+
+
+AL_INLINE(double, fixtorad, (fixed x),
+{
+   /* the constant comes from the 16.16 representation of 2pi/256 */
+   return fixtof(fixmul(x & 0xFFFF, (fixed)1608));
+})
+
+
 #ifdef ALLEGRO_NO_ASM
 
 /* use generic C versions */


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