Pawn with formula calculation
#1

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

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

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


From the pawn-lang.pdf
Reply
#5

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

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

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

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));
    }
}
Reply
#9

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);
    }
Reply
#10

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));
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)