Alternative for '%' in PAWN -
Jochemd - 01.06.2012
Hello,
If you're a C# scripter you must be familliar with the '%' function (3600 % 1500 = 600). I want to do such thing in PAWN but I noticed it's not possible to use this function in PAWN as it does not support it. I was thinking about an alternative way but it keeps crossing my mind.
Could you help me thinking a bit?
Jochem
Re: Alternative for '%' in PAWN -
Vince - 01.06.2012
The modulus operator actually just works like you describe. Must be some other problem.
Re: Alternative for '%' in PAWN -
Jochemd - 01.06.2012
Код:
error 004: function "operator%(Float:,_:)" is not implemented
I wonder why it shows this then
Oh, apparently it doesn't work with floats! Though I need a way to do it with floats.
Re: Alternative for '%' in PAWN -
MP2 - 01.06.2012
Re-download the SA:MP includes.
Re: Alternative for '%' in PAWN -
Vince - 01.06.2012
Ye, I just tested the modulus operator with float values and it seems to work fine. It compiles, at least.
Re: Alternative for '%' in PAWN -
Jochemd - 01.06.2012
Quote:
Originally Posted by MP2
Re-download the SA:MP includes.
|
I'm quite sure the SA-MP includes are not a problem. Without the 'Float:' tag it works fine.
This would give error 4.
Quote:
Originally Posted by Vince
Ye, I just tested the modulus operator with float values and it seems to work fine. It compiles, at least.
|
Could you show me how you did it?
Re: Alternative for '%' in PAWN -
MP2 - 01.06.2012
EDIT: Disregard the earlier post.
I also get this error when using two floats:
Код:
C:\Program Files (x86)\Rockstar Games\GTA San Andreas\filterscripts\mod.pwn(10) : error 004: function "operator%(Float:,Float:)" is not implemented
Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase
1 Error.
Re: Alternative for '%' in PAWN -
Jochemd - 01.06.2012
I haven't. Why are they 'forbidden'?
Re: Alternative for '%' in PAWN -
MP2 - 01.06.2012
It works if you remove those from float.inc, I'm not sure why they are forbidden.
Running this code outputs a float of 0.0:
pawn Код:
#include <a_samp>
public OnFilterScriptInit()
{
new Float:fvar = 123.0;
new Float:fmod = fvar % 60.0;
printf("%f", fmod);
return 1;
}
So I guess it just isn't supported.
Re: Alternative for '%' in PAWN -
MadeMan - 01.06.2012
Alternative functions:
pawn Код:
stock Float:modulus(Float:a, Float:b)
{
while(a > b)
a -= b;
return a;
}
pawn Код:
stock Float:modulus(Float:a, Float:b)
{
return (((a/b)-floatround(a/b, floatround_floor))*b);
}
The second one is not very accurate.
Re: Alternative for '%' in PAWN -
Vince - 01.06.2012
pawn Код:
new Float:aqdjfkm = 150.45 % 35.6;
That's what I used to test with. Compiles fine.
Re: Alternative for '%' in PAWN -
Babul - 01.06.2012
the modulo operator returns the remainder of 2 integers. for floats, you simply need to reinvent the formula:
Код:
new Float:a=10.0;
new Float:b=4.0;
new result=floatround((a/b),floatround_floor)*b;
10.0 / 4.0 = 2.5
round_floor of 2.5 = 0.5
0.5 * 4.0 = 2.0
...looks similar to
10 % 4 = 2
another test: 100.0 % 3.0
100.0/3.0=33.333333
0.333333*3.0=1.000000
100%33=1
oh, i forgot to mention
3600.0/1500.0=2.4
0.4*1500=600
3600.0%1500.0=600
oops i made a mistake there. the floor is NOT the method to round, it takes the main part instead of the small parts... moment. editing post (again) lol
here is the solution: the floatround_floor returns the big part %f.0, subtracted that from the whole number %f, will return the little %.f part:
Код:
new result=((a/b)-floatround((a/b),floatround_floor))*b;
3600.0 / 1500.0 = 2.4
floatround of 2.4 = 2.0 // i forgot this to create the small number...
2.4 - 2.0 = 0.4 //incl. this line..
0.4 * 1500.0 = 600.0
did i miss something again?
Re: Alternative for '%' in PAWN -
MadeMan - 01.06.2012
Quote:
Originally Posted by Babul
the modulo operator returns the remainder of 2 integers. for floats, you simply need to reinvent the formula:
Код:
new Float:a=10.0;
new Float:b=4.0;
new result=floatround((a/b),floatround_floor)*b;
10.0 / 4.0 = 2.5
round_floor of 2.5 = 0.5
0.5 * 4.0 = 2.0
...looks similar to
10 % 4 = 2
another test: 100.0 % 3.0
100.0/3.0=33.333333
0.333333*3.0=1.000000
100%33=1 ![Smiley](images/smilies/smile.png)
|
Your theory is good, but try it out