Skip to content

Commit 5539484

Browse files
authored
Add support for float modulo operator (alliedmodders#1953)
1 parent a9a1939 commit 5539484

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

core/logic/smn_float.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
231231
return sp_ftoc(val);
232232
}
233233

234+
static cell_t sm_FloatMod(IPluginContext* pCtx, const cell_t* params)
235+
{
236+
float val = fmodf(sp_ctof(params[1]), sp_ctof(params[2]));
237+
238+
return sp_ftoc(val);
239+
}
240+
234241
static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
235242
{
236243
float val = sp_ctof(params[1]);
@@ -358,6 +365,7 @@ REGISTER_NATIVES(floatnatives)
358365
{"FloatAdd", sm_FloatAdd},
359366
{"FloatSub", sm_FloatSub},
360367
{"FloatFraction", sm_FloatFraction},
368+
{"FloatMod", sm_FloatMod},
361369
{"RoundToZero", sm_RoundToZero},
362370
{"RoundToCeil", sm_RoundToCeil},
363371
{"RoundToFloor", sm_RoundToFloor},

plugins/include/float.inc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ native float FloatAdd(float oper1, float oper2);
9797
#pragma deprecated This native is internal implementation. For subtraction use the '-' operator.
9898
native float FloatSub(float oper1, float oper2);
9999
100+
/**
101+
* Returns the modulus of oper1 and oper2.
102+
*
103+
* Note: This native is internal implementation. For modulo use the '%' operator.
104+
*
105+
* @param oper1 First value.
106+
* @param oper2 Second value.
107+
* @return oper1%oper2.
108+
* @deprecated This native is internal implementation. For modulo use the '%' operator.
109+
*/
110+
#pragma deprecated This native is internal implementation. For modulo use the '%' operator.
111+
native float FloatMod(float oper1, float oper2);
112+
100113
/**
101114
* Returns the decimal part of a float.
102115
*
@@ -269,6 +282,7 @@ native float __FLOAT_MUL__(float a, float b) = FloatMul;
269282
native float __FLOAT_DIV__(float a, float b) = FloatDiv;
270283
native float __FLOAT_ADD__(float a, float b) = FloatAdd;
271284
native float __FLOAT_SUB__(float a, float b) = FloatSub;
285+
native float __FLOAT_MOD__(float a, float b) = FloatMod;
272286
273287
native bool __FLOAT_GT__(float a, float b);
274288
native bool __FLOAT_GE__(float a, float b);
@@ -282,6 +296,7 @@ native float operator*(float oper1, float oper2) = FloatMul;
282296
native float operator/(float oper1, float oper2) = FloatDiv;
283297
native float operator+(float oper1, float oper2) = FloatAdd;
284298
native float operator-(float oper1, float oper2) = FloatSub;
299+
native float operator%(float oper1, float oper2) = FloatMod;
285300
native bool operator!(float oper1) = __FLOAT_NOT__;
286301
native bool operator>(float oper1, float oper2) = __FLOAT_GT__;
287302
native bool operator>=(float oper1, float oper2) = __FLOAT_GE__;
@@ -387,12 +402,15 @@ stock bool operator<=(int oper1, float oper2)
387402
return __FLOAT_LE__(float(oper1), oper2);
388403
}
389404
390-
/**
391-
* Forbidden operators.
392-
*/
393-
forward float operator%(float oper1, float oper2);
394-
forward float operator%(float oper1, int oper2);
395-
forward float operator%(int oper1, float oper2);
405+
stock float operator%(float oper1, int oper2)
406+
{
407+
return __FLOAT_MOD__(oper1, float(oper2));
408+
}
409+
410+
stock float operator%(int oper1, float oper2)
411+
{
412+
return __FLOAT_MOD__(float(oper1), oper2);
413+
}
396414
#endif // __sourcepawn2__
397415
398416
#define FLOAT_PI 3.1415926535897932384626433832795

0 commit comments

Comments
 (0)