From: Stefan Behnel on 2 Apr 2010 11:11 Patrick Maupin, 02.04.2010 07:25:> On Apr 1, 11:52 pm, Dennis Lee Bieber wrote: >> On Thu, 01 Apr 2010 22:44:51 +0200, superpollo >> declaimed the following in gmane.comp.python.general: >> >>> how much is one half times one half? >> >> import math >> print math.exp((math.log(1) - math.log(2)) >> + (math.log(1) - math.log(2))) > > That's all well and good, but base 'e' is kind of complicated. Some > of us were using base 10, and others took Tim's lead and were using > base 2: > > >>> print math.exp(((math.log(1)/math.log(2) - math.log(2)/math.log(2)) + (math.log(1)/math.log(2) - math.log(2)/math.log(2)))*math.log(2)) > 0.25 The above can be rewritten as print('0.25') which is much faster and also a lot more readable. Stefan From: Andreas Waldenburger on 2 Apr 2010 15:41 On Thu, 01 Apr 2010 22:44:51 +0200 superpollo wrote: > how much is one half times one half? While everyone else is mocking you: Can you please elaborate on why you want to know and what kind of problem you're trying to solve with this? Also, don't you think you should have picked a maths forum for this kind of question? Meanwhile: http://en.wikipedia.org/wiki/Fractions#Multiplying_by_a_fraction And in Italian: http://it.wikipedia.org/wiki/Frazione_(matematica)#Moltiplicazione_e_division /W (Yes, I have nothing to do right now.) -- INVALID? DE! From: Patrick Maupin on 2 Apr 2010 15:34 On Apr 2, 2:41 pm, Andreas Waldenburger wrote: > While everyone else is mocking you: Can you please elaborate on why you > want to know and what kind of problem you're trying to solve with this? > Also, don't you think you should have picked a maths forum for this > kind of question? Methinks the OP is fluent in the way of choosing newsgroups. According to google, he has posted 6855 messages in 213 groups. http://groups.google.com/groups/profile?enc_user=ul3SQhIAAAAYmLD0Oj5Yxp-liP3Vw9uApbyajUBv9M9XLUB2gqkZmQ And I can't speak for anybody else, but I just assumed it was an April Fool's question. I meant to be laughing with the OP, not at him, so sorry if I misunderstood. Regards, Pat From: Mensanator on 2 Apr 2010 15:35 On Apr 1, 9:44 pm, Steven D'Aprano wrote:> On Thu, 01 Apr 2010 19:49:43 -0500, Tim Chase wrote: > > David Robinow wrote: > >> \$ python -c "print 1/2 * 1/2" > >> 0 > > >>  But that's not what I learned in grade school. > >> (Maybe I should upgrade to 3.1?) > > > That's because you need to promote one of them to a float so you get a > > floating-point result: > > >    >>> 1/2 * 1/2 > >    0 > >    >>> 1/2 * 1/2.0 > >    0.0 > > > Oh...wait ;-) > > Tim, I'm sure you know the answer to this, but for the benefit of the > Original Poster, the problem is that you need to promote *both* divisions > to floating point. Otherwise one of them will give int 0, which gives 0.0 > when multiplied by 0.5. > > >>> 1.0/2 * 1/2.0 > > 0.25 > > If you want an exact result when multiplying arbitrary fractions, you > need to avoid floats and decimals and use Fractions: > > >>> Fraction(1, 2)**2 > > Fraction(1, 4) Where do you get that from? > > -- > Steven- Hide quoted text - > > - Show quoted text - From: Dave Angel on 2 Apr 2010 18:11 Mensanator wrote:> On Apr 1, 9:44 pm, Steven D'Aprano cybersource.com.au> wrote: > >> >>>>> 1/2.0 >>>>> >> 0.25 >> >> If you want an exact result when multiplying arbitrary fractions, you >> need to avoid floats and decimals and use Fractions: >> >> >>>>> Fraction(1, 2)**2 >>>>> >> Fraction(1, 4) >> > > Where do you get that from? > > In Python2.6, from fractions import Fraction And Fraction is now a class which supports fractional arithmetic. First  |  Prev  |  Next  |  Last Pages: 1 2 3 4 5 6 7 8 9 10 11 12 Prev: free proxy & fastNext: C-style static variables in Python?