AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Why wont this calculate the k/d ratio? (https://forums.alliedmods.net/showthread.php?t=183494)

Bilal Pro 04-22-2012 11:36

Why wont this calculate the k/d ratio?
 
Hello people,

Im using natives to get frags/deaths Why wont this calculate the k/d ratio

PHP Code:

formatex(szTextcharsmax(szText), "K/D Ratio: \r%.2f"jb_get_user_frags(id) / jb_get_user_death(id)) 


claudiuhks 04-22-2012 11:51

Re: Why wont this calculate the k/d ratio?
 
It's unable to divide with zero.
If there are deaths, KPD will be equal with the result of division between kills and deaths.
Else, if there are no deaths, KPD will be equal with kills because it's now allowed to divide with zero.

PHP Code:

static FloatfKPDiKillsiDeaths;
iKills GetKillsiPlayer ), iDeaths GetDeathsiPlayer );
fKPD iDeaths floatiKills ) / floatiDeaths ) : floatiKills );

/*
  if( iDeaths > 0 ) fKPD = float( iKills ) / float( iDeaths );
  else if( iDeaths == 0 ) fKPD = float( iKills );
*/

client_printiPlayerprint_chat"KPD %0.2f"fKPD );

// Example output: KPD 1.74 


Bilal Pro 04-22-2012 11:55

Re: Why wont this calculate the k/d ratio?
 
I didn't understand that :(

fysiks 04-22-2012 15:13

Re: Why wont this calculate the k/d ratio?
 
Quote:

Originally Posted by Bilal Pro (Post 1694472)
I didn't understand that :(

Well, your first mistake was trying to divide two integers (called integer division) which does not work like it would on a calculator. You have to make them floating point values before you can divide them.

Also, as he mentioned, you have to check for the case where you would be dividing by zero which is not allowed.

Bilal Pro 04-23-2012 10:36

Re: Why wont this calculate the k/d ratio?
 
Solved


All times are GMT -4. The time now is 07:45.

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