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=156481)

reinert 05-07-2011 06:46

Float problem
 
Hey, Why do I get round float ?

I've code:

PHP Code:

new 6436
new 1544

get_float_ab
()
{
    return 
float:( a/1.0 );


Then I'm printing it as a message:
PHP Code:

client_print(0print_chat"a / b = %0.1f"get_float_ab() ) 

And it prints:

a / b = 4.0

As the result should be 6436 / 1544 = 4.1 ( 4.168... )

Hunter-Digital 05-07-2011 07:06

Re: Float problem
 
Your stock needs a float tag and its case sensitive, so it MUST be "Float".

EDIT: this seems to work, returns 4.168393
Code:

Float:get_float_ab()
{
    return (a + 0.0) / (b + 0.0)
}

EDIT #2: It works (a / b = 4.168393) even without Float: tag but it gives out a tag mismatch on compile.

reinert 05-07-2011 07:15

Re: Float problem
 
ok, but it still returns me round number, 4.0 instead of 4.1.

Hunter-Digital 05-07-2011 07:16

Re: Float problem
 
I dunno what version of my post you saw, read again 'cause it works now. :lol:

reinert 05-07-2011 07:24

Re: Float problem
 
Ok thanks, now it works. one more question but it's not so necessary, How can I round number from 4.16 to 4.2 ?

Round the second number after dot.

Hunter-Digital 05-07-2011 07:33

Re: Float problem
 
If you're only using it for printing, you can just use %.1f and it will round 4.168393 to 4.2.

But for actual stored rounded value, I dunno :/

Elusive138 05-07-2011 07:40

Re: Float problem
 
There might be an easier way, but for rounding to a number of decimal places, you can always do
PHP Code:

fNum floatround(fNum*10) / 10.0 

PHP Code:

stock Float:floatround_dp(Float:numdp)
{
     new 
Float:mult floatpower10.0float(dp) )
     return 
floatroundnum mult ) / mult


All that does is shift everything you want to keep left of the decimal point, round it, and shift it back.

reinert 05-07-2011 07:49

Re: Float problem
 
PHP Code:

return floatdiv(ab

Gives me 2 tag mismatch errors.

grimvh2 05-07-2011 07:50

Re: Float problem
 
a and b arent floats

ConnorMcLeod 05-07-2011 09:20

Re: Float problem
 
Code:
return floatdiv(float(a), float(b))


All times are GMT -4. The time now is 04:17.

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