[DB-SIG] should precision and sign of decimal(0) be significant?

Chris Clark Chris.Clark at actian.com
Wed May 8 18:22:07 CEST 2013


On Wed, 08 May 2013 12:59:08 +0200, M.-A. Lemburg <mal at egenix.com> wrote:
> On 08.05.2013 11:48, Vernon D. Cole wrote:
>> I am working on code (django-mssql) where I found the following test:
>>
>> ##            expected = (
>> ##                Decimal('0.00'),
>> ##                Decimal('0.0'),
>> ##                Decimal('-0.00'))
>> ##
>> ##            cur = con.cursor()
>>
>> ##            cur.paramstyle = 'format'  # a nonstandard extension -- VDC
>>
>> ##            cur.execute("SELECT %s as A, %s as B, %s as C", expected)
>> ##
>> ##            result = cur.fetchall()
>> ##            self.assertEqual(result[0], expected)
>>
>> I added the paramstyle alteration, to match the original test's assumption
>> -- that is not what I am asking about.  My question is: is this test
>> reasonable on it's face?
>>
>> I personally think that all values of zero are equal, and the user should
>> have no expectation that neqative zero, or an accurately precise zero,
>> should be returned. My present code returns three copies of Decimal(0).
>>
>> But someone must disagree, or this test would not have been written.
>>
>> What is the feeling of the group?
> It's possible that the DB module passes decimals as
> strings to the database and the database could treat them
> differently in the resp. string form (e.g. raise an error
> in case the precision doesn't match).
>
> The test will pass either way, since all values compare
> equal.

To reiterate MA's reply.

It is DBMS/driver dependent on how these values are treated (and 
sent/retrieved to/from the DBMS). Most servers will thunk it down to a 
plain zero but you can't predict what each vendor does.

In python those are _different_ but as per MA's comment they compare the 
same:

    Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
     >>> from decimal import Decimal
     >>> Decimal('0.00') == Decimal('0.0') == Decimal('-0.00')
    True
     >>> Decimal('0.00')
    Decimal('0.00')
     >>> Decimal('0.0')
    Decimal('0.0')
     >>> Decimal('-0.00')
    Decimal('-0.00')

I personally have a bunch of similar tests to make sure the "different" 
values are handled correctly.

Chris



More information about the DB-SIG mailing list