AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   float problem (https://forums.alliedmods.net/showthread.php?t=168078)

gamer99 09-24-2011 00:45

float problem
 
Hi ,

In one float variable I have the value as

0.02

But when I am trying to use that value it is automatically changing to

0.021999 .

Because of it I am having problem. How to solve it? Please help.

fysiks 09-24-2011 01:20

Re: float problem
 
Show the code.

gamer99 09-24-2011 01:33

Re: float problem
 
here you go

Code:

public cvar_results(id,const cvar[],const value[])
{
       
        for(new i;i<dSize;i++)
        {
                ArrayGetString(g_sCvars,i,sBuffer,255)
                parse(sBuffer,sCvar,63,sMin,63,sMiddle,63,sMax,63)
               
                dMin=str_to_float(sMin)
                dMax=str_to_float(sMax)
                fMiddle=str_to_float(sMiddle)
               
                if(equal(sCvar,cvar))
                {
                        dValue=str_to_float(value)
                        if(dValue<dMin||dValue>dMax)
                        {
                                if(g_dPlayerStatus[id]==true)
                                {
                                        //ban cvars
                                        g_dCvarsWarning[id]++
                                        format(sBanReason,63,"%s %0.2f",cvar,dValue)
                                        format(sCvarWarning,sizeof(sCvarWarning)-1,"%L",LANG_SERVER,"CVAR_WARNING",sBanReason,g_dCvarsWarning[id],iCvarWarning,cvar,fMiddle)
                                        format(s_Warning,sizeof(s_Warning)-1,"%s %L",sCvarWarning,LANG_SERVER,"LAST_WARNING",sCvarWarning,g_dCvarsWarning[id],iCvarWarning)
                                        //server_print("Illegal CVAR %s",sBanReason)
                                        if(g_dCvarsWarning[id] > iCvarWarning)
                                        {
                                                //server_print("Punish player")
                                                ban(id,iCvars,sBanReason ,iCvarBantime)
                                        }
                                        else
                                        {
                                                if(g_dCvarsWarning[id] < iCvarWarning)
                                                {
                                                        warning(id,sCvarWarning)
                                                }
                                                else
                                                {
                                                        warning(id,s_Warning)
                                                }
                                                //server_print("CVAR value set to %s %f",cvar,fMiddle)
                                        }       
                                }
                        }
                }
        }
}


fysiks 09-24-2011 03:27

Re: float problem
 
And how am I supposed to know which of the four variables is in question and where you are seeing the incorrect value?

You need to explain these things, we can't read your mind.

gamer99 09-24-2011 05:09

Re: float problem
 
ArrayGetString(g_sCvars,i,sBuffer,255)

This is like m_yaw 0.022 0.022 0.022

then

in the variables

dMin=str_to_float(sMin)
dMax=str_to_float(sMax)
fMiddle=str_to_float(sMiddle)

I am getting like 0.219999 instead of 2.2, Because of it my checking is getting failed.

jim_yang 09-24-2011 06:31

Re: float problem
 
Code:


static cell AMX_NATIVE_CALL str_to_float(AMX *amx, cell *params)
{
        cell *str = get_amxaddr(amx, params[1]);

        bool neg = false;
        unsigned long part1 = 0;

        if (*str == '-')
        {
                neg = true;
                ++str;
        }
        else if (*str == '+')
                ++str;

        while (*str)
        {
                if (*str == '.')
                {
                        ++str;
                        break;
                }
               
                if (*str < '0' || *str > '9')
                {
                        REAL fl = neg ? -static_cast<REAL>(part1) : static_cast<REAL>(part1);
                        return amx_ftoc(fl);
                }

                part1 *= 10;
                part1 += *str - '0';

                ++str;
        }

        unsigned long part2 = 0;
        unsigned long div = 1;
       
        while (*str)
        {
                if (*str < '0' || *str > '9')
                        break;

                part2 *= 10;
                part2 += *str - '0';
                div *= 10;
                ++str;
        }

        REAL fl = static_cast<REAL>(part1) + (static_cast<REAL>(part2) / div);
       
        if (neg)
                fl = -fl;
       
        return amx_ftoc(fl);
}

hope this helps, try to figure it out why by yourself or wait for someone's explanation

gamer99 09-24-2011 06:44

Re: float problem
 
:(

Can you tell me how to use it?
Just a simple example?

jim_yang 09-24-2011 06:47

Re: float problem
 
that's not the deal about how you use it. you should google float point precision

gamer99 09-24-2011 08:54

Re: float problem
 
Quote:

Originally Posted by jim_yang (Post 1562059)
that's not the deal about how you use it. you should google float point precision

Actually the problem is as it is a dynamic process some other value may be 0.1 instead of 0.022 or any value.

So I don't know actually how to get exact float by parsing the string.

Bugsy 09-24-2011 09:15

Re: float problem
 
How much precision do you need?


All times are GMT -4. The time now is 19:37.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.