![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 72
Rep Power: 4
![]() |
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 Energy = Energy - 0.05*(Energy) |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
I think the shortest you could get would be:
energy -= 0.05 * energy energy *= 0.95 |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Posts: 72
Rep Power: 4
![]() |
I was hoping there's something like:
Gone = energy * 50% |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
If I'm understanding you right .....
gone = energy*percentage; energy -= gone total_gone += gone |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Nope, unless you create a function to do it:
def pc(x): return x / 100.0 gone = energy * pc(50) pc = 0.01 gone = energy * 50*pc |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0
![]() |
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 saleprice20% 28.0 40.0 80.0 28.0 |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Isn't the point of invocation what matters? Or at least what matters in this case.
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |