SA-MP Forums Archive
Pawn with formula calculation - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Pawn with formula calculation (/showthread.php?tid=539284)



Pawn with formula calculation - V1ceC1ty - 27.09.2014

I'm currently testing a formula to work out an incrementing value based on what number is entered but for some strange reason what is output is completely different from what should output. I'm just wondering if there is a specific way I need to format this for pawn that I'm doing wrong?

EDIT: TL; DR version, I need this PHP code converted to pawn.

I'm basing this off a php formula I found, which works 100%. Test it here if you need to http://writecodeonline.com/php/
Код:
function formula($L) {
  $a=0;
  for($x=1; $x<$L; $x++) {
    $a += floor($x+300*pow(2, ($x/7)));
  }
  return floor($a/4);
}

for($L=1;$L<100;$L++) {
  echo 'Result '.$L.': '.formula($L).'<br />';
}
This is my command for testing the formula
pawn Код:
CMD:checkformula(playerid, params[])
{
    new Float:formula, Float:result, string[128];
    for(new i = 1; i<4; i++)
    {
        formula = (i+300*(floatpower(2, i/7)));
        result = (formula/4);
        format(string, sizeof(string), "For Input: %i, Formula = %i, Result = %i", i, floatround(formula), floatround(result));
        SendClientMessage(playerid, 0xCCFF00AA, string);
    }
    return 1;
}
What outputs:
Код:
For input: 1, Formula = 301, Result = 75
For input: 2, Formula = 302, Result = 76
For input: 3, Formula = 303, Result = 76
What should output but doesn't:
Код:
For input: 1, Formula = 332, Result = 83
For input: 2, Formula = 699, Result = 174
For input: 3, Formula = 1105, Result = 276



Re: Pawn with formula calculation - thefatshizms - 27.09.2014

Correct me if I'm wrong but, isn't "^" a binary operator for "Exclusive OR" ?


Re: Pawn with formula calculation - V1ceC1ty - 27.09.2014

Quote:
Originally Posted by thefatshizms
Посмотреть сообщение
Correct me if I'm wrong but, isn't "^" a binary operator for "Exclusive OR" ?
I always considered it to the power, like 10І = 100


Re: Pawn with formula calculation - thefatshizms - 27.09.2014


From the pawn-lang.pdf


Re: Pawn with formula calculation - V1ceC1ty - 27.09.2014

Quote:
Originally Posted by thefatshizms
Посмотреть сообщение

From the pawn-lang.pdf
You're right, I should have used floatpower() but it's giving similar results as before. I've updated my first post to this format:

pawn Код:
CMD:checkformula(playerid, params[])
{
    new Float:formula, Float:result, string[128];
    for(new i = 1; i<4; i++)
    {
        formula = (i+300*(floatpower(2, i/7)));
        result = (formula/4);
        format(string, sizeof(string), "For Input: %i, Formula = %i, Result = %i", i, floatround(formula), floatround(result));
        SendClientMessage(playerid, 0xCCFF00AA, string);
    }
    return 1;
}
Код:
For input: 1, Formula = 301, Result = 75
For input: 2, Formula = 302, Result = 76
For input: 3, Formula = 303, Result = 76



Re: Pawn with formula calculation - Vince - 27.09.2014

You're raising to the power of 0? This always yield 1.
Код:
(1+300*(2 * 1))
(1+300*2)
(1+600)
601



Re: Pawn with formula calculation - V1ceC1ty - 27.09.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
You're raising to the power of 0? This always yield 1.
Yeah I just figured that out, edited it just as you posted, but it's still giving similar results


Re: Pawn with formula calculation - thefatshizms - 27.09.2014

That's verry weird... I've basically copied the exact formula and it still won't work. My try:

pawn Код:
stock formula(l)
{
    new a = 0;
    for(new x = 1; x < l; x++)
    {
        a += floatround(x+300*floatpower(2, (x/7)), floatround_floor);
    }
    return floatround(a/4, floatround_floor);
}

public OnFilterScriptInit()
{
    for(new i = 1; i < 100; i++)
    {
        printf("%i", formula(i));
    }
}



Re: Pawn with formula calculation - V1ceC1ty - 27.09.2014

Quote:
Originally Posted by thefatshizms
Посмотреть сообщение
That's verry weird... I've basically copied the exact formula and it still won't work. My try:

pawn Код:
stock formula(l)
{
    new a = 0;
    for(new x = 1; x < l; x++)
    {
        a += floatround(x+300*floatpower(2, (x/7)), floatround_floor);
    }
    return floatround(a/4, floatround_floor);
}

public OnFilterScriptInit()
{
    for(new i = 1; i < 100; i++)
    {
        printf("%i", formula(i));
    }
}
I've just changed my
pawn Код:
for(new i = 1; i<4; i++)
to
pawn Код:
for(new Float:i = 1; i<4; i++)
And this seems to work if i=1, but further than that it doesn't work. If i=2 result = 91.92 but in the PHP one it should be result = 175

I'm not the best at maths so it's probably something else I'm doing wrong.

This is what I've got now:
pawn Код:
new Float:formula, Float:result, Float:base = 300, string[128];
    for(new Float:i = 1; i<4; i++)
    {
        formula = (i+base*floatpower(2, (i/7)));
        result = (formula/4);
        format(string, sizeof(string), "For Input: %i, Formula = %i, Result = %i", floatround(i), floatround(formula), floatround(result));
        SendClientMessage(playerid, 0xCCFF00AA, string);
    }



Re: Pawn with formula calculation - thefatshizms - 27.09.2014

Managed to do it, here:
pawn Код:
stock formula(l)
{
    new a = 0;
    for(new Float:x = 1; x < l; x++)
    {
        a += floatround(x+300*floatpower(2, (x/7)), floatround_floor);
    }
    return floatround(a/4, floatround_floor);
}

public OnFilterScriptInit()
{
    for(new i = 1; i < 10; i++)
    {
        printf("%i", formula(i));
    }
}