Raised This Month: $51 Target: $400
 12% 

str_to_float weird converting


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DjOptimuS
Senior Member
Join Date: Jan 2009
Old 02-09-2012 , 23:29   str_to_float weird converting
Reply With Quote #1

I tried to make a plugin today for mp_roundtime, to let admins set this cvar but restricted between 2 floats, mine are minimum 1.75 and maximum 5.0

But for example, when i convert the 1.9 string into float, the plugins returns 1.899999.

What's really funny it's when i convert the 1.899999 float back to string, which will return 1.8

Finally i tried to set the cvar as a string itself and it worked, but i wonder why is this happening and how to avoid it. Thank you.

PS. I already tried with floatround, nothing seems to work. Or maybe i did something wrong. Thank you.

Last edited by DjOptimuS; 02-09-2012 at 23:40.
DjOptimuS is offline
DjOptimuS
Senior Member
Join Date: Jan 2009
Old 02-09-2012 , 23:41   Re: str_to_float weird converting
Reply With Quote #2

A tiny test code

PHP Code:
#include <amxmodx>
#include <amxmisc>

#define PLUGIN    "New Plugin"
#define AUTHOR    "Unknown"
#define VERSION    "1.0"

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_srvcmd("test""test", -1"- help");
}

public 
test()
{
    new 
float[] = "1.9";
    new 
Float:testare str_to_float(float);
    
server_print("%f"testare);
    
server_print("");
    
server_print("");
    
server_print("");
    
formatex(floatsizeof (float) - 1"%f"testare);
    
server_print(float);
    
server_print(float);
    
server_print(float);

This prints
Code:
test
1.899999



1.8
1.8
1.8

Last edited by DjOptimuS; 02-09-2012 at 23:42.
DjOptimuS is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-09-2012 , 23:43   Re: str_to_float weird converting
Reply With Quote #3

It's been known for a lot of languages to have this issue with floating points.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 02-09-2012 at 23:43.
Exolent[jNr] is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 02-10-2012 , 00:23   Re: str_to_float weird converting
Reply With Quote #4

http://en.wikipedia.org/wiki/Single_...g-point_format
for the reason of the precision, some real numbers can't be accurate in this format
__________________
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 02:54.


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