2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Math/Floating.h $
15 * Low-level floating point math macros and routines
18 * Revision 1.2 2002/06/09 04:41:13 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:12 root
25 * 4 4/07/99 6:22p Dave
26 * Fred and Freespace support for multiple background bitmaps and suns.
27 * Fixed link errors on all subprojects. Moved encrypt_init() to
28 * cfile_init() and lcl_init(), since its safe to call twice.
30 * 3 11/05/98 4:18p Dave
31 * First run nebula support. Beefed up localization a bit. Removed all
32 * conditional compiles for foreign versions. Modified mission file
35 * 2 10/07/98 10:53a Dave
38 * 1 10/07/98 10:49a Dave
40 * 13 2/26/98 3:28p John
41 * Changed all sqrt's to use fl_sqrt. Took out isqrt function
43 * 12 1/26/98 10:43p Mike
44 * Make ships not all zoom away from an impending shockwave at the same
45 * time. Based on ai class and randomness
47 * 11 1/17/98 3:32p Mike
48 * Add rand_range(), returns random float in min..max.
50 * 10 7/29/97 2:36p Hoffoss
51 * Added header file required by _isnan().
53 * 9 7/29/97 2:35p Hoffoss
54 * Added a NaN check macro.
56 * 8 2/17/97 5:18p John
57 * Added a bunch of RCS headers to a bunch of old files that don't have
69 extern float fl_isqrt( float a );
71 extern int rand_chance(float frametime, float chance = 1.0f);
72 float frand_range(float min, float max);
74 // determine if a floating point number is NaN (Not a Number)
75 #define fl_is_nan(fl) isnan(fl)
77 // Handy macros to prevent type casting all over the place
79 #define fl_sqrt(fl) (float)sqrt((float)(fl))
80 #define fl_isqrt(fl) (1.0f/(float)sqrt((float)(fl)))
81 #define fl_abs(fl) (float)fabs((double)(fl))
82 #define i2fl(i) ((float)(i))
83 #define fl2i(fl) ((int)(fl))
84 #define flceil(fl) (int)ceil(fl)
85 #define flfloor(fl) (int)floor(fl)
86 #define f2fl(fx) ((float)(fx)/65536.0f)
87 #define fl2f(fl) (int)((fl)*65536.0f)
89 // convert a measurement in degrees to radians
90 #define fl_radian(fl) ((float)((fl * 3.14159f)/180.0f))
92 // convert a measurement in radians to degrees
93 #define fl_degrees(fl) ((float)((fl * 180.0f)/3.14159))
95 // use this instead of:
96 // for: (int)floor(x+0.5f) use fl_round_2048(x)
97 // (int)ceil(x-0.5f) use fl_round_2048(x)
98 // (int)floor(x-0.5f) use fl_round_2048(x-1.0f)
99 // (int)floor(x) use fl_round_2048(x-0.5f)
100 // for values in the range -2048 to 2048
101 // use this instead of:
102 // for: (int)floor(x+0.5f) use fl_round_2048(x)
103 // (int)ceil(x-0.5f) use fl_round_2048(x)
104 // (int)floor(x-0.5f) use fl_round_2048(x-1.0f)
105 // (int)floor(x) use fl_round_2048(x-0.5f)
106 // for values in the range -2048 to 2048
109 extern const float *p_fl_magic;
111 inline int fl_round_2048( float x )
114 tmp_quad = x + *p_fl_magic;
115 return *((int *)&tmp_quad);
118 inline float fl_sqrt( float x)
129 float fl_isqrt( float x )
137 return 1.0f / retval;
143 // rounds off a floating point number to a multiple of some number
144 extern float fl_roundoff(float x, int multiple);