Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Implement floordiv operator for gdb.Value
Date: Tue, 20 Sep 2016 15:41:00 -0000	[thread overview]
Message-ID: <883c76d5-54e9-e8fe-5713-eec2c4010498@redhat.com> (raw)
In-Reply-To: <20160920132633.GA897@redhat.com>

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


  parent reply	other threads:[~2016-09-20 15:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-20 13:34 Jonathan Wakely
2016-09-20 14:46 ` Jonathan Wakely
2016-09-20 15:41 ` Pedro Alves [this message]
2016-09-20 16:41   ` Jonathan Wakely
2016-09-20 16:42     ` Jonathan Wakely
2016-09-20 17:01     ` Pedro Alves
2016-09-20 17:11       ` Jonathan Wakely
2016-09-20 17:08     ` Paul.Koning
2016-09-20 18:20       ` Jonathan Wakely
2016-09-20 19:06         ` Paul.Koning

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=883c76d5-54e9-e8fe-5713-eec2c4010498@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=jwakely@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox