@@ -19,4 +19,118 @@ int ClampToInt(int Value, int Min, int Max) {
19
19
if (Value < Min) return Min;
20
20
else if (Value > Max) return Max;
21
21
else return Value;
22
+ }
23
+
24
+ // 函数3: 整数除以255
25
+ // 参考: 无
26
+ // 简介: 移位
27
+ int Div255 (int Value) {
28
+ return (((Value >> 8 ) + Value + 1 ) >> 8 );
29
+ }
30
+
31
+ // 函数4: 取绝对值
32
+ // 参考: https://oi-wiki.org/math/bit/
33
+ // 简介: 比n > 0 ? n : -n 快
34
+
35
+ int Abs (int n) {
36
+ return (n ^ (n >> 31 )) - (n >> 31 );
37
+ /* n>>31 取得 n 的符号,若 n 为正数,n>>31 等于 0,若 n 为负数,n>>31 等于 - 1
38
+ 若 n 为正数 n^0=0, 数不变,若 n 为负数有 n^-1
39
+ 需要计算 n 和 - 1 的补码,然后进行异或运算,
40
+ 结果 n 变号并且为 n 的绝对值减 1,再减去 - 1 就是绝对值 */
41
+ }
42
+
43
+ // 函数5: 四舍五入
44
+ // 参考: 无
45
+ // 简介: 无
46
+ double Round (double V)
47
+ {
48
+ return (V > 0.0 ) ? floor (V + 0.5 ) : Round (V - 0.5 );
49
+ }
50
+
51
+ // 函数6: 返回-1到1之间的随机数
52
+ // 参考: 无
53
+ // 简介: 无
54
+ double Rand ()
55
+ {
56
+ return (double )rand () / (RAND_MAX + 1.0 );
57
+ }
58
+
59
+ // 函数7: Pow函数的近似计算,针对double类型和float类型
60
+ // 参考: http://www.cvchina.info/2010/03/19/log-pow-exp-approximation/
61
+ // 参考: http://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/
62
+ // 简介: 这个函数只是为了加速的近似计算,有5%-12%不等的误差
63
+ double Pow (double X, double Y)
64
+ {
65
+ Approximation V = { X };
66
+ V.X [1 ] = (int )(Y * (V.X [1 ] - 1072632447 ) + 1072632447 );
67
+ V.X [0 ] = 0 ;
68
+ return V.Value ;
69
+ }
70
+
71
+
72
+ float Pow (float X, float Y)
73
+ {
74
+ Approximation V = { X };
75
+ V.X [1 ] = (int )(Y * (V.X [1 ] - 1072632447 ) + 1072632447 );
76
+ V.X [0 ] = 0 ;
77
+ return (float )V.Value ;
78
+ }
79
+
80
+ // 函数8: Exp函数的近似计算,针对double类型和float类型
81
+ double Exp (double Y) // 用联合体的方式的速度要快些
82
+ {
83
+ Approximation V;
84
+ V.X [1 ] = (int )(Y * 1485963 + 1072632447 );
85
+ V.X [0 ] = 0 ;
86
+ return V.Value ;
87
+ }
88
+
89
+ float Exp (float Y) // 用联合体的方式的速度要快些
90
+ {
91
+ Approximation V;
92
+ V.X [1 ] = (int )(Y * 1485963 + 1072632447 );
93
+ V.X [0 ] = 0 ;
94
+ return (float )V.Value ;
95
+ }
96
+
97
+ // 函数9: Pow函数更准一点的近似计算,但是速度会稍慢
98
+ // http://martin.ankerl.com/2012/01/25/optimized-approximative-pow-in-c-and-cpp/
99
+ // Besides that, I also have now a slower approximation that has much less error
100
+ // when the exponent is larger than 1. It makes use exponentiation by squaring,
101
+ // which is exact for the integer part of the exponent, and uses only the exponent’s fraction for the approximation:
102
+ // should be much more precise with large Y
103
+
104
+ double PrecisePow (double X, double Y){
105
+ // calculate approximation with fraction of the exponent
106
+ int e = (int )Y;
107
+ Approximation V = { X };
108
+ V.X [1 ] = (int )((Y - e) * (V.X [1 ] - 1072632447 ) + 1072632447 );
109
+ V.X [0 ] = 0 ;
110
+ // exponentiation by squaring with the exponent's integer part
111
+ // double r = u.d makes everything much slower, not sure why
112
+ double r = 1.0 ;
113
+ while (e)
114
+ {
115
+ if (e & 1 ) r *= X;
116
+ X *= X;
117
+ e >>= 1 ;
118
+ }
119
+ return r * V.Value ;
120
+ }
121
+
122
+ // 函数10: 返回Min到Max之间的随机数
123
+ // 参考: 无
124
+ // 简介: Min为随机数的最小值,Max为随机数的最大值
125
+ int Random (int Min, int Max){
126
+ return rand () % (Max + 1 - Min) + Min;
127
+ }
128
+
129
+ // 函数11: 符号函数
130
+ // 参考: 无
131
+ // 简介: 无
132
+ int sgn (int X){
133
+ if (X > 0 ) return 1 ;
134
+ if (X < 0 ) return -1 ;
135
+ return 0 ;
22
136
}
0 commit comments