Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 30th, 2007, 3:04 AM   #1
kurt
Programmer
 
Join Date: Oct 2005
Posts: 72
Rep Power: 4 kurt is on a distinguished road
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?
kurt is offline   Reply With Quote
Old Jan 30th, 2007, 4:48 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
I think the shortest you could get would be:
energy -= 0.05 * energy
Or:
energy *= 0.95
Arevos is offline   Reply With Quote
Old Jan 30th, 2007, 4:59 AM   #3
kurt
Programmer
 
Join Date: Oct 2005
Posts: 72
Rep Power: 4 kurt is on a distinguished road
I was hoping there's something like:

Gone = energy * 50%
kurt is offline   Reply With Quote
Old Jan 30th, 2007, 5:15 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
If I'm understanding you right .....
   gone = energy*percentage;
   energy -= gone
   total_gone += gone
grumpy is offline   Reply With Quote
Old Jan 30th, 2007, 6:52 AM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by kurt View Post
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.
Arevos is offline   Reply With Quote
Old Jan 30th, 2007, 10:52 AM   #6
ptmcg
Newbie
 
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0 ptmcg is on a distinguished road
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
ptmcg is offline   Reply With Quote
Old Jan 30th, 2007, 1:32 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Very short.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 30th, 2007, 6:55 PM   #8
ptmcg
Newbie
 
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0 ptmcg is on a distinguished road
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
ptmcg is offline   Reply With Quote
Old Jan 30th, 2007, 7:55 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 31st, 2007, 3:41 AM   #10
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Isn't the point of invocation what matters? Or at least what matters in this case.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ strange arithmetic? Mcoy C++ 13 Mar 18th, 2006 1:49 AM
Arithmetic CodeJunkie Assembly 2 Jan 8th, 2006 3:28 PM
modulo arithmetic question LOI Kratong C++ 6 Sep 28th, 2005 3:07 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:55 PM.

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