AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   float 1.3 + 0.1 = 1.399999 (https://forums.alliedmods.net/showthread.php?t=255181)

aron9forever 01-11-2015 17:38

float 1.3 + 0.1 = 1.399999
 
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

aron9forever 01-11-2015 17:45

Re: float 1.3 + 0.1 = 1.399999
 
fixed by changing %f to %.1f and using floatadd(str_to_float(PLUGIN_VERSION),0.1)

fysiks 01-11-2015 20:01

Re: float 1.3 + 0.1 = 1.399999
 
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.

Nextra 01-11-2015 22:28

Re: float 1.3 + 0.1 = 1.399999
 
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.

aron9forever 01-12-2015 20:26

Re: float 1.3 + 0.1 = 1.399999
 
Quote:

Originally Posted by fysiks (Post 2248026)
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 (Post 2248044)
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?

Jhob94 01-13-2015 04:11

Re: float 1.3 + 0.1 = 1.399999
 
just rename the plugin manually lmfao....

Nextra 01-16-2015 06:24

Re: float 1.3 + 0.1 = 1.399999
 
Quote:

Originally Posted by aron9forever (Post 2248477)
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.


All times are GMT -4. The time now is 15:30.

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