From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22748 invoked by alias); 20 Sep 2016 15:34:00 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 22731 invoked by uid 89); 20 Sep 2016 15:33:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-5.0 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=expecting, 6.0, toward, Hx-languages-length:3051 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 20 Sep 2016 15:33:58 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2D1F5C05A28E for ; Tue, 20 Sep 2016 15:33:57 +0000 (UTC) Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u8KFXtTv030464; Tue, 20 Sep 2016 11:33:56 -0400 Subject: Re: [PATCH] Implement floordiv operator for gdb.Value To: Jonathan Wakely , gdb-patches@sourceware.org References: <20160920132633.GA897@redhat.com> From: Pedro Alves Message-ID: <883c76d5-54e9-e8fe-5713-eec2c4010498@redhat.com> Date: Tue, 20 Sep 2016 15:41:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: <20160920132633.GA897@redhat.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2016-09/txt/msg00226.txt.bz2 Hi there, Thanks! On 09/20/2016 02:26 PM, Jonathan Wakely wrote: > This is my attempt to implement the // operator on gdb.Value objects. > There is already BINOP_INTDIV which works fine for integral types, but > for floats I use BINOP_DIV and then call floor() on the result. This > doesn't support decimal floats though. > > Is this a reasonable solution? Is the test sufficient? > See below. > @@ -1142,7 +1160,15 @@ valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other) > } > > if (res_val) > - result = value_to_value_object (res_val); > + { > + if (floor_it) > + { > + double d = value_as_double (res_val); Should be s/double/DOUBLEST, I suppose? > + d = floor (d); > + res_val = value_from_double (value_type (res_val), d); > + } > + result = value_to_value_object (res_val); > + } > > do_cleanups (cleanup); > return result; > @@ -1200,6 +1226,12 @@ valpy_remainder (PyObject *self, PyObject *other) > } > > static PyObject * > +valpy_floordiv (PyObject *self, PyObject *other) > +{ > + return valpy_binop (VALPY_FLOORDIV, self, other); > +} > + > +static PyObject * > valpy_power (PyObject *self, PyObject *other, PyObject *unused) > { > /* We don't support the ternary form of pow. I don't know how to express > @@ -1837,7 +1869,7 @@ static PyNumberMethods value_object_as_number = { > NULL, /* nb_inplace_and */ > NULL, /* nb_inplace_xor */ > NULL, /* nb_inplace_or */ > - NULL, /* nb_floor_divide */ > + valpy_floordiv, /* nb_floor_divide */ > valpy_divide, /* nb_true_divide */ > NULL, /* nb_inplace_floor_divide */ > NULL, /* nb_inplace_true_divide */ > diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp > index 57a9ba1..81837e9 100644 > --- a/gdb/testsuite/gdb.python/py-value.exp > +++ b/gdb/testsuite/gdb.python/py-value.exp > @@ -87,6 +87,8 @@ proc test_value_numeric_ops {} { > gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values" > gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values" > # Remainder of float is implemented in Python but not in GDB's value system. > + gdb_test "python print ('result = ' + str(i//j))" " = 2" "floor-divide two integer values" > + gdb_test "python print ('result = ' + str(f//g))" " = 0" "floor-divide two double values" Is the "two double values" test returning an integer somehow? I ask because IIUC, regardless of Python version, a floor-divide involving a float should result in a float, while a floor-divide of integers should result in an integer. And that's what the patch looks like should end up with. So I was expecting to see "0.0" in the "two double values" case: (gdb) python print (5.0//6.0) 0.0 (gdb) python print (5//6) 0 I think it'd be good to test with negative numbers too, to make sure that we round (and keep rounding) toward the same direction Python rounds: (gdb) python print (8.0//-3) -3.0 (gdb) python print (8//-3) -3 (gdb) print 8/-3 $1 = -2 Thanks, Pedro Alves