This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Solved something wrong about float plus


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kazya3
Member
Join Date: Aug 2019
Location: CN
Old 10-05-2022 , 23:39   something wrong about float plus
Reply With Quote #1

Hi everyone, there's sth wrong in my code

float p_fireRating[MAXPLAYERS + 1] = {1.00, ...};

void addSkills(int client)
{
p_fireRating[client] = p_fireRating[client] + 0.01; //get 1.009999 not 1.01
}

and i show it in the menu item like that
void ShowMenuMain(int client)
{
char firingRateText[64];
FormatEx(firingRateText,sizeof(firingRateText ),"weapon-fireRate:[%.2f]",p_fireRating[client]);
//menu
g_hMenuMain = new Menu(MainMenuHandler);
g_hMenuMain.SetTitle("");
g_hMenuMain.AddItem("", firingRateText);//be shown as 1.00, not 1.01
g_hMenuMain.ExitButton = true;

g_hMenuMain.Display(client, MENU_TIME_FOREVER);
}

HOWEVER, it get p_fireRating[client] equals 1.009999 after plus 0.01.
So the menu just show the 1.00 because %.02f ,what's wrong

player use credits for level up, and because of that reason, it seems nothing happened first time(indeed p_fireRating = 1.009999 and show 1.00 )

Last edited by kazya3; 11-04-2022 at 23:29.
kazya3 is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 10-06-2022 , 00:07   Re: something wrong about float plus
Reply With Quote #2

Quote:
Originally Posted by kazya3 View Post
Hi everyone, there's sth wrong in my code

float p_fireRating[MAXPLAYERS + 1] = {1.00, ...};

void addSkills(int client)
{
p_fireRating[client] = p_fireRating[client] + 0.01;
}

and i show it in the menu item like that
void ShowMenuMain(int client)
{
char firingRateText[64];
FormatEx(firingRateText,sizeof(firingRateText ),"weapon-fireRate:[%.2f]",p_fireRating[client]);
//menu
g_hMenuMain = new Menu(MainMenuHandler);
g_hMenuMain.SetTitle("");
g_hMenuMain.AddItem("", firingRateText);//0
g_hMenuMain.ExitButton = true;

g_hMenuMain.Display(client, MENU_TIME_FOREVER);
}

HOWEVER, it get p_fireRating[client] equals 1.009999 after plus 0.01.
So the menu just show the 1.00 because %.02f ,what's wrong

player use credits for level up, and because of that reason, it seems nothing happened first time(indeed p_fireRating = 1.009999 and show 1.00 )
post your full code (use php tags to wrap your code) + which game

make sure your code is compliable before posting it
__________________

Last edited by alasfourom; 10-06-2022 at 00:08.
alasfourom is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 10-06-2022 , 07:02   Re: something wrong about float plus
Reply With Quote #3

bkz it's float, lol
__________________
Grey83 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 10-06-2022 , 08:28   Re: something wrong about float plus
Reply With Quote #4

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
Bacardi is offline
kazya3
Member
Join Date: Aug 2019
Location: CN
Old 10-06-2022 , 12:36   Re: something wrong about float plus
Reply With Quote #5

thanks man! I thought that problem really weird
kazya3 is offline
Earendil
Senior Member
Join Date: Jan 2020
Location: Spain
Old 10-20-2022 , 21:36   Re: something wrong about float plus
Reply With Quote #6

Quote:
Originally Posted by kazya3 View Post
Hi everyone, there's sth wrong in my code

float p_fireRating[MAXPLAYERS + 1] = {1.00, ...};

void addSkills(int client)
{
p_fireRating[client] = p_fireRating[client] + 0.01; //get 1.009999 not 1.01
}

and i show it in the menu item like that
void ShowMenuMain(int client)
{
char firingRateText[64];
FormatEx(firingRateText,sizeof(firingRateText ),"weapon-fireRate:[%.2f]",p_fireRating[client]);
//menu
g_hMenuMain = new Menu(MainMenuHandler);
g_hMenuMain.SetTitle("");
g_hMenuMain.AddItem("", firingRateText);//be shown as 1.00, not 1.01
g_hMenuMain.ExitButton = true;

g_hMenuMain.Display(client, MENU_TIME_FOREVER);
}

HOWEVER, it get p_fireRating[client] equals 1.009999 after plus 0.01.
So the menu just show the 1.00 because %.02f ,what's wrong

player use credits for level up, and because of that reason, it seems nothing happened first time(indeed p_fireRating = 1.009999 and show 1.00 )
Your code is completely right and the result is expected. Floating point arithmetic in programming usually comes with some inaccuracy, you can't get rid off of it. You can only mitigate by using floating points with more precision(double in C/C++/Java) but here on SM you have to deal with 32-bit floating points with single precision. You may be able to fix it as Bacardi said, but have in mind that this may happen again the next time you use floats.

Here is some info about the problem of floating points. Go to 4 & 4.1.
__________________
>>My plugins<<
>>GitHub<<

Last edited by Earendil; 10-20-2022 at 21:40.
Earendil is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 10-29-2022 , 23:35   Re: something wrong about float plus
Reply With Quote #7

If you only need 1 or 2 digits after the dot, you can use an int, which you will divide each time

Example for 0.01 precision (2 digits after dot):

0.01 -> 1
0.1 -> 10
1 -> 100
1.57 -> 157

to write:
number = 157; // 1.57
Format(buffer, sizeof(buffer), "%i.%i", number / 100, number % 100); // This will write 1.57 in the buffer

This way you never have to deal with the float type and the issues it brings, but it'll be a bit more work to support.

Please note that this will reduce the maximum int you can get:
default int max: 2 147 483 647
max number with this method (2 digits after dot): 21 474 836.47

NOTE: A division between two integers will always return an integer. It will also always round to the lowest int afaik, not sure for negative numbers though.
NOTE 2: Damn I love modulo <3
__________________
GitHub | Discord: @azalty | Steam

Last edited by azalty; 10-29-2022 at 23:40.
azalty 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 13:59.


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