Raised This Month: $ Target: $400
 0% 

float 1.3 + 0.1 = 1.399999


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 01-11-2015 , 17:38   float 1.3 + 0.1 = 1.399999
Reply With Quote #1

Okay so what gives?

static const
PLUGIN_NAME[] = "xxxx",
PLUGIN_AUTHOR[] = "xxx",
PLUGIN_VERSION[] = "1.3"


new Temp[256];
formatex(Temp, 255, "addons/amxmodx/plugins/plugin%f.amxx",str_to_float(PLUGIN_VERSION)+0 .1);
and it results in a file named plugin1.399999.amxx

why? I want it to be 1.4
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 01-11-2015 , 17:45   Re: float 1.3 + 0.1 = 1.399999
Reply With Quote #2

fixed by changing %f to %.1f and using floatadd(str_to_float(PLUGIN_VERSION),0.1)
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-11-2015 , 20:01   Re: float 1.3 + 0.1 = 1.399999
Reply With Quote #3

I'd probably recommend using integers. One for the major version and one for the minor version.

However, I don't see any reason to have the full file path to a .amxx file but even more-so why you would need that of a future version.
__________________
fysiks is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 01-11-2015 , 22:28   Re: float 1.3 + 0.1 = 1.399999
Reply With Quote #4

Floats are not precise enough and both 1.3 and 0.1 can not be accurately represented. By default the AMXX %f formatting will not round this small error away and will thus result in a 1.39999 being output (which is technically correct).

Your floatadd workaround does nothing, it is the %.1f formatting that "fixes" the issue. You can easily verify this using a small C program and printing the same numbers with something like %.12f so the numbers don't get rounded by the library.
__________________
In Flames we trust!

Last edited by Nextra; 01-11-2015 at 22:28.
Nextra is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 01-12-2015 , 20:26   Re: float 1.3 + 0.1 = 1.399999
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
I'd probably recommend using integers. One for the major version and one for the minor version.

However, I don't see any reason to have the full file path to a .amxx file but even more-so why you would need that of a future version.
The idea was for a self updating plugin
I scaped the version usage tho, came up with a solution to overwriting the existing .amxx file even if it's being run by the server

Quote:
Originally Posted by Nextra View Post
Floats are not precise enough and both 1.3 and 0.1 can not be accurately represented. By default the AMXX %f formatting will not round this small error away and will thus result in a 1.39999 being output (which is technically correct).

Your floatadd workaround does nothing, it is the %.1f formatting that "fixes" the issue. You can easily verify this using a small C program and printing the same numbers with something like %.12f so the numbers don't get rounded by the library.
since when does decimal selection round the float? shouldn't it still print 1.3 with %.1f of 1,39999?
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.

Last edited by aron9forever; 01-12-2015 at 20:28.
aron9forever is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 01-16-2015 , 06:24   Re: float 1.3 + 0.1 = 1.399999
Reply With Quote #6

Quote:
Originally Posted by aron9forever View Post
since when does decimal selection round the float? shouldn't it still print 1.3 with %.1f of 1,39999?
Always. It tells it to round to X digits after the decimal point. However some implementations use "round to even" instead of the common "banker's rounding" so that's why you may be confused. Rounding does always occur but I'm not certain how (or by what) the method is specified.
__________________
In Flames we trust!
Nextra is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 01-13-2015 , 04:11   Re: float 1.3 + 0.1 = 1.399999
Reply With Quote #7

just rename the plugin manually lmfao....
__________________
Jhob94 is offline
Reply


Thread Tools
Display Modes

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 15:30.


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