Alternative for '%' in PAWN
#1

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
Reply
#2

The modulus operator actually just works like you describe. Must be some other problem.
Reply
#3

Код:
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.
Reply
#4

Re-download the SA:MP includes.
Reply
#5

Ye, I just tested the modulus operator with float values and it seems to work fine. It compiles, at least.
Reply
#6

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.

pawn Код:
new Float:mem;
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?
Reply
#7

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.
Reply
#8

I haven't. Why are they 'forbidden'?
Reply
#9

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.
Reply
#10

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.
Reply
#11

pawn Код:
new Float:aqdjfkm = 150.45 % 35.6;
That's what I used to test with. Compiles fine.
Reply
#12

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?
Reply
#13

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
Your theory is good, but try it out
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)