Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Arithmetic By Percentage (http://www.programmingforums.org/showthread.php?t=12461)

kurt Jan 30th, 2007 3:04 AM

Arithmetic By Percentage
 
Let's say i want to perform a subtraction on a variable (say, Energy) by 5%, the shortest way i can think of is:

:

Tolerance = 0.05 * Energy
Energy = Energy - Tolerance

or

:

Energy = Energy - 0.05*(Energy)
I know it's already pretty short, but is there a shorter, elegant way?

Arevos Jan 30th, 2007 4:48 AM

I think the shortest you could get would be:
:

energy -= 0.05 * energy
Or:
:

energy *= 0.95

kurt Jan 30th, 2007 4:59 AM

I was hoping there's something like:

Gone = energy * 50%

grumpy Jan 30th, 2007 5:15 AM

If I'm understanding you right .....
:

  gone = energy*percentage;
  energy -= gone
  total_gone += gone


Arevos Jan 30th, 2007 6:52 AM

Quote:

Originally Posted by kurt (Post 123265)
I was hoping there's something like:

Gone = energy * 50%

Nope, unless you create a function to do it:
:

def pc(x): return x / 100.0

gone = energy * pc(50)

Or
:

pc = 0.01

gone = energy * 50*pc

The problem is that % is already the modulus operator, so it can't also be used for percentages.

ptmcg Jan 30th, 2007 10:52 AM

A Percent class for doing percentage arithmetic
 
Python's ability to emulate numeric types makes it easy to "extend" Python with some interesting behavior. Here's a Percent class that allows you to write code like: saleprice = origprice - discount, where origprice is a number, and discount is a class instance that computes the actual discount based on the value of origprice.

You could do the same thing with a variable called markup as in markup=Percent(10) and then compute retailprice = wholesaleprice + markup.

-- Paul

:

class Percent(object):
    def __init__(self,pctval):
        self.pct = pctval
        self.pctFloat = pctval/100.0
       
    def __mul__(self,other):
        if isinstance(other,Percent):
            return Percent(self.pct*other.pctFloat)
        else:
            return other*self.pctFloat
           
    def __rmul__(self,other):
        return self * other

    def __radd__(self,other):
        if isinstance(other,Percent):
            return Percent(self.pct+other.pct)
        else:
            return other + self*other

    def __rsub__(self,other):
        if isinstance(other,Percent):
            return Percent(other.pct-self.pct)
        else:
            return other - self*other

    def __str__(self):
        if int(self.pct)==self.pct:
            return "%d%%" % self.pct
        else:
            return "%f%%" % self.pct

discount = Percent(20)
print discount

print 35 - discount
print 50 - discount
print 100 - discount

origprice = 35.00
saleprice = origprice - discount
print saleprice

Prints:
:

20%
28.0
40.0
80.0
28.0


DaWei Jan 30th, 2007 1:32 PM

Very short.

ptmcg Jan 30th, 2007 6:55 PM

Well, once you get the class junk out of the way, I'd say this is pretty short/elegant, certainly more along the lines of what the OP was looking for.

:

discount = Percent(20)
origprice = 35.00
saleprice = origprice - discount


Percent is more than just a fancy wrapper around "divide by 100". Note that discount can be subtracted from any number of other values, and it will calculate the respective percentage before returning the value.

Are you the curmudgeon-in-residence on these forums? :)

-- Paul

DaWei Jan 30th, 2007 7:55 PM

If it makes me a curmudgeon to point out when you're being silly, yes, indeed. The OP distinctly asked for a short way. Merely hiding things so that they aren't visible at the point of invocation doesn't qualify as either short or elegant.

Game_Ender Jan 31st, 2007 3:41 AM

Isn't the point of invocation what matters? Or at least what matters in this case.


All times are GMT -5. The time now is 1:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC