2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
17 #include "sm_globals.h"
19 /****************************************
21 * FLOATING POINT NATIVE IMPLEMENTATIONS *
23 ****************************************/
26 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
28 float val = static_cast<float>(params[1]);
33 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
35 float val = sp_ctof(params[1]);
36 val = (val >= 0) ? val : -val;
41 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
43 float val = sp_ctof(params[1]) + sp_ctof(params[2]);
48 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
50 float val = sp_ctof(params[1]) - sp_ctof(params[2]);
55 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
57 float val = sp_ctof(params[1]) * sp_ctof(params[2]);
62 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
64 float val = sp_ctof(params[1]) / sp_ctof(params[2]);
69 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
71 float val1 = sp_ctof(params[1]);
72 float val2 = sp_ctof(params[2]);
77 } else if (val1 < val2) {
84 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
86 float val = sp_ctof(params[1]);
87 float base = sp_ctof(params[2]);
89 if ((val <= 0) || (base <= 0))
91 return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
97 val = log(val) / log(base);
103 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
105 float val = sp_ctof(params[1]);
107 return sp_ftoc(exp(val));
110 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
112 float base = sp_ctof(params[1]);
113 float exponent = sp_ctof(params[2]);
115 return sp_ftoc(pow(base, exponent));
118 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
120 float val = sp_ctof(params[1]);
124 return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
127 return sp_ftoc(sqrt(val));
130 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
132 float val = sp_ctof(params[1]);
158 val = (float)floor(val + 0.5);
163 return static_cast<int>(val);
166 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
168 float val = sp_ctof(params[1]);
169 val = val - floor(val);
174 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
176 float val = sp_ctof(params[1]);
182 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
184 float val = sp_ctof(params[1]);
190 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
192 float val = sp_ctof(params[1]);
198 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
200 float val = sp_ctof(params[1]);
206 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
208 float val = sp_ctof(params[1]);
214 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
216 float val = sp_ctof(params[1]);
222 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
224 float val1 = sp_ctof(params[1]);
225 float val2 = sp_ctof(params[2]);
226 val1 = atan2(val1, val2);
228 return sp_ftoc(val1);
231 REGISTER_NATIVES(floatnatives)
234 {"FloatMul", sm_FloatMul},
235 {"FloatDiv", sm_FloatDiv},
236 {"FloatAdd", sm_FloatAdd},
237 {"FloatSub", sm_FloatSub},
238 {"FloatFraction", sm_FloatFraction},
239 {"FloatRound", sm_FloatRound},
240 {"FloatCompare", sm_FloatCompare},
241 {"SquareRoot", sm_SquareRoot},
243 {"Exponential", sm_Exponential},
244 {"Logarithm", sm_Logarithm},
246 {"Cosine", sm_Cosine},
247 {"Tangent", sm_Tangent},
248 {"FloatAbs", sm_FloatAbs},
249 {"ArcTangent", sm_ArcTangent},
250 {"ArcCosine", sm_ArcCosine},
251 {"ArcSine", sm_ArcSine},
252 {"ArcTangent2", sm_ArcTangent2},