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.0f) ? 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.0f) || (base <= 0.0f))
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_RountToNearest(IPluginContext *pCtx, const cell_t *params)
133 float val = sp_ctof(params[1]);
134 val = (float)floor(val + 0.5f);
136 return static_cast<int>(val);
139 static cell_t sm_RoundToFloor(IPluginContext *pCtx, const cell_t *params)
141 float val = sp_ctof(params[1]);
144 return static_cast<int>(val);
147 static cell_t sm_RoundToCeil(IPluginContext *pCtx, const cell_t *params)
149 float val = sp_ctof(params[1]);
152 return static_cast<int>(val);
155 static cell_t sm_RoundToZero(IPluginContext *pCtx, const cell_t *params)
157 float val = sp_ctof(params[1]);
165 return static_cast<int>(val);
168 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
170 float val = sp_ctof(params[1]);
171 val = val - floor(val);
176 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
178 float val = sp_ctof(params[1]);
184 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
186 float val = sp_ctof(params[1]);
192 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
194 float val = sp_ctof(params[1]);
200 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
202 float val = sp_ctof(params[1]);
208 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
210 float val = sp_ctof(params[1]);
216 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
218 float val = sp_ctof(params[1]);
224 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
226 float val1 = sp_ctof(params[1]);
227 float val2 = sp_ctof(params[2]);
228 val1 = atan2(val1, val2);
230 return sp_ftoc(val1);
233 REGISTER_NATIVES(floatnatives)
236 {"FloatMul", sm_FloatMul},
237 {"FloatDiv", sm_FloatDiv},
238 {"FloatAdd", sm_FloatAdd},
239 {"FloatSub", sm_FloatSub},
240 {"FloatFraction", sm_FloatFraction},
241 {"RoundToZero", sm_RoundToZero},
242 {"RoundToCeil", sm_RoundToCeil},
243 {"RoundToFloor", sm_RoundToFloor},
244 {"RountToNearest", sm_RountToNearest},
245 {"FloatCompare", sm_FloatCompare},
246 {"SquareRoot", sm_SquareRoot},
248 {"Exponential", sm_Exponential},
249 {"Logarithm", sm_Logarithm},
251 {"Cosine", sm_Cosine},
252 {"Tangent", sm_Tangent},
253 {"FloatAbs", sm_FloatAbs},
254 {"ArcTangent", sm_ArcTangent},
255 {"ArcCosine", sm_ArcCosine},
256 {"ArcSine", sm_ArcSine},
257 {"ArcTangent2", sm_ArcTangent2},