This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Implement floordiv operator for gdb.Value


On 20/09/16 16:33 +0100, Pedro Alves wrote:
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?

OK - if I do that then floor(d) will convert it back to double,
unless you #include <cmath> and using std::floor, so that the overload
for long double is visible (in C++ <math.h> names like floor are
overloaded so you don't need to use floorf/floor/floorl according to
the type).

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

This seems to be an existing property of gdb.Value, as even using the
normal division operator (and without my patch) I see floats printed
without a decimal part when they are an integer value:

(gdb) python print (gdb.Value(5.0)/5.0)
1
(gdb) python print (5.0/5.0)
1.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

Good point, I'll do that.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]