-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathfloating.h
More file actions
102 lines (79 loc) · 3.76 KB
/
floating.h
File metadata and controls
102 lines (79 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#ifndef _FLOATING_H
#define _FLOATING_H
#include <cmath>
#include <cfloat>
#include <limits>
#include "globalincs/pstypes.h" // for PI
extern float frand();
extern int rand_chance(float frametime, float chance = 1.0f);
float frand_range(float min, float max);
// determine if a floating point number is NaN (Not a Number)
inline bool fl_is_nan(float fl) {
return std::isnan(fl);
}
// Handy macros to prevent type casting all over the place
#define fl_sqrt(fl) sqrtf(fl)
#define fl_isqrt(fl) (1.0f/sqrtf(fl))
#define fl_abs(fl) fabsf(fl)
#define i2fl(i) (static_cast<float>(i)) // int to float
#define i2ch(i) (static_cast<char>(i)) // int to char
#define i2sz(i) (static_cast<size_t>(i)) // int to size_t
#define l2d(l) (static_cast<double>(l)) // long to double
#define fl2i(fl) (static_cast<int>(fl)) // float to int
#define ch2i(ch) (static_cast<int>(ch)) // char to int
#define sz2i(sz) (static_cast<int>(sz)) // size_t to int
#define d2l(d) (static_cast<long>(d)) // double to long
#define fl2ir(fl) (static_cast<int>(fl + (((fl) < 0.0f) ? -0.5f : 0.5f))) // float to int, rounding
#define d2lr(d) (static_cast<long>(d + (((d) < 0.0) ? -0.5 : 0.5))) // double to long, rounding
#define f2fl(fx) (static_cast<float>(fx)/65536.0f) // fix to float
#define f2d(fx) (static_cast<double>(fx)/65536.0) // fix to double
#define fl2f(fl) (static_cast<int>((fl)*65536.0f)) // float to fix
#define fl_tan(fl) tanf(fl)
// convert a measurement in degrees to radians
#define fl_radians(fl) (static_cast<float>((fl) * (PI / 180.0f)))
// convert a measurement in radians to degrees
#define fl_degrees(fl) (static_cast<float>((fl) * (180.0f / PI)))
constexpr float DEGREE_UB = 359.99f; // upper bound in degrees when rounding to 100ths place
// convert a measurement in radians to degrees, rounding to the nearest .01 (used in FRED)
constexpr float fl_degrees_100ths(float fl)
{
float val = fl_degrees(fl);
float new_val = fl2ir(val * 100.0f) / 100.0f;
if (new_val > DEGREE_UB)
new_val -= 360.0f;
return new_val;
}
// sees if a floating point number is within a certain threshold (by default, epsilon) of zero
inline bool fl_near_zero(float a, float e = std::numeric_limits<float>::epsilon())
{
return a < e && a > -e;
}
// sees if two floating point numbers are approximately equal, taking into account the argument magnitudes
// see commit c62037
// and see also this article, because fl_equal may need to be rewritten if it is recruited into more demanding situations:
// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
inline bool fl_equal(float a, float b)
{
return fl_abs(a - b) <= FLT_EPSILON * std::max({ 1.0f, fl_abs(a), fl_abs(b) });
}
// sees if two floating point numbers are approximately equal, with a user-specifiable epsilon
inline bool fl_equal(float a, float b, float epsilon)
{
return fl_abs(a - b) <= epsilon;
}
// rounds off a floating point number to a multiple of some number
extern float fl_roundoff(float x, int multiple);
const float GOLDEN_RATIO = 0.618033989f;
float golden_ratio_rand();
// clamps the input to -1, 1 to avoid floating point error putting it outside that range
float acosf_safe(float x);
float asinf_safe(float x);
#endif