Jump to content

Decimal problem


Deltanic

Recommended Posts

Some code in onPlayerWasted:

local playerkills = getElementData ( source, "Kills" )
local playerdeaths = getElementData ( source, "Deaths" ) + 1
if playerkills == 0 then
local playerratio = 1/playerdeaths
tonumber( ("%.2f"):format(playerratio) ) 
setElementData ( source, "Ratio", playerratio  )
else
local playerratio = playerkills/playerdeaths
tonumber( ("%.2f"):format(playerratio) ) 
setElementData ( source, "Ratio", playerratio  )
end

But, it returns when I died three times and I haven't killed still 0.333343, and I don't know where that four can come from. Is this script wrong, or am I doing it wrong?

Edited by Guest
Link to comment

EDIT: Result after 3 deaths, 0 kills:

0.330000013

Code:

local playerkills = getElementData ( source, "Kills" )
local playerdeaths = getElementData ( source, "Deaths" ) + 1
if playerkills == 0 then
local playerratio = 1/playerdeaths
setElementData ( source, "Ratio", tonumber( ("%.2f"):format(playerratio)  )
else
local playerratio = playerkills/playerdeaths
setElementData ( source, "Ratio", tonumber( ("%.2f"):format(playerratio)  )
end

So the result is different, but it isn't what I want. What to do now?

Link to comment
EDIT: Result after 3 deaths, 0 kills:

0.330000013

Code:

local playerkills = getElementData ( source, "Kills" )
local playerdeaths = getElementData ( source, "Deaths" ) + 1
if playerkills == 0 then
local playerratio = 1/playerdeaths
setElementData ( source, "Ratio", tonumber( ("%.2f"):format(playerratio)  )
else
local playerratio = playerkills/playerdeaths
setElementData ( source, "Ratio", tonumber( ("%.2f"):format(playerratio)  )
end

So the result is different, but it isn't what I want. What to do now?

What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail.

Link to comment

What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail.

playerdeaths will never be 0 if he doesn't start out on a negative number (which is unlikely) since he increments it before any dangerous divisions.

Link to comment

What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail.

I use 1 / playerdeaths, because divide by 0 is impossible. But what you say is also true, the answer would be not what I want. But I changed it:

1/playerdeaths/2

Indeed playerdeaths would be never 0, because this is only called when the player dies.

But that doesn't matter. My numbers still won't have 2 decimals. I still don't know what to do.

Link to comment

I think he wants a Kill per Death ratio...

function getPlayerKDRatio(player)
local kills, deaths = getElementData(player, "Kills"), getElementData(player, "Deaths")
local ratio = kills/deaths
if(kills > 0 and deaths <= 0) then return string.format("%.2f", kills) end
return string.format("%.2f", ratio)
end

Link to comment

What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail.

playerdeaths will never be 0 if he doesn't start out on a negative number (which is unlikely) since he increments it before any dangerous divisions.

Not necessarily.

What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail.

I use 1 / playerdeaths, because divide by 0 is impossible. But what you say is also true, the answer would be not what I want. But I changed it:

1/playerdeaths/2

Indeed playerdeaths would be never 0, because this is only called when the player dies.

But that doesn't matter. My numbers still won't have 2 decimals. I still don't know what to do.

You can NOT divide by 0 but you can divide 0 by anything but 0, which will give you 0.

Link to comment

Not necessarily.. what?

If you start with a non-negative number, say, 5, you divide by 6.

If you start with 0, you divide by 1.

The only way in which you can have a dangerous division operator is if you start with -1, becoming 0. And that's unlikely, since you can't have died -1 times in a game.

Link to comment

But you can get invalid or unexpected results from getElementData since every resource can modify element's data especially such as "Deaths", "Kills", etc. therefore using names such as "myresource.deaths" is more suitable for element data and is safer.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...