3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
18 #include "sm_globals.h"
20 /****************************************
22 * FLOATING POINT NATIVE IMPLEMENTATIONS *
24 ****************************************/
27 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
29 float val = static_cast<float>(params[1]);
34 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
36 float val = sp_ctof(params[1]);
37 val = (val >= 0) ? val : -val;
42 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
44 float val = sp_ctof(params[1]) + sp_ctof(params[2]);
49 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
51 float val = sp_ctof(params[1]) - sp_ctof(params[2]);
56 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
58 float val = sp_ctof(params[1]) * sp_ctof(params[2]);
63 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
65 float val = sp_ctof(params[1]) / sp_ctof(params[2]);
70 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
72 float val1 = sp_ctof(params[1]);
73 float val2 = sp_ctof(params[2]);
78 } else if (val1 < val2) {
85 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
87 float val = sp_ctof(params[1]);
88 float base = sp_ctof(params[2]);
90 if ((val <= 0) || (base <= 0))
92 return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
98 val = log(val) / log(base);
104 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
106 float val = sp_ctof(params[1]);
108 return sp_ftoc(exp(val));
111 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
113 float base = sp_ctof(params[1]);
114 float exponent = sp_ctof(params[2]);
116 return sp_ftoc(pow(base, exponent));
119 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
121 float val = sp_ctof(params[1]);
125 return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
128 return sp_ftoc(sqrt(val));
131 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
133 float val = sp_ctof(params[1]);
159 val = (float)floor(val + 0.5);
164 return static_cast<int>(val);
167 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
169 float val = sp_ctof(params[1]);
170 val = val - floor(val);
175 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
177 float val = sp_ctof(params[1]);
183 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
185 float val = sp_ctof(params[1]);
191 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
193 float val = sp_ctof(params[1]);
199 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
201 float val = sp_ctof(params[1]);
207 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
209 float val = sp_ctof(params[1]);
215 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
217 float val = sp_ctof(params[1]);
223 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
225 float val1 = sp_ctof(params[1]);
226 float val2 = sp_ctof(params[2]);
227 val1 = atan2(val1, val2);
229 return sp_ftoc(val1);
232 REGISTER_NATIVES(floatnatives)
235 {"FloatMul", sm_FloatMul},
236 {"FloatDiv", sm_FloatDiv},
237 {"FloatAdd", sm_FloatAdd},
238 {"FloatSub", sm_FloatSub},
239 {"FloatFraction", sm_FloatFraction},
240 {"FloatRound", sm_FloatRound},
241 {"FloatCompare", sm_FloatCompare},
242 {"SquareRoot", sm_SquareRoot},
244 {"Exponential", sm_Exponential},
245 {"Logarithm", sm_Logarithm},
247 {"Cosine", sm_Cosine},
248 {"Tangent", sm_Tangent},
249 {"FloatAbs", sm_FloatAbs},
250 {"ArcTangent", sm_ArcTangent},
251 {"ArcCosine", sm_ArcCosine},
252 {"ArcSine", sm_ArcSine},
253 {"ArcTangent2", sm_ArcTangent2},