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_FloatStr(IPluginContext *pCtx, const cell_t *params)
170 pCtx->LocalToString(params[1], &str);
176 return sp_ftoc((float)atof(str));
179 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
181 float val = sp_ctof(params[1]);
182 val = val - floor(val);
187 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
189 float val = sp_ctof(params[1]);
195 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
197 float val = sp_ctof(params[1]);
203 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
205 float val = sp_ctof(params[1]);
211 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
213 float val = sp_ctof(params[1]);
219 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
221 float val = sp_ctof(params[1]);
227 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
229 float val = sp_ctof(params[1]);
235 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
237 float val1 = sp_ctof(params[1]);
238 float val2 = sp_ctof(params[2]);
239 val1 = atan2(val1, val2);
241 return sp_ftoc(val1);
244 REGISTER_NATIVES(floatnatives)
247 {"FloatStr", sm_FloatStr},
248 {"FloatMul", sm_FloatMul},
249 {"FloatDiv", sm_FloatDiv},
250 {"FloatAdd", sm_FloatAdd},
251 {"FloatSub", sm_FloatSub},
252 {"FloatFraction", sm_FloatFraction},
253 {"FloatRound", sm_FloatRound},
254 {"FloatCompare", sm_FloatCompare},
255 {"SquareRoot", sm_SquareRoot},
257 {"Exponential", sm_Exponential},
258 {"Logarithm", sm_Logarithm},
260 {"Cosine", sm_Cosine},
261 {"Tangent", sm_Tangent},
262 {"FloatAbs", sm_FloatAbs},
263 {"ArcTangent", sm_ArcTangent},
264 {"ArcCosine", sm_ArcCosine},
265 {"ArcSine", sm_ArcSine},
266 {"ArcTangent2", sm_ArcTangent2},