Parent Directory | Revision Log
Revision 17 - (view) (download)
1 : | ian | 1 | /* Float arithmetic |
2 : | * | ||
3 : | * (c) Copyright 1999, Artran, Inc. | ||
4 : | * Written by Greg Garner (gmg@artran.com) | ||
5 : | * Modified in March 2001 to include user defined | ||
6 : | * operators for the floating point functions. | ||
7 : | * | ||
8 : | * This file is provided as is (no warranties). | ||
9 : | */ | ||
10 : | |||
11 : | #if defined _float_included | ||
12 : | #endinput | ||
13 : | #endif | ||
14 : | #define _float_included | ||
15 : | |||
16 : | /* Different methods of rounding */ | ||
17 : | enum floatround_method { | ||
18 : | floatround_round = 0, | ||
19 : | floatround_floor, | ||
20 : | floatround_ceil, | ||
21 : | floatround_tozero | ||
22 : | ian | 17 | }; |
23 : | ian | 1 | |
24 : | enum anglemode { | ||
25 : | radian = 0, | ||
26 : | degrees, | ||
27 : | grades | ||
28 : | ian | 17 | }; |
29 : | ian | 1 | |
30 : | /* Convert an integer into a floating point value */ | ||
31 : | native Float:float(value); | ||
32 : | |||
33 : | /* Convert a string into a floating point value */ | ||
34 : | native Float:floatstr(const string[]); | ||
35 : | |||
36 : | /* Multiple two floats together */ | ||
37 : | native Float:floatmul(Float:oper1, Float:oper2); | ||
38 : | |||
39 : | /* Divide the dividend float by the divisor float */ | ||
40 : | native Float:floatdiv(Float:dividend, Float:divisor); | ||
41 : | |||
42 : | /* Add two floats together */ | ||
43 : | native Float:floatadd(Float:dividend, Float:divisor); | ||
44 : | |||
45 : | /* Subtract oper2 float from oper1 float */ | ||
46 : | native Float:floatsub(Float:oper1, Float:oper2); | ||
47 : | |||
48 : | /* Return the fractional part of a float */ | ||
49 : | native Float:floatfract(Float:value); | ||
50 : | |||
51 : | /* Round a float into a integer value */ | ||
52 : | native floatround(Float:value, floatround_method:method=floatround_round); | ||
53 : | |||
54 : | /* Compare two integers. If the two elements are equal, return 0. | ||
55 : | * If the first argument is greater than the second argument, return 1, | ||
56 : | * If the first argument is less than the second argument, return -1. */ | ||
57 : | native floatcmp(Float:fOne, Float:fTwo); | ||
58 : | |||
59 : | /* Return the square root of the input value, same as floatpower(value, 0.5) */ | ||
60 : | native Float:floatsqroot(Float:value); | ||
61 : | |||
62 : | /* Return the value raised to the power of the exponent */ | ||
63 : | native Float:floatpower(Float:value, Float:exponent); | ||
64 : | |||
65 : | /* Return the logarithm */ | ||
66 : | native Float:floatlog(Float:value, Float:base=10.0); | ||
67 : | |||
68 : | /* Return the sine, cosine or tangent. | ||
69 : | * The input angle may be in radians, degrees or grades. */ | ||
70 : | native Float:floatsin(Float:value, anglemode:mode=radian); | ||
71 : | native Float:floatcos(Float:value, anglemode:mode=radian); | ||
72 : | native Float:floattan(Float:value, anglemode:mode=radian); | ||
73 : | |||
74 : | /* Return the hyperbolic sine, cosine or tangent. | ||
75 : | * The input angle may be in radians, degrees or grades. */ | ||
76 : | native Float:floatsinh(Float:angle, anglemode:mode=radian); | ||
77 : | native Float:floatcosh(Float:angle, anglemode:mode=radian); | ||
78 : | native Float:floattanh(Float:angle, anglemode:mode=radian); | ||
79 : | |||
80 : | /* Return the absolute value */ | ||
81 : | native Float:floatabs(Float:value); | ||
82 : | |||
83 : | /* Return the angle of a sine, cosine or tangent. | ||
84 : | * The output angle may be in radians, degrees, or grades. */ | ||
85 : | native Float:floatatan(Float:angle, {anglemode,_}:radix); | ||
86 : | native Float:floatacos(Float:angle, {anglemode,_}:radix); | ||
87 : | native Float:floatasin(Float:angle, {anglemode,_}:radix); | ||
88 : | native Float:floatatan2(Float:x, Float:y, {anglemode,_}:radix); | ||
89 : | |||
90 : | #pragma rational Float | ||
91 : | |||
92 : | /* user defined operators */ | ||
93 : | native Float:operator*(Float:oper1, Float:oper2) = floatmul; | ||
94 : | native Float:operator/(Float:oper1, Float:oper2) = floatdiv; | ||
95 : | native Float:operator+(Float:oper1, Float:oper2) = floatadd; | ||
96 : | native Float:operator-(Float:oper1, Float:oper2) = floatsub; | ||
97 : | |||
98 : | stock Float:operator++(Float:oper) | ||
99 : | return oper+1.0; | ||
100 : | |||
101 : | stock Float:operator--(Float:oper) | ||
102 : | return oper-1.0; | ||
103 : | |||
104 : | stock Float:operator-(Float:oper) | ||
105 : | return oper^Float:cellmin; /* IEEE values are sign/magnitude */ | ||
106 : | |||
107 : | stock Float:operator*(Float:oper1, oper2) | ||
108 : | return floatmul(oper1, float(oper2)); /* "*" is commutative */ | ||
109 : | |||
110 : | stock Float:operator/(Float:oper1, oper2) | ||
111 : | return floatdiv(oper1, float(oper2)); | ||
112 : | |||
113 : | stock Float:operator/(oper1, Float:oper2) | ||
114 : | return floatdiv(float(oper1), oper2); | ||
115 : | |||
116 : | stock Float:operator+(Float:oper1, oper2) | ||
117 : | return floatadd(oper1, float(oper2)); /* "+" is commutative */ | ||
118 : | |||
119 : | stock Float:operator-(Float:oper1, oper2) | ||
120 : | return floatsub(oper1, float(oper2)); | ||
121 : | |||
122 : | stock Float:operator-(oper1, Float:oper2) | ||
123 : | return floatsub(float(oper1), oper2); | ||
124 : | |||
125 : | stock bool:operator==(Float:oper1, Float:oper2) | ||
126 : | return floatcmp(oper1, oper2) == 0; | ||
127 : | |||
128 : | stock bool:operator==(Float:oper1, oper2) | ||
129 : | return floatcmp(oper1, float(oper2)) == 0; /* "==" is commutative */ | ||
130 : | |||
131 : | stock bool:operator!=(Float:oper1, Float:oper2) | ||
132 : | return floatcmp(oper1, oper2) != 0; | ||
133 : | |||
134 : | stock bool:operator!=(Float:oper1, oper2) | ||
135 : | return floatcmp(oper1, float(oper2)) != 0; /* "==" is commutative */ | ||
136 : | |||
137 : | stock bool:operator>(Float:oper1, Float:oper2) | ||
138 : | return floatcmp(oper1, oper2) > 0; | ||
139 : | |||
140 : | stock bool:operator>(Float:oper1, oper2) | ||
141 : | return floatcmp(oper1, float(oper2)) > 0; | ||
142 : | |||
143 : | stock bool:operator>(oper1, Float:oper2) | ||
144 : | return floatcmp(float(oper1), oper2) > 0; | ||
145 : | |||
146 : | stock bool:operator>=(Float:oper1, Float:oper2) | ||
147 : | return floatcmp(oper1, oper2) >= 0; | ||
148 : | |||
149 : | stock bool:operator>=(Float:oper1, oper2) | ||
150 : | return floatcmp(oper1, float(oper2)) >= 0; | ||
151 : | |||
152 : | stock bool:operator>=(oper1, Float:oper2) | ||
153 : | return floatcmp(float(oper1), oper2) >= 0; | ||
154 : | |||
155 : | stock bool:operator<(Float:oper1, Float:oper2) | ||
156 : | return floatcmp(oper1, oper2) < 0; | ||
157 : | |||
158 : | stock bool:operator<(Float:oper1, oper2) | ||
159 : | return floatcmp(oper1, float(oper2)) < 0; | ||
160 : | |||
161 : | stock bool:operator<(oper1, Float:oper2) | ||
162 : | return floatcmp(float(oper1), oper2) < 0; | ||
163 : | |||
164 : | stock bool:operator<=(Float:oper1, Float:oper2) | ||
165 : | return floatcmp(oper1, oper2) <= 0; | ||
166 : | |||
167 : | stock bool:operator<=(Float:oper1, oper2) | ||
168 : | return floatcmp(oper1, float(oper2)) <= 0; | ||
169 : | |||
170 : | stock bool:operator<=(oper1, Float:oper2) | ||
171 : | return floatcmp(float(oper1), oper2) <= 0; | ||
172 : | |||
173 : | stock bool:operator!(Float:oper) | ||
174 : | return (_:oper & ((-1)/2)) == 0; /* -1 = all bits to 1; /2 = remove most significant bit (sign) | ||
175 : | works on both 32bit and 64bit systems; no constant required */ | ||
176 : | /* forbidden operations */ | ||
177 : | forward operator%(Float:oper1, Float:oper2); | ||
178 : | forward operator%(Float:oper1, oper2); | ||
179 : | ian | 17 | forward operator%(oper1, Float:oper2); |
180 : | |||
181 : | |||
182 : | stock Float:floatmin(Float:ValueA, Float:ValueB) | ||
183 : | { | ||
184 : | if (ValueA<=ValueB) | ||
185 : | { | ||
186 : | return ValueA; | ||
187 : | } | ||
188 : | |||
189 : | return ValueB; | ||
190 : | } | ||
191 : | |||
192 : | stock Float:floatmax(Float:ValueA, Float:ValueB) | ||
193 : | { | ||
194 : | if (ValueA>=ValueB) | ||
195 : | { | ||
196 : | return ValueA; | ||
197 : | } | ||
198 : | |||
199 : | return ValueB; | ||
200 : | } | ||
201 : | stock Float:floatclamp(Float:Value, Float:MinValue, Float:MaxValue) | ||
202 : | { | ||
203 : | if (Value<=MinValue) | ||
204 : | { | ||
205 : | return MinValue; | ||
206 : | } | ||
207 : | if (Value>=MaxValue) | ||
208 : | { | ||
209 : | return MaxValue; | ||
210 : | } | ||
211 : | |||
212 : | return Value; | ||
213 : | } |
Contact | ViewVC Help |
Powered by ViewVC 1.0.4 |