tags:

views:

101

answers:

2

What happens when memdiff and/or totaldiff are negative? I was hoping for a negative memperc, but it doesn't seem like that's happening. Messing around in Python gives all sorts of confusing results when I plug in negative numbers.

local mem, percent, memdiff, totalMem, totaldiff = GetMemUsage("StarTip")
if mem then
    if totaldiff == 0 then totaldiff = 0.001 end
    memperc = (memdiff / totaldiff * 100)
    local num = memperc
    if num < 1 then num = 1 end
    if num > 100 then num = 100 end
    local r, g, b = gradient[num][1], gradient[num][2], gradient[num][3]
    return GetColorCode(format("%s (%.2f%%)", memshort(mem), memperc), r, g, b)
end

Edit: Oh come on, the question isn't a bad question. Maybe I should have been more clear on what I'm trying to do.

I'm taking two memory values, one overall and one specific to this addon. I'm creating a difference by doing thismem - lastmem. That's my difference. I have two of them, overall and addon specific. When Lua garbage collects, I get over 100% when I do memdiff / totaldiff * 100, when it should be negative. I don't know why.

Edit2:

Let me give some examples.

lastmem = 95
mem = 100.
lastaddonmem = 20
addonmem = 25.

totaldiff = mem - lastmem
addondiff = addonmem - lastaddonmem

perc = addondiff / totaldiff * 100 
perc = 100

lastmem = 100
mem = 95.
lastaddonmem = 25
addonmem = 20.

totaldiff = mem - lastmem
addondiff = addonmem - lastaddonmem

perc = addondiff / totaldiff * 100 
perc = 100

I know I'm going about this the wrong way. That's why I'm here.

Edit3: Why do you guys want to close this? I admit I'm dumb when it comes to math. Is it that people have that much intolerance for the mathematically challenged? I simply don't get math. Numbers confuse me like no other challenge of mine. I'm not uneducated. I have a learning disability. I don't see what the big deal is.

I ended up going with:

local mem, percent, memdiff, totalMem, totaldiff = GetMemUsage("StarTip")
if mem then
    if totaldiff == 0 then totaldiff = 0.0001 end
    local memperc
    if memdiff < 0 then
        memdiff = abs(memdiff)
        totaldiff = abs(totaldiff)
        memperc = memdiff / totaldiff * 100
        memperc = memperc * -1
    else
        memperc = memdiff / totaldiff * 100
    end
    local num = floor(memperc)
    if num < 1 then num = 1 end
    if num > 100 then num = 100 end
    local r, g, b = gradient[num][1], gradient[num][2], gradient[num][3]
    return GetColorCode(format("%s (%.2f%%)", memshort(mem), memperc), r, g, b)
end
+1  A: 
  • if memdiff and totaldiff have the same sign, then memdiff/totaldiff > 0. Multiplying by 100 doesn't change the sign

  • if memdiff and totaldiff have different signs, then memdiff/totaldiff < 0. Multiplying by 100, again, doesn't change this.

  • if memdiff == 0 then memdiff/totaldiff == 0. Multiplying by 100 doesn't change this.

to figure out why memdiff/totaldiff * 100 > 1, just note that this implies that memdiff*100 > totaldiff which implies that 100 > totaldiff/memdiff. There's no rules that say that percentages have to be between 0 and 1.

aaronasterling
+1  A: 
if totaldiff == 0 then totaldiff = 0.001 end 

    memperc = (memdiff / totaldiff * 100)

So if memdiff > 0.001 memperc will be greater than 100

EG 1: (0.001/0.001)*100 = 100

EG 2: (0.002/0.001)*100 = 200

Jaydee
according to OP, this is math and not programming and therefor decimals are for noobs. Go rational or go home.
aaronasterling
Aaron, you're simply being malicious here.
Scott
Now it makes sense after rereading this. :) You found where the problem is, and it's not where I thought it was.
Scott
"this is math and not programming and therefor decimals are for noobs". I think you got that the wrong way round, real numbers in Maths are normal but in programming a pain.
Jaydee
@jaydee. It was a joke but you have it the other way around. You would never right .001 (ughh!!) in math. It's 1/100. There are no real numbers in programming, everything in sight is rational. Mainly I'm just irritated that you said the same thing I did but in such a manner that Scott didn't have to think to get it.
aaronasterling
actually, 1/1000 and I should have said that there are no irrational numbers
aaronasterling