Raised This Month: $ Target: $400
 0% 

get_pcvar_num returning different number?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 10-27-2011 , 10:38   Re: get_pcvar_num returning different number?
Reply With Quote #3

I've searched/debugged a bit and the problem is most likely a loss of precision double -> float. Let me explain.

The bug is not from AMXX, all the native does is returning (int)ptr->value, so the float value converted to an integer.
This float value is set when you register a cvar, in the engine, using Q_atof(). This function itself works fine. The functions looks like :

PHP Code:
float Q_atof (char *str)
{
    
double    val;
    
int        sign;
    
int        c;
    
int        decimaltotal;
    
    if (*
str == '-')
    {
        
sign = -1;
        
str++;
    }
    else
        
sign 1;
        
    
val 0;

//
// check for hex
//
    
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
    {
        
str += 2;
        while (
1)
        {
            
= *str++;
            if (
>= '0' && <= '9')
                
val = (val*16) + '0';
            else if (
>= 'a' && <= 'f')
                
val = (val*16) + 'a' 10;
            else if (
>= 'A' && <= 'F')
                
val = (val*16) + 'A' 10;
            else
                return 
val*sign;
        }
    }
    
//
// check for character
//
    
if (str[0] == '\'')
    {
        return 
sign str[1];
    }
    
//
// assume decimal
//
    
decimal = -1;
    
total 0;
    while (
1)
    {
        
= *str++;
        if (
== '.')
        {
            
decimal total;
            continue;
        }
        if (
<'0' || '9')
            break;
        
val val*10 '0';
        
total++;
    }

    if (
decimal == -1)
        return 
val*sign;
    while (
total decimal)
    {
        
val /= 10;
        
total--;
    }
    
    return 
val*sign;

The problem is it returns a float, when you're working with double. The value is right when it's about to be returned, but it's lost precision once it has been converted to float.
I don't know much why and how such things are handled but the problem is that.

Since from the engine, you can't fix directly. It's possible to fix amxmodx by overwriting this value using atof() from the string, in register_cvar native, it will work.

Another way is to use a common way where you use the letters as flags then you could use read_flags() to get the bits sum from the letters.
I don't remember seeing a plugin dealing with such big number in a cvar.
__________________

Last edited by Arkshine; 10-27-2011 at 10:44.
Arkshine is offline
 



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 14:22.


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