Raised This Month: $ Target: $400
 0% 

float problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gamer99
Senior Member
Join Date: Jul 2011
Old 09-24-2011 , 00:45   float problem
Reply With Quote #1

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.
__________________
Thanks and regards,
Gamer99
gamer99 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-24-2011 , 01:20   Re: float problem
Reply With Quote #2

Show the code.
__________________
fysiks is offline
gamer99
Senior Member
Join Date: Jul 2011
Old 09-24-2011 , 01:33   Re: float problem
Reply With Quote #3

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)
					}	
				}
			}
		}
	}
}
__________________
Thanks and regards,
Gamer99
gamer99 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-24-2011 , 03:27   Re: float problem
Reply With Quote #4

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.
__________________
fysiks is offline
gamer99
Senior Member
Join Date: Jul 2011
Old 09-24-2011 , 05:09   Re: float problem
Reply With Quote #5

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.
__________________
Thanks and regards,
Gamer99
gamer99 is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 09-24-2011 , 06:31   Re: float problem
Reply With Quote #6

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
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
gamer99
Senior Member
Join Date: Jul 2011
Old 09-24-2011 , 06:44   Re: float problem
Reply With Quote #7



Can you tell me how to use it?
Just a simple example?
__________________
Thanks and regards,
Gamer99
gamer99 is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 09-24-2011 , 06:47   Re: float problem
Reply With Quote #8

that's not the deal about how you use it. you should google float point precision
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
gamer99
Senior Member
Join Date: Jul 2011
Old 09-24-2011 , 08:54   Re: float problem
Reply With Quote #9

Quote:
Originally Posted by jim_yang View Post
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.
__________________
Thanks and regards,
Gamer99
gamer99 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-24-2011 , 09:15   Re: float problem
Reply With Quote #10

How much precision do you need?
__________________
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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