[Tutor] sales tax

Michael Langford mlangford.cs03 at gtalumni.org
Wed Sep 19 05:58:14 CEST 2007


There is definitely a secondary understanding of math that comes from
working on computers. How computers do math is quite a bit different from
how you or I do it on a piece of paper. The fact that python can do
arbitrary large integer arithmetic makes it quite nice for many math
applications as you don't have to deal with some of the issues of other
languages.

Floating point numbers are different than anything you may have encountered
out of computing. Their nuances are fully detailed in the wonderful "What
every computer scientist should know about floating point":
http://docs.sun.com/source/806-3568/ncg_goldberg.html

However, the paper is a little long and quite obtuse, even though it goes
through everything from a mathematical perspective. Don't feel like you need
to understand it, but when you DO need to understand the true issues with
floating point, that will be your guide.

There is also great advantage in learning how to do fixed point
calculations, especially in python, with its awesome integer type. This is
especially the case for times when you need very accurate addition and
subtraction, such as with large quantities of money.
http://fixedpoint.sourceforge.net/ is a project that implements it. I don't
seem to see fixed point numbers in the python standard libraries, but then
again, I'd not be surprised if they were there.

      --Michael

-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com

On 9/18/07, Ian Witham <witham.ian at gmail.com> wrote:
>
> As Michael points out, you need to explicitly use the round function, as
> the float formatting merely truncates anything after the second decimal
> place.
>
> I ran across a similar problem with the int() fuction early on. Anything
> after the decimal point is truncated, not rounded, leading to behavior I was
> not expecting.
>
> For example:
>
> int(-1.5) == -1
> int(-.5) == 0
> int(.5) == 0
> int(1.5) == 1
>
> Ian
>
>
> On 9/19/07, Christopher Spears < cspears2002 at yahoo.com> wrote:
> >
> > I wrote a script that takes a price and a sales tax
> > and calculates the new price.
> >
> > #!/usr/bin/env python
> >
> > def calculate_price(price, percent_tax):
> >     sales_tax = price * percent_tax
> >     new_price = price + sales_tax
> >     return new_price
> >
> > price = float(raw_input("Enter a price: "))
> > percent_tax = float(raw_input("Enter a sales tax: "))
> > print "%.2f" % calculate_price(price, percent_tax)
> >
> > Here is the script in action:
> > io at io-station-1 ./chap5 173> python sales_tax.py
> > Enter a price: 10.00
> > Enter a sales tax: .0825
> > 10.82
> >
> > I'm not convinced that the new price is completely
> > accurate because the price without the string
> > formating is 10.825
> >
> > io at io-station-1 ./chap5 164> python sales_tax.py
> > Enter a price: 10
> > Enter a sales tax: .0825
> > 10.825000
> >
> > Wouldn't the price be rounded up to 10.83 in the real
> > world?  How can I accomplish this?
> > _______________________________________________
> > Tutor maillist  -   Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070918/7216643c/attachment.htm 


More information about the Tutor mailing list