From: David Robinow on
On Thu, Apr 1, 2010 at 10:44 PM, Steven D'Aprano
<steve(a)remove-this-cybersource.com.au> 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)

I should have known he wouldn't get it.
From: Tim Chase on
Steven D'Aprano wrote:
>> 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

You can get away with just promoting one of them...you just have
to promote the _correct_ one (one involved in the first division)
so that its promotion-of-subresult-to-float carries into all
subsequent operations/operators:

>>> 1/2 * 1/2 # (((1/2)*1)/2)==(((0)*1)/2) in 2.x
0
>>> 1/2 * 1/2.0 # (((1/2)*1)/2.0)==(((0)*1)/2.0) in 2.x
0.0
>>> 1/2 * 1.0/2 # (((1/2)*1.0)/2)==(((0)*1.0)/2) in 2.x
0.0
>>> 1/2.0 * 1/2 # (((1/2.0)*1)/2)
0.25
>>> 1.0/2 * 1/2 # (((1.0/2)*1)/2)
0.25

I'd rather be explicit in *real* code that I'd write and
explicitly float'ify constants or float() integer variables. The
OP's question was both OT and pretty basic middle-school math
that google would have nicely answered[1] so IMHO warranted a bit
of fun. :)

-tkc

[1]
http://www.google.com/search?q=1%2F2+*+1%2F2



From: Patrick Maupin on
On Apr 1, 11:52 pm, Dennis Lee Bieber <wlfr...(a)ix.netcom.com> wrote:
> On Thu, 01 Apr 2010 22:44:51 +0200, superpollo <ute...(a)esempio.net>
> 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
From: Steven D'Aprano on
On Thu, 01 Apr 2010 22:34:46 -0500, Tim Chase wrote:

>> 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
>
> You can get away with just promoting one of them...you just have to
> promote the _correct_ one

Doh!

Of course you do. I knew that!



--
Steven
From: Wanderer on
On Apr 1, 7:34 pm, Patrick Maupin <pmau...(a)gmail.com> wrote:
> On Apr 1, 4:42 pm, Tim Chase <python.l...(a)tim.thechases.com> wrote:

> > Uh, did you try it at the python prompt?  


When I try it at the IPython prompt, I get

Object 'how much is one half times one half' not found.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11 12
Prev: free proxy & fast
Next: C-style static variables in Python?