AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Efficient way to cut decimals from a float (https://forums.alliedmods.net/showthread.php?t=94335)

ConnorMcLeod 06-10-2009 02:24

Efficient way to cut decimals from a float
 
Hello, is there a way to cut decimals from a float ?

Basically, float( floatround( fValue ) ) just sucks.


PHP Code:

    entity_get_vector(idEV_VEC_velocityvecVel)
    
floatsqroot(vecVel[0]*vecVel[0] + vecVel[1]*vecVel[1]) 

I want the last value to be XXX.0

jim_yang 06-10-2009 08:18

Re: Efficient way to cut decimals from a float
 
Code:

public test(id)
{
    console_print(id, "%f", float_round(123.123456))
    return PLUGIN_HANDLED
}
Float:float_round(Float:f)
{
    new a = _:f;
    new e = 150 - ((a>>23) & 0xFF);
    a >>= e;
    a <<= e;
    return Float:a;
}

Don't know if this works for all cases.

ConnorMcLeod 06-10-2009 11:16

Re: Efficient way to cut decimals from a float
 
Quote:

Originally Posted by xPaw (Post 845356)
Another epic way, if even it will work
PHP Code:

new Float:fixdszFloat];

formatszFloatcharsmaxszFloat ), "%.0f"vecVel );
fixd str_to_floatszFloat ); 


I don't want to print.


Quote:

Originally Posted by jim_yang (Post 845512)
Code:

public test(id)
{
    console_print(id, "%f", float_round(123.123456))
    return PLUGIN_HANDLED
}
Float:float_round(Float:f)
{
    new a = _:f;
    new e = 150 - ((a>>23) & 0xFF);
    a >>= e;
    a <<= e;
    return Float:a;
}

Don't know if this works for all cases.

Wow, thanks !!

ConnorMcLeod 05-29-2010 07:20

Re: Efficient way to cut decimals from a float
 
Any way to cut only last decimals ?

I think something like following code would work, but i want something like previous formula.

PHP Code:

Float:CutDecimals(&flFloatiDecimals)
{
    if( 
<= iDecimals <= )
    {
        new 
szFloat[32], szFormat[5] = "%.0f"
        
szFormat[2] += iDecimals
        formatex
(szFloatcharsmax(szFloat), szFormatflFloat)
        
flFloat str_to_float(szFloat)
    }



Sylwester 05-29-2010 08:45

Re: Efficient way to cut decimals from a float
 
Multiply the float argument by 10, 100, 1000 etc. before using the formula and divide the result by the same value afterwards.

joropito 05-29-2010 11:25

Re: Efficient way to cut decimals from a float
 
This should work (not tested).

Be careful with decimals value...

PHP Code:

Float:float_round(Float:fdecimals 0)
{
    new 
_:f
    
new 150 - ((a>>23) & 0xFF)
    
>>= (- (decimals 7FFFFF))
    
<<= (- (decimals 7FFFFF))
    return 
Float:a


EDIT: Updated to add decimals value overflow control. Still untested...

Sylwester 05-29-2010 11:38

Re: Efficient way to cut decimals from a float
 
it won't work

joropito 05-29-2010 11:43

Re: Efficient way to cut decimals from a float
 
Quote:

Originally Posted by Sylwester (Post 1194383)
it won't work

Try now, I've changed + by -

Sylwester 05-29-2010 11:46

Re: Efficient way to cut decimals from a float
 
It doesn't matter if you use + or -. It still will not work.

jim_yang 05-29-2010 11:47

Re: Efficient way to cut decimals from a float
 
right, that method works only at some special range. so don't try it any more


All times are GMT -4. The time now is 13:50.

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