Raised This Month: $ Target: $400
 0% 

Float problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 07-16-2012 , 17:01   Float problem
Reply With Quote #1

Hi.
I creating my own stats plugin and i have problem with displayed float value.

For example, for kill - you got 1.0 point and for suicide/death - you got -0.5 point.
Example player has 3 kills and 1 death and i want to display 2.50 (3 * 1.0 - 1 * 0.5), but plugin display 2.49
Example code:
Code:
new Float:points[33];

public event_death()
{
new attacker = read_data(1);
new victim = read_data(2);

points[attacker] += 1.0;
points[victim] -= 0.5;
}

public show_points(id)
{
client_print(id, print_chat, "%.2f", points[id])  //Show 2.49, not 2.50
}
Where i got error ?
Thanks for any help.
hh2 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-16-2012 , 17:17   Re: Float problem
Reply With Quote #2

It's a common problem with floats. Precision is off sometimes, so you may see a value being off by 0.000001 or whatever.
You can round to 2 decimal places if you don't need more than that:
Code:
stock Float:round(Float:value, decimals) {     new Float:p = float(power(10, decimals));     return float(floatround(value * p, floatround_floor)) / p; }

This might also have issues with precision even though we are limiting to specific decimal places.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 07-16-2012 , 17:44   Re: Float problem
Reply With Quote #3

Still i got problem with this.
I use your stock everywhere where i got a float and it dit not help.

And i have question, how i use your stock ?
Possibly i make fault when using this:
Code:
points[id] = round(0.5, 1);
points[id] += round(1.5, 1);
It's work ?
hh2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-16-2012 , 23:52   Re: Float problem
Reply With Quote #4

Quote:
Originally Posted by hh2 View Post
Still i got problem with this.
I use your stock everywhere where i got a float and it dit not help.

And i have question, how i use your stock ?
Possibly i make fault when using this:
Code:
points[id] = round(0.5, 1);
points[id] += round(1.5, 1);
It's work ?
You should only use it when you display the value. Also, the second argument should be 2 if you want to round to 2 decimal places.
__________________
fysiks is offline
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 07-17-2012 , 08:51   Re: Float problem
Reply With Quote #5

fysiks, still dont work.

I save my data to sql:
sql - 3.50 (good)
plugin, when i display value - 3.49 (bad)

Stock from exolent dont working.
Where is error now ?

Last edited by hh2; 07-17-2012 at 08:54.
hh2 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-17-2012 , 10:36   Re: Float problem
Reply With Quote #6

There is no error in your code.
The problem is with floating point precision.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 07-17-2012 , 12:17   Re: Float problem
Reply With Quote #7

Ok, thanks for any help.
Topic exhausted.
hh2 is offline
Aooka
Veteran Member
Join Date: Aug 2011
Location: Villeurbanne
Old 07-17-2012 , 12:33   Re: Float problem
Reply With Quote #8

think to use nVault if you want to save data
__________________
Pawn ? Useless
Aooka is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-17-2012 , 12:33   Re: Float problem
Reply With Quote #9

Quote:
Originally Posted by Aooka View Post
think to use nVault if you want to save data
... That has nothing to do with the discussion.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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 23:13.


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