You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@arrow.apache.org by Hirokazu Suzuki <he...@gmail.com> on 2023/04/09 00:03:34 UTC

[DISCUSSION][C++] Re-proposal of 'divmod' in Compute Functions

Hello all,

This is a (re)proposal for a divmod kernel for the compute function.
This proposes the following two combinations of quotient and remainder for
'divmod'.

- [floor-quotient, modulo]
- [trunc-quotient, remainder]

The first proposal for divmod was made in ARROW-12755 [1] and has been
 discussed in GH-11116 [2], but has not yet been completed.

On the other hand, the following relationships hold for the dividend,
quotient, divisor,
and modulo,

  quotient ∈ Z
  dividend = quotient * divisor + modulo,
  |modulo| < |divisor|

Since the sign of modulo is undefined in this expression, each language has
a different
 behavior regarding the sign of the remainder when a negative number is
involved.

For example, in C, C++, Go, Java, Rust, etc., the remainder will match the
sign
 of the divisor. This is equivalent to performing a "truncation division to
zero: trunc"
 when finding the quotient.

On the other hand, R yields a remainder that matches the sign of the
divisor,
which is equivalent to performing "truncated division to infinity: floor"
when finding
 the quotient.

In addition, some languages provide both of these, such as Julia and Ruby.

The following table lists the signs and operators for integer remainders.

Table: Language, operator and operands
 which sign is corresponding to the sign of remainder

|Language |dividend    |divisor        |
|:--------|:-----------|:--------------|
| C       | %, div     |               |
| C++     | %, div     |               |
| C#      | %          |               |
| Go      | %          |               |
| Java    | %          | Math.floorMod |
| Julia   | rem        | mod           |
| MATLAB  | rem        | mod           |
| Python  | math.fmod  | %             |
| R       |            | %%            |
| Ruby    | remainder  | %, modulo     |
| Rust    | %          |               |
| Scala   | %          |               |
| Scheme  | remainder  | modulo        |
| SQL     | mod        |               |

(The remainder of floating point numbers is omitted.)

Since both styles exist, it is necessary to name and implement compute
functions
 in a way avoiding confusion that may be used by many languages.

So I propose to create two different divmods sets of quotient rounding
 (trunc or floor) and remainders.

- trunc_divmod ≡ [trunc_quotient, remainder]
- floor_divmod ≡ [floor_quotient, modulo]

It is inconvenient without the kernel, at least for positive number.
So I put this proposal here to step the work forward.

I am not an expert in this field and there may be some misunderstandings.
 I would appreciate it if you could point out any mistakes.

[1] https://issues.apache.org/jira/browse/ARROW-12755)
[2] https://github.com/apache/arrow/pull/11116)

Best regards,
Hirokazu SUZUKI

Re: [DISCUSSION][C++] Re-proposal of 'divmod' in Compute Functions

Posted by Hirokazu Suzuki <he...@gmail.com>.
@kou Thank you for the comment. I will create issue about this.

2023年5月10日(水) 9:43 Sutou Kouhei <ko...@clear-code.com>:

> Hi,
>
> There is no objection for this proposal.
> Could you write this proposal to
> https://github.com/apache/arrow/issues/28497 too?
>
> Then we can move forward (implement) this proposal.
>
> Thanks,
> --
> kou
>
> In <CA...@mail.gmail.com>
>   "[DISCUSSION][C++] Re-proposal of 'divmod' in Compute Functions" on Sun,
> 9 Apr 2023 09:03:34 +0900,
>   Hirokazu Suzuki <he...@gmail.com> wrote:
>
> > Hello all,
> >
> > This is a (re)proposal for a divmod kernel for the compute function.
> > This proposes the following two combinations of quotient and remainder
> for
> > 'divmod'.
> >
> > - [floor-quotient, modulo]
> > - [trunc-quotient, remainder]
> >
> > The first proposal for divmod was made in ARROW-12755 [1] and has been
> >  discussed in GH-11116 [2], but has not yet been completed.
> >
> > On the other hand, the following relationships hold for the dividend,
> > quotient, divisor,
> > and modulo,
> >
> >   quotient ∈ Z
> >   dividend = quotient * divisor + modulo,
> >   |modulo| < |divisor|
> >
> > Since the sign of modulo is undefined in this expression, each language
> has
> > a different
> >  behavior regarding the sign of the remainder when a negative number is
> > involved.
> >
> > For example, in C, C++, Go, Java, Rust, etc., the remainder will match
> the
> > sign
> >  of the divisor. This is equivalent to performing a "truncation division
> to
> > zero: trunc"
> >  when finding the quotient.
> >
> > On the other hand, R yields a remainder that matches the sign of the
> > divisor,
> > which is equivalent to performing "truncated division to infinity: floor"
> > when finding
> >  the quotient.
> >
> > In addition, some languages provide both of these, such as Julia and
> Ruby.
> >
> > The following table lists the signs and operators for integer remainders.
> >
> > Table: Language, operator and operands
> >  which sign is corresponding to the sign of remainder
> >
> > |Language |dividend    |divisor        |
> > |:--------|:-----------|:--------------|
> > | C       | %, div     |               |
> > | C++     | %, div     |               |
> > | C#      | %          |               |
> > | Go      | %          |               |
> > | Java    | %          | Math.floorMod |
> > | Julia   | rem        | mod           |
> > | MATLAB  | rem        | mod           |
> > | Python  | math.fmod  | %             |
> > | R       |            | %%            |
> > | Ruby    | remainder  | %, modulo     |
> > | Rust    | %          |               |
> > | Scala   | %          |               |
> > | Scheme  | remainder  | modulo        |
> > | SQL     | mod        |               |
> >
> > (The remainder of floating point numbers is omitted.)
> >
> > Since both styles exist, it is necessary to name and implement compute
> > functions
> >  in a way avoiding confusion that may be used by many languages.
> >
> > So I propose to create two different divmods sets of quotient rounding
> >  (trunc or floor) and remainders.
> >
> > - trunc_divmod ≡ [trunc_quotient, remainder]
> > - floor_divmod ≡ [floor_quotient, modulo]
> >
> > It is inconvenient without the kernel, at least for positive number.
> > So I put this proposal here to step the work forward.
> >
> > I am not an expert in this field and there may be some misunderstandings.
> >  I would appreciate it if you could point out any mistakes.
> >
> > [1] https://issues.apache.org/jira/browse/ARROW-12755)
> > [2] https://github.com/apache/arrow/pull/11116)
> >
> > Best regards,
> > Hirokazu SUZUKI
>

Re: [DISCUSSION][C++] Re-proposal of 'divmod' in Compute Functions

Posted by Sutou Kouhei <ko...@clear-code.com>.
Hi,

There is no objection for this proposal.
Could you write this proposal to
https://github.com/apache/arrow/issues/28497 too?

Then we can move forward (implement) this proposal.

Thanks,
-- 
kou

In <CA...@mail.gmail.com>
  "[DISCUSSION][C++] Re-proposal of 'divmod' in Compute Functions" on Sun, 9 Apr 2023 09:03:34 +0900,
  Hirokazu Suzuki <he...@gmail.com> wrote:

> Hello all,
> 
> This is a (re)proposal for a divmod kernel for the compute function.
> This proposes the following two combinations of quotient and remainder for
> 'divmod'.
> 
> - [floor-quotient, modulo]
> - [trunc-quotient, remainder]
> 
> The first proposal for divmod was made in ARROW-12755 [1] and has been
>  discussed in GH-11116 [2], but has not yet been completed.
> 
> On the other hand, the following relationships hold for the dividend,
> quotient, divisor,
> and modulo,
> 
>   quotient ∈ Z
>   dividend = quotient * divisor + modulo,
>   |modulo| < |divisor|
> 
> Since the sign of modulo is undefined in this expression, each language has
> a different
>  behavior regarding the sign of the remainder when a negative number is
> involved.
> 
> For example, in C, C++, Go, Java, Rust, etc., the remainder will match the
> sign
>  of the divisor. This is equivalent to performing a "truncation division to
> zero: trunc"
>  when finding the quotient.
> 
> On the other hand, R yields a remainder that matches the sign of the
> divisor,
> which is equivalent to performing "truncated division to infinity: floor"
> when finding
>  the quotient.
> 
> In addition, some languages provide both of these, such as Julia and Ruby.
> 
> The following table lists the signs and operators for integer remainders.
> 
> Table: Language, operator and operands
>  which sign is corresponding to the sign of remainder
> 
> |Language |dividend    |divisor        |
> |:--------|:-----------|:--------------|
> | C       | %, div     |               |
> | C++     | %, div     |               |
> | C#      | %          |               |
> | Go      | %          |               |
> | Java    | %          | Math.floorMod |
> | Julia   | rem        | mod           |
> | MATLAB  | rem        | mod           |
> | Python  | math.fmod  | %             |
> | R       |            | %%            |
> | Ruby    | remainder  | %, modulo     |
> | Rust    | %          |               |
> | Scala   | %          |               |
> | Scheme  | remainder  | modulo        |
> | SQL     | mod        |               |
> 
> (The remainder of floating point numbers is omitted.)
> 
> Since both styles exist, it is necessary to name and implement compute
> functions
>  in a way avoiding confusion that may be used by many languages.
> 
> So I propose to create two different divmods sets of quotient rounding
>  (trunc or floor) and remainders.
> 
> - trunc_divmod ≡ [trunc_quotient, remainder]
> - floor_divmod ≡ [floor_quotient, modulo]
> 
> It is inconvenient without the kernel, at least for positive number.
> So I put this proposal here to step the work forward.
> 
> I am not an expert in this field and there may be some misunderstandings.
>  I would appreciate it if you could point out any mistakes.
> 
> [1] https://issues.apache.org/jira/browse/ARROW-12755)
> [2] https://github.com/apache/arrow/pull/11116)
> 
> Best regards,
> Hirokazu SUZUKI