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
15 #include "sm_globals.h"
17 /****************************************
19 * FLOATING POINT NATIVE IMPLEMENTATIONS *
21 ****************************************/
24 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
26 float val = static_cast<float>(params[1]);
31 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
33 float val = sp_ctof(params[1]);
34 val = (val >= 0) ? val : -val;
39 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
41 float val = sp_ctof(params[1]) + sp_ctof(params[2]);
46 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
48 float val = sp_ctof(params[1]) - sp_ctof(params[2]);
53 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
55 float val = sp_ctof(params[1]) * sp_ctof(params[2]);
60 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
62 float val = sp_ctof(params[1]) / sp_ctof(params[2]);
67 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
69 float val1 = sp_ctof(params[1]);
70 float val2 = sp_ctof(params[2]);
75 } else if (val1 < val2) {
82 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
84 float val = sp_ctof(params[1]);
85 float base = sp_ctof(params[2]);
87 if ((val <= 0) || (base <= 0))
89 return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f).", val, base);
95 val = log(val) / log(base);
101 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
103 float val = sp_ctof(params[1]);
105 return sp_ftoc(exp(val));
108 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
110 float base = sp_ctof(params[1]);
111 float exponent = sp_ctof(params[2]);
113 return sp_ftoc(pow(base, exponent));
116 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
118 float val = sp_ctof(params[1]);
122 return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f).", val);
125 return sp_ftoc(sqrt(val));
128 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
130 float val = sp_ctof(params[1]);
156 val = (float)floor(val + 0.5);
161 return static_cast<int>(val);
164 static cell_t sm_FloatStr(IPluginContext *pCtx, const cell_t *params)
168 pCtx->LocalToString(params[1], &str);
174 return sp_ftoc((float)atof(str));
177 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
179 float val = sp_ctof(params[1]);
180 val = val - floor(val);
185 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
187 float val = sp_ctof(params[1]);
193 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
195 float val = sp_ctof(params[1]);
201 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
203 float val = sp_ctof(params[1]);
209 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
211 float val = sp_ctof(params[1]);
217 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
219 float val = sp_ctof(params[1]);
225 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
227 float val = sp_ctof(params[1]);
233 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
235 float val1 = sp_ctof(params[1]);
236 float val2 = sp_ctof(params[2]);
237 val1 = atan2(val1, val2);
239 return sp_ftoc(val1);
242 REGISTER_NATIVES(floatnatives)
245 {"FloatStr", sm_FloatStr},
246 {"FloatMul", sm_FloatMul},
247 {"FloatDiv", sm_FloatDiv},
248 {"FloatAdd", sm_FloatAdd},
249 {"FloatSub", sm_FloatSub},
250 {"FloatFraction", sm_FloatFraction},
251 {"FloatRound", sm_FloatRound},
252 {"FloatCompare", sm_FloatCompare},
253 {"SquareRoot", sm_SquareRoot},
255 {"Exponential", sm_Exponential},
256 {"Logarithm", sm_Logarithm},
258 {"Cosine", sm_Cosine},
259 {"Tangent", sm_Tangent},
260 {"FloatAbs", sm_FloatAbs},
261 {"ArcTangent", sm_ArcTangent},
262 {"ArcCosine", sm_ArcCosine},
263 {"ArcSine", sm_ArcSine},
264 {"ArcTangent2", sm_ArcTangent2},