AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Mathematical Problem (https://forums.alliedmods.net/showthread.php?t=306843)

THC420 04-16-2018 10:45

Mathematical Problem
 
1 Attachment(s)
So I want to know what percentage of the level he is at, but the number displayed in the hud is always wrong
PHP Code:

        porcentaje = (g_ammopacks[id] / levelsAmmopack(g_level[id]))*100 



and this is how i display it
PHP Code:

         ShowSyncHudMsg(ID_SHOWHUDg_MsgSync2"|Vida: %s|^n|Chaleco: %s|^n|Level: %s|^n|AmmoPacks: %s/%s|^n|Restantes: %s (%d%%)|^n|Clase: %s|^n|Velocidad: %.0f|"AddComma(pev(ID_SHOWHUDpev_health)), AddComma(get_user_armor(id)), AddComma(g_level[id]), AddComma(g_ammopacks[ID_SHOWHUD]),AddComma(levelsAmmopack(g_level[id])), AddComma(restantes), porcentaje, class, spd


in the case of this picture it has to be 23.08 % the porcentage displayed, but it is 100 :s

E1_531G 04-16-2018 14:39

Re: Mathematical Problem
 
Quote:

Restantes: %s (%d%%)
%d isn't for float. Use %f.

EFFx 04-16-2018 21:12

Re: Mathematical Problem
 
Code:

Restantes: %s (%d%%)
Use %.2f%% instead of %d%%. When 2 is the numbers after the '.'.

fysiks 04-16-2018 21:43

Re: Mathematical Problem
 
Also, make sure that the values that you are dividing are floating-point values. I'm guessing that they are currently integers. So, if they are indeed integers, you'll need to use float() on them to make sure that you aren't doing integer division.

EFFx 04-16-2018 23:19

Re: Mathematical Problem
 
PHP Code:

new Float:porcentaje = (g_ammopacks[id] / levelsAmmopack(g_level[id])) * 100.0 


Black Rose 04-17-2018 14:51

Re: Mathematical Problem
 
The problem is that you are dividing an integer with an integer just like fysiks said and after that you multiply by 100.
Here's a simple example to show you why it doesn't work:
Code:
    server_print("1: %.2f", 5/3*100.0); // 1: 100.00 (5/3 = 1, 1*100 = 100)     server_print("2: %.2f", 5*100.0/3); // 2: 166.66 (5*100 = 500, 500/3 = 166.66)

As you can see, as long as the multiplication is done before division the result is what you expect.

Here are the options you have.
Code:
    // Saved as integer, move the multiplication.     new intVar1 = floatround(1 * 100.0 / 3)     server_print("1: %.1f / %d", float(intVar1), intVar1); // 1: 33.0 / 33     // Saved as integer, turn it into float before dividing.     new intVar2 = floatround(float(1) / 3 * 100.0)     server_print("2: %.1f / %d", float(intVar2), intVar2); // 2: 33.0 / 33     // Saved as float, move the multiplication.     new Float:flVar3 = 1 * 100.0 / 3     server_print("3: %.1f / %d", flVar3, floatround(flVar3)); // 3: 33.3 / 33     // Saved as float, turn it into float before dividing.     new Float:flVar4 = float(1) / 3 * 100.0     server_print("4: %.1f / %d", flVar4, floatround(flVar4)); // 4: 33.3 / 33
I suggest the first option for simplicity.

THC420 04-18-2018 09:04

SOLVED Mathematical Problem
 
yes, that was the problem, thanks man


All times are GMT -4. The time now is 04:32.

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