[BangPypers] A link I found today

Anand Balachandran Pillai abpillai at gmail.com
Mon Oct 12 12:39:43 CEST 2009


On Mon, Oct 12, 2009 at 4:06 PM, Anand Balachandran Pillai <
abpillai at gmail.com> wrote:

>
>
> On Mon, Oct 12, 2009 at 4:05 PM, Anand Balachandran Pillai <
> abpillai at gmail.com> wrote:
>
>>
>>
>> On Mon, Oct 12, 2009 at 4:02 PM, Anand Balachandran Pillai <
>> abpillai at gmail.com> wrote:
>>
>>>
>>>
>>> On Mon, Oct 12, 2009 at 3:47 PM, Anand Chitipothu <anandology at gmail.com>wrote:
>>>
>>>> On Mon, Oct 12, 2009 at 2:42 PM, Baiju Muthukadan <baiju at muthukadan.net>
>>>> wrote:
>>>> > http://bitcheese.net/wiki/nopython
>>>> >
>>>> > Don't start a flame war now, please ;)
>>>>
>>>> 2.3 - 3.4 and 2/3.0 in Python, Ruby and Haskell interpreters.
>>>>
>>>> $ python3.0
>>>> Python 3.0.1 (r301:69597, Feb 14 2009, 19:03:52)
>>>> [GCC 4.0.1 (Apple Inc. build 5490)] on darwin
>>>> Type "help", "copyright", "credits" or "license" for more information.
>>>> >>> 2.3 - 3.4
>>>> -1.1000000000000001
>>>> >>> 2/3.0
>>>> 0.66666666666666663
>>>>
>>>> $ irb
>>>> >> 2.3 - 3.4
>>>> => -1.1
>>>> >> 2/3.0
>>>> => 0.666666666666667
>>>> >> ^D
>>>>
>>>> $ ghci
>>>> GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
>>>> Loading package base ... linking ... done.
>>>> Prelude> 2.3 - 3.4
>>>> -1.1
>>>> Prelude> 2/3.0
>>>> 0.6666666666666666
>>>> Prelude> Leaving GHCi.
>>>>
>>>> It looks like number of decimal digits printed are 17 in Python, 16 in
>>>> Haskell and 15 in Ruby.
>>>>
>>>> Is there any way to change that behavior in Python?
>>>>
>>>
>>>  Not in the interpreter AFAIK. In code, use Decimal type.
>>>
>>> import decimal
>>>  >>> x=decimal.Decimal('2.3')
>>> >>> y=decimal.Decimal('3.4')
>>> >>> x-y
>>> Decimal("-1.1")
>>>
>>> I am not however a fan of the decimal module since it uses strings as the
>>>
>>> base type.
>>>
>>
>>  You do end up with quirks like the following however.
>>   >>> x=decimal.Decimal('2.3')
>> >>> y=decimal.Decimal('3.4')
>> >>> z=x-y
>> >>> z
>> Decimal("-1.1")
>> >>> str(z)
>> '-1.1'
>> >>> z>(2.2-3.4)
>> True
>>
>>  Oops, I meant,
>
> >>> z>(2.3-3.4)
> True
>
>
 Hmmm... apparently comparison with floats are not implemented
 for this type, so the above is not surprising..

Example.

# This is clearly false
 >>> z>-1.0
True

But,

# That is better!
>>> z>decimal.Decimal("-1.0")
False



>


-- 
--Anand
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20091012/c488e7bb/attachment.htm>


More information about the BangPypers mailing list