SA-MP Forums Archive
Floats confusion - 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: Floats confusion (/showthread.php?tid=378305)



Floats confusion - cosbraa - 17.09.2012

So I changed a calculator command I had, which only worked with integers, to work with float numbers.
It all "worked" fine except, for example, I type:

/calculate 1.4 1.4 add
it sends the messages:
1.39999 + 1.39999 = 2.79999

Why does it change the 1.4 to 1.3999?

Cheers,
haven't worked with floats too much.



Re: Floats confusion - clarencecuzz - 17.09.2012

Can you show the code for the calculator command?


Re: Floats confusion - cosbraa - 17.09.2012

pawn Код:
CMD:calculate(playerid, params[])
{
    new Float:Value1, Method[20], Float:Value2, string[128];
    if(sscanf(params, "ffs", Value1, Value2, Method))
    {
        SendClientMessage(playerid, GREY, "SYNTAX: /calc(ulate) [value] [value] [method]");
        SendClientMessage(playerid, GREY, "Method: Add, Subtract, Multiply & Divide.");
    }
    else
    {
        if(strcmp(Method, "add", true) == 0)
        {
            new Float:Sum = Value1 + Value2;
            format(string, sizeof(string), "%f + %f = %f.", Value1, Value2, Sum);
            SendClientMessage(playerid, WHITE, string);
        }
    }
    return 1;
}



Re: Floats confusion - clarencecuzz - 17.09.2012

Try this:
pawn Код:
format(string, sizeof(string), "%0.2f + %0.2f = %0.2f.", Value1, Value2, Sum);



Re: Floats confusion - cosbraa - 17.09.2012

Didn't fix it.
Now it says:
1.39 + 1.39 = 2.79.


Re: Floats confusion - Roko_foko - 17.09.2012

I don't know will this help but try: https://sampwiki.blast.hk/wiki/Floatadd


Re: Floats confusion - [ABK]Antonio - 17.09.2012

It's a limitation in the system - it does it with floats a lot, there's a thread around here explainin it somewhere... (i know from workin with floats)