]> icculus.org git repositories - taylor/freespace2.git/blob - include/floating.h
Initial revision
[taylor/freespace2.git] / include / floating.h
1 /*
2  * $Logfile: /Freespace2/code/Math/Floating.h $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Low-level floating point math macros and routines
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:12  root
11  * Initial revision
12  *
13  * 
14  * 4     4/07/99 6:22p Dave
15  * Fred and Freespace support for multiple background bitmaps and suns.
16  * Fixed link errors on all subprojects. Moved encrypt_init() to
17  * cfile_init() and lcl_init(), since its safe to call twice.
18  * 
19  * 3     11/05/98 4:18p Dave
20  * First run nebula support. Beefed up localization a bit. Removed all
21  * conditional compiles for foreign versions. Modified mission file
22  * format.
23  * 
24  * 2     10/07/98 10:53a Dave
25  * Initial checkin.
26  * 
27  * 1     10/07/98 10:49a Dave
28  * 
29  * 13    2/26/98 3:28p John
30  * Changed all sqrt's to use fl_sqrt.  Took out isqrt function
31  * 
32  * 12    1/26/98 10:43p Mike
33  * Make ships not all zoom away from an impending shockwave at the same
34  * time.  Based on ai class and randomness
35  * 
36  * 11    1/17/98 3:32p Mike
37  * Add rand_range(), returns random float in min..max.
38  * 
39  * 10    7/29/97 2:36p Hoffoss
40  * Added header file required by _isnan().
41  * 
42  * 9     7/29/97 2:35p Hoffoss
43  * Added a NaN check macro.
44  * 
45  * 8     2/17/97 5:18p John
46  * Added a bunch of RCS headers to a bunch of old files that don't have
47  * them.
48  *
49  * $NoKeywords: $
50  */
51
52 #ifndef _FLOATING_H
53 #define _FLOATING_H
54
55 #include <math.h>
56 #include <float.h>
57
58 extern float fl_isqrt( float a );
59 extern float frand();
60 extern int rand_chance(float frametime, float chance = 1.0f);
61 float frand_range(float min, float max);
62
63 // determine if a floating point number is NaN (Not a Number)
64 #define fl_is_nan(fl) _isnan(fl)
65
66 // Handy macros to prevent type casting all over the place
67
68 #define fl_sqrt(fl) (float)sqrt((float)(fl))
69 #define fl_isqrt(fl) (1.0f/(float)sqrt((float)(fl)))
70 #define fl_abs(fl) (float)fabs((double)(fl))
71 #define i2fl(i) ((float)(i))
72 #define fl2i(fl) ((int)(fl))
73 #define flceil(fl) (int)ceil(fl)
74 #define flfloor(fl) (int)floor(fl)
75 #define f2fl(fx) ((float)(fx)/65536.0f)
76 #define fl2f(fl) (int)((fl)*65536.0f)
77
78 // convert a measurement in degrees to radians
79 #define fl_radian(fl)   ((float)((fl * 3.14159f)/180.0f))
80
81 // convert a measurement in radians to degrees
82 #define fl_degrees(fl)  ((float)((fl * 180.0f)/3.14159))
83
84 // use this instead of:
85 // for:  (int)floor(x+0.5f) use fl_round_2048(x)
86 //       (int)ceil(x-0.5f)  use fl_round_2048(x)
87 //       (int)floor(x-0.5f) use fl_round_2048(x-1.0f)
88 //       (int)floor(x)      use fl_round_2048(x-0.5f)
89 // for values in the range -2048 to 2048
90 // use this instead of:
91 // for:  (int)floor(x+0.5f) use fl_round_2048(x)
92 //       (int)ceil(x-0.5f)  use fl_round_2048(x)
93 //       (int)floor(x-0.5f) use fl_round_2048(x-1.0f)
94 //       (int)floor(x)      use fl_round_2048(x-0.5f)
95 // for values in the range -2048 to 2048
96
97 extern const float *p_fl_magic;
98
99 inline int fl_round_2048( float x )
100 {
101         double tmp_quad;
102         tmp_quad = x + *p_fl_magic;
103         return *((int *)&tmp_quad);
104 }
105
106 /*
107 inline float fl_sqrt( float x)
108 {
109         float retval;
110
111         _asm fld x
112         _asm fsqrt
113         _asm fstp retval
114         
115         return retval;
116 }
117
118 float fl_isqrt( float x )
119 {
120         float retval;
121
122         _asm fld x
123         _asm fsqrt
124         _asm fstp retval
125         
126         return 1.0f / retval;
127
128 */
129
130
131
132 // rounds off a floating point number to a multiple of some number
133 extern float fl_roundoff(float x, int multiple);
134
135
136 #endif
137