Raised This Month: $ Target: $400
 0% 

Efficient way to cut decimals from a float


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-10-2009 , 02:24   Efficient way to cut decimals from a float
Reply With Quote #1

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
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 06-10-2009 at 02:33.
ConnorMcLeod is offline
Old 06-10-2009, 03:06
xPaw
This message has been deleted by xPaw. Reason: bad
jim_yang
Veteran Member
Join Date: Aug 2006
Old 06-10-2009 , 08:18   Re: Efficient way to cut decimals from a float
Reply With Quote #2

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.
__________________
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
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-10-2009 , 11:16   Re: Efficient way to cut decimals from a float
Reply With Quote #3

Quote:
Originally Posted by xPaw View Post
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 View Post
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 !!
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 05-29-2010 , 07:20   Re: Efficient way to cut decimals from a float
Reply With Quote #4

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)
    }

__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 05-29-2010 at 07:26.
ConnorMcLeod is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 05-29-2010 , 08:45   Re: Efficient way to cut decimals from a float
Reply With Quote #5

Multiply the float argument by 10, 100, 1000 etc. before using the formula and divide the result by the same value afterwards.
__________________
Impossible is Nothing
Sylwester is offline
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 05-29-2010 , 11:25   Re: Efficient way to cut decimals from a float
Reply With Quote #6

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...
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.

Last edited by joropito; 05-29-2010 at 11:43.
joropito is offline
Send a message via MSN to joropito
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 05-29-2010 , 11:38   Re: Efficient way to cut decimals from a float
Reply With Quote #7

it won't work
__________________
Impossible is Nothing
Sylwester is offline
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 05-29-2010 , 11:43   Re: Efficient way to cut decimals from a float
Reply With Quote #8

Quote:
Originally Posted by Sylwester View Post
it won't work
Try now, I've changed + by -
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 05-29-2010 , 11:46   Re: Efficient way to cut decimals from a float
Reply With Quote #9

It doesn't matter if you use + or -. It still will not work.
__________________
Impossible is Nothing
Sylwester is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 05-29-2010 , 11:47   Re: Efficient way to cut decimals from a float
Reply With Quote #10

right, that method works only at some special range. so don't try it any more
__________________
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
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 13:50.


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