Rules FAQ Members List Search Register Login

 Raised This Month: \$61 Target: \$400 15%

Author Message
Alon2k2
Junior Member
Join Date: Apr 2010
 11-16-2023 , 06:43   A calculation problem with floats numbers #1 Hi, I have a problem with doing calculations like 0.01 + 0.03. Is there toFixed for this thing like in JavaScript? Code: ```new Float:floatVal = 0.01 + 0.03; PrintToChat(client, "\$%.2f", floatVal); // \$0.03 wrong!!```
helloworlds
Junior Member
Join Date: Oct 2022
11-23-2023 , 09:38   Re: A calculation problem with floats numbers
#2

There's probably some more elegant way to do this with arithmetic and/or bit fiddling, but here's one approach that takes advantage of the "FloatToString" function handling this case automagically. It should at least solve your example case.

PHP Code:
``` #include <sourcemod>// Parameters:// f - Floating point number to convert.// n - At how many decimal points to truncate at.// out - Buffer to store string in.// maxlen - Maximum length of string buffer.// Return value: Number of characters written to the buffer, not including the null terminator.int FloatToStringTruncate(float f, int n, char[] out, int maxlen){    int res = FloatToString(FloatFraction(f), out, maxlen);    int cutoff = 3 + n;    if (cutoff >= maxlen)    {        return res;    }    out[cutoff] = '\0';    return Format(out, cutoff, "%d%s%s",        RoundToFloor(f),        n <= 0 ? "" : ".",        out[n <= 0 ? 3 : 2]    );}public void OnPluginStart(){    float f = 0.04;    PrintToServer("Plain: %f", f);    PrintToServer("Format-class functions: %.2f", f);    char buffer[32];    FloatToStringTruncate(f, 2, buffer, sizeof(buffer));    PrintToServer("FloatToStringTruncate: %s", buffer);}  ```
...or whatever "truncate at n chars after the decimal point" method you'd wanna use.

This outputs:

Quote:
 Plain: 0.039999 Format-class functions: 0.03 FloatToStringTruncate: 0.04

Last edited by helloworlds; 11-23-2023 at 09:44.
101
Member
Join Date: Nov 2023
11-24-2023 , 15:44   Re: A calculation problem with floats numbers
#3

Quote:
 Originally Posted by helloworlds There's probably some more elegant way to do this with arithmetic and/or bit fiddling, but here's one approach that takes advantage of the "FloatToString" function handling this case automagically. It should at least solve your example case. PHP Code: ``` #include  // Parameters: // f - Floating point number to convert. // n - At how many decimal points to truncate at. // out - Buffer to store string in. // maxlen - Maximum length of string buffer. // Return value: Number of characters written to the buffer, not including the null terminator. int FloatToStringTruncate(float f, int n, char[] out, int maxlen) {     int res = FloatToString(FloatFraction(f), out, maxlen);     int cutoff = 3 + n;     if (cutoff >= maxlen)     {         return res;     }     out[cutoff] = '\0';     return Format(out, cutoff, "%d%s%s",         RoundToFloor(f),         n <= 0 ? "" : ".",         out[n <= 0 ? 3 : 2]     ); } public void OnPluginStart() {     float f = 0.04;     PrintToServer("Plain: %f", f);     PrintToServer("Format-class functions: %.2f", f);     char buffer[32];     FloatToStringTruncate(f, 2, buffer, sizeof(buffer));     PrintToServer("FloatToStringTruncate: %s", buffer); }  ``` ...or whatever "truncate at n chars after the decimal point" method you'd wanna use. This outputs:

PHP Code:
``` #include <sourcemod> #define fix    0.0000001 // Fix ONLY FOR PRINT Floats (Don't Use During Calculations) public OnPluginStart()  {     RegServerCmd("sm_test",Command_test,"Test Floats"); } public Action:Command_test(args) {     float X=0.03;     float Z=0.01;     PrintToServer("fixed X+Z = %f",X+Z+fix); // Result 0.040000     PrintToServer("Abs X+Z = %f",X+Z); // Result 0.039999 }  ```
helloworlds
Junior Member
Join Date: Oct 2022
11-26-2023 , 08:10   Re: A calculation problem with floats numbers
#4

Quote:
 Originally Posted by 101 I Don't Know Why This Happens [...]
It has to do with the fact that float32 are represented as a sign bit, 8 bits for exponent, and 23 bits for the fraction, and you just can't represent all values accurately with this. For more info, see:
https://en.wikipedia.org/wiki/IEEE_754, https://en.wikipedia.org/wiki/Single...g-point_format
101
Member
Join Date: Nov 2023
 11-26-2023 , 09:16   Re: A calculation problem with floats numbers #5 So the bits of fraction insist on carrying Nonzero numbers no matter what the result! by the way, thank you I found extra information here : https://stackoverflow.com/questions/...floating-point But who can believe that 0.039999 is closer to 0.04 than (0.040000 itself)!
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
12-25-2023 , 13:57   Re: A calculation problem with floats numbers
#6

Quote:
 Originally Posted by Bacardi You can try trick output message, by adding: + 0.0000001 PHP Code: ```     float value = 1.0;          PrintToServer("= value %.2f", value);     value += 0.01;          PrintToServer("- value %f", value);     PrintToServer("- - value %f", value + 0.0000001);     PrintToServer("- - value %.2f", value);     PrintToServer("- - value %.2f", value + 0.0000001);  ``` Code: ```= value 1.00 - value 1.009999 - - value 1.010000 - - value 1.00 - - value 1.01```
__________________
Do not Private Message @me

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Server Discussion     Source Servers (SRCDS)     HL1 Servers (HLDS) AMX Mod X     News     Bug Reports     General     Off-Topic     Plugins         Suggestions / Requests         Approved Plugins         New Plugin Submissions         Unapproved/Old Plugins         Translation Request         High-Traffic Plugins             GunGame             UAIO (Ultimate All-In-One Plugin)             xREDIRECT             CSDM             AMX Super             RuneMod             Zombie Plague Mod             SuperHero Mod                 News                 Tech Support                 Scripting Help                 Off-Topic / General Chat                 Heroes                     Suggestions / Requests                     Approved Heroes                     New Submissions                     Unapproved/Old Heroes                         Module Heroes                     SuperHero Mod Stats - By 123                 (OLD) Bug Reports     Scripting         Scripting Help         Code Snippets/Tutorials         Module Coding     Donor Access SourceMod     News     General     Plugins         Plugins         Unapproved Plugins         Plugin/Gameplay Ideas and Requests         High-Traffic Plugins             SourceMod Anti-Cheat             Zombie:Reloaded             SourceBans / SourceBans++             VSH / Freak Fortress             Store             SM_Hosties             HLstatsX:CE     Scripting     Extensions     Snippets and Tutorials     Donor Access     Metamod: Source         Metamod:Source Plugins         Metamod:Source Questions         Coding MM:S Plugins & SM Extensions Hosted Stuff     Asherkin's Plugins         TFDodgeball         TF2Items         SteamTools     Bail's Plugins         CSDM         CS:S DM Off-Topic & Trash     Off-Topic     Trash

All times are GMT -4. The time now is 20:42.

 DMCA - Archive - Top