4 #include "sp_typeutil.h"
6 using namespace SourcePawn;
8 //:TODO: these need to be registered....
10 /****************************************
12 * FLOATING POINT NATIVE IMPLEMENTATIONS *
14 ****************************************/
17 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
19 float val = static_cast<float>(params[1]);
24 static cell_t sm_floatabs(IPluginContext *pCtx, const cell_t *params)
26 float val = sp_ctof(params[1]);
27 val = (val >= 0) ? val : -val;
32 static cell_t sm_floatadd(IPluginContext *pCtx, const cell_t *params)
34 float val = sp_ctof(params[1]) + sp_ctof(params[2]);
39 static cell_t sm_floatsub(IPluginContext *pCtx, const cell_t *params)
41 float val = sp_ctof(params[1]) - sp_ctof(params[2]);
46 static cell_t sm_floatmul(IPluginContext *pCtx, const cell_t *params)
48 float val = sp_ctof(params[1]) * sp_ctof(params[2]);
53 static cell_t sm_floatdiv(IPluginContext *pCtx, const cell_t *params)
55 float val = sp_ctof(params[1]) / sp_ctof(params[2]);
60 static cell_t sm_floatcmp(IPluginContext *pCtx, const cell_t *params)
62 float val1 = sp_ctof(params[1]);
63 float val2 = sp_ctof(params[2]);
68 } else if (val1 < val2) {
75 static cell_t sm_floatlog(IPluginContext *pCtx, const cell_t *params)
77 float val = sp_ctof(params[1]);
78 float base = sp_ctof(params[2]);
80 if ((val <= 0) || (base <= 0))
82 //:TODO: error out! logs cant take in negative numbers and log 0=-inf
88 val = log(val) / log(base);
94 static cell_t sm_floatexp(IPluginContext *pCtx, const cell_t *params)
96 float val = sp_ctof(params[1]);
98 return sp_ftoc(exp(val));
101 static cell_t sm_floatpower(IPluginContext *pCtx, const cell_t *params)
103 float base = sp_ctof(params[1]);
104 float exponent = sp_ctof(params[2]);
106 return sp_ftoc(pow(base, exponent));
109 static cell_t sm_floatsqroot(IPluginContext *pCtx, const cell_t *params)
111 float val = sp_ctof(params[1]);
115 //:TODO: error out! we dont support complex numbers
118 return sp_ftoc(sqrt(val));
121 static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params)
123 float val = sp_ctof(params[1]);
149 val = (float)floor(val + 0.5);
154 return static_cast<int>(val);
157 static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params)
161 pCtx->LocalToString(params[1], &str);
167 return sp_ftoc((float)atof(str));
170 static cell_t sm_floatfract(IPluginContext *pCtx, const cell_t *params)
172 float val = sp_ctof(params[1]);
173 val = val - floor(val);
178 static cell_t sm_floatsin(IPluginContext *pCtx, const cell_t *params)
180 float val = sp_ctof(params[1]);
186 static cell_t sm_floatcos(IPluginContext *pCtx, const cell_t *params)
188 float val = sp_ctof(params[1]);
194 static cell_t sm_floattan(IPluginContext *pCtx, const cell_t *params)
196 float val = sp_ctof(params[1]);
202 static cell_t sm_floatasin(IPluginContext *pCtx, const cell_t *params)
204 float val = sp_ctof(params[1]);
210 static cell_t sm_floatacos(IPluginContext *pCtx, const cell_t *params)
212 float val = sp_ctof(params[1]);
218 static cell_t sm_floatatan(IPluginContext *pCtx, const cell_t *params)
220 float val = sp_ctof(params[1]);
226 static cell_t sm_floatatan2(IPluginContext *pCtx, const cell_t *params)
228 float val1 = sp_ctof(params[1]);
229 float val2 = sp_ctof(params[2]);
230 val1 = atan2(val1, val2);
232 return sp_ftoc(val1);