Topic: Should GCD/LCM take variable numbers of arguments?
Author: Daryle Walker <darylew@gmail.com>
Date: Sat, 22 Aug 2015 21:51:08 -0700 (PDT)
Raw View
------=_Part_834_813212632.1440305468854
Content-Type: multipart/alternative;
boundary="----=_Part_835_2132862396.1440305468855"
------=_Part_835_2132862396.1440305468855
Content-Type: text/plain; charset=UTF-8
I recently saw descriptions of GCD and LCM as part of the Library
Fundamentals v2 paper (N4529), the original paper (N4601), and
cppreference.com. I'm wondering if those functions should take variable
numbers of arguments. I mean like:
<typename T>
constexpr T gcd(T x); // returns |x|
<typename T, typename U>
constexpr common_type_t<T, U> gcd(T x, U y); // current definition
<typename T, typename U, typename V, typename... W>
constexpr common_type_t<T, U, V, W...> gcd(T x, U y, V z, W ...a); //
returns gcd(x, gcd(y, z, a...))
// And similar for "lcm"
Good idea?...
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_835_2132862396.1440305468855
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I recently saw descriptions of GCD and LCM as part of the =
Library Fundamentals v2 paper (N4529), the original paper (N4601), and cppr=
eference.com. I'm wondering if those functions should take variable num=
bers of arguments. I mean like:<div><br></div><div class=3D"prettyprint" st=
yle=3D"background-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187=
, 187); word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"s=
ubprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">&l=
t;typename</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">T</span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">></span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"><br>constexpr T gcd(T x)=
; =C2=A0// returns |x|<br></span><span style=3D"color: #008;" class=3D"styl=
ed-by-prettify"><typename</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-p=
rettify">T</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
, </span><span style=3D"color: #606;" class=3D"styled-by-prettify">typename=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #606;" class=3D"styled-by-prettify">U</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">></span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br>constexpr common_type_t</span>=
<span style=3D"color: #008;" class=3D"styled-by-prettify"><T</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify">, </span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">U</span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">></span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> gcd(T x, U y); =C2=A0// current definition=
<br></span><span style=3D"color: #008;" class=3D"styled-by-prettify"><ty=
pename</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #606;" class=3D"styled-by-prettify">T</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify">, </span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">typename</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #606;" class=3D"styled-by-prettify">U</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify">, </span><span style=3D"color: #606;" class=3D=
"styled-by-prettify">typename</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-=
prettify">V</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>, </span><span style=3D"color: #606;" class=3D"styled-by-prettify">typenam=
e</span><span style=3D"color: #000;" class=3D"styled-by-prettify">... </spa=
n><span style=3D"color: #606;" class=3D"styled-by-prettify">W</span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">></span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br>constexpr common_type_t<=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify"><T</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">, </span><span s=
tyle=3D"color: #606;" class=3D"styled-by-prettify">U</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">, </span><span style=3D"color: #6=
06;" class=3D"styled-by-prettify">V</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">, </span><span style=3D"color: #606;" class=3D"sty=
led-by-prettify">W</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">...</span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>></span><span style=3D"color: #000;" class=3D"styled-by-prettify"> gcd(=
T x, U y, V z, W ...a); =C2=A0// returns gcd(x, gcd(y, z, a...))<br><br><br=
>// And similar for "lcm"<br></span></div></code></div><div><br><=
br></div><div>Good idea?...</div><div><br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_835_2132862396.1440305468855--
------=_Part_834_813212632.1440305468854--
.
Author: Myriachan <myriachan@gmail.com>
Date: Wed, 26 Aug 2015 12:42:35 -0700 (PDT)
Raw View
------=_Part_5977_1731344511.1440618155833
Content-Type: multipart/alternative;
boundary="----=_Part_5978_1705726041.1440618155834"
------=_Part_5978_1705726041.1440618155834
Content-Type: text/plain; charset=UTF-8
On Saturday, August 22, 2015 at 9:51:09 PM UTC-7, Daryle Walker wrote:
>
> I recently saw descriptions of GCD and LCM as part of the Library
> Fundamentals v2 paper (N4529), the original paper (N4601), and
> cppreference.com. I'm wondering if those functions should take variable
> numbers of arguments. I mean like:
>
> <typename T>
> constexpr T gcd(T x); // returns |x|
> <typename T, typename U>
> constexpr common_type_t<T, U> gcd(T x, U y); // current definition
> <typename T, typename U, typename V, typename... W>
> constexpr common_type_t<T, U, V, W...> gcd(T x, U y, V z, W ...a); //
> returns gcd(x, gcd(y, z, a...))
>
>
> // And similar for "lcm"
>
>
> Good idea?...
>
>
That seems reasonable; implementations could potentially have a faster
version for multiple inputs if they wanted.
I'm wondering about what the return type ought to be, though. gcd(INT_MIN,
0) == -INT_MIN, which would be undefined if the return type is int.
Perhaps make_unsigned_t<common_type_t<T, U, V, W...>> ?
I don't know whether it's worthwhile, but we could define gcd and lcm for
zero and one parameter as well. gcd() == 0, lcm() == 1 I suppose; gcd(x)
== x and lcm(x) == x, obviously.
Melissa
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_5978_1705726041.1440618155834
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
On Saturday, August 22, 2015 at 9:51:09 PM UTC-7, Daryle Walker wrote:<bloc=
kquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-l=
eft: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">I recently saw des=
criptions of GCD and LCM as part of the Library Fundamentals v2 paper (N452=
9), the original paper (N4601), and <a href=3D"http://cppreference.com" tar=
get=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'http://www.=
google.com/url?q\75http%3A%2F%2Fcppreference.com\46sa\75D\46sntz\0751\46usg=
\75AFQjCNFfMEVsriPdiSLWG49XWK9zX_c5Ug';return true;" onclick=3D"this.hr=
ef=3D'http://www.google.com/url?q\75http%3A%2F%2Fcppreference.com\46sa\=
75D\46sntz\0751\46usg\75AFQjCNFfMEVsriPdiSLWG49XWK9zX_c5Ug';return true=
;">cppreference.com</a>. I'm wondering if those functions should take v=
ariable numbers of arguments. I mean like:<div><br></div><div style=3D"back=
ground-color:rgb(250,250,250);border:1px solid rgb(187,187,187);word-wrap:b=
reak-word"><code><div><span style=3D"color:#008"><typename</span><span s=
tyle=3D"color:#000"> </span><span style=3D"color:#606">T</span><span style=
=3D"color:#008">></span><span style=3D"color:#000"><br>constexpr T gcd(T=
x); =C2=A0// returns |x|<br></span><span style=3D"color:#008"><typename=
</span><span style=3D"color:#000"> </span><span style=3D"color:#606">T</spa=
n><span style=3D"color:#000">, </span><span style=3D"color:#606">typename</=
span><span style=3D"color:#000"> </span><span style=3D"color:#606">U</span>=
<span style=3D"color:#008">></span><span style=3D"color:#000"><br>conste=
xpr common_type_t</span><span style=3D"color:#008"><T</span><span style=
=3D"color:#000">, </span><span style=3D"color:#606">U</span><span style=3D"=
color:#008">></span><span style=3D"color:#000"> gcd(T x, U y); =C2=A0// =
current definition<br></span><span style=3D"color:#008"><typename</span>=
<span style=3D"color:#000"> </span><span style=3D"color:#606">T</span><span=
style=3D"color:#000">, </span><span style=3D"color:#606">typename</span><s=
pan style=3D"color:#000"> </span><span style=3D"color:#606">U</span><span s=
tyle=3D"color:#000">, </span><span style=3D"color:#606">typename</span><spa=
n style=3D"color:#000"> </span><span style=3D"color:#606">V</span><span sty=
le=3D"color:#000">, </span><span style=3D"color:#606">typename</span><span =
style=3D"color:#000">... </span><span style=3D"color:#606">W</span><span st=
yle=3D"color:#008">></span><span style=3D"color:#000"><br>constexpr comm=
on_type_t</span><span style=3D"color:#008"><T</span><span style=3D"color=
:#000">, </span><span style=3D"color:#606">U</span><span style=3D"color:#00=
0">, </span><span style=3D"color:#606">V</span><span style=3D"color:#000">,=
</span><span style=3D"color:#606">W</span><span style=3D"color:#000">...</=
span><span style=3D"color:#008">></span><span style=3D"color:#000"> gcd(=
T x, U y, V z, W ...a); =C2=A0// returns gcd(x, gcd(y, z, a...))<br><br><br=
>// And similar for "lcm"<br></span></div></code></div><div><br><=
br></div><div>Good idea?...</div><div><br></div></div></blockquote><div><br=
>That seems reasonable; implementations could potentially have a faster ver=
sion for multiple inputs if they wanted.<br><br>I'm wondering about wha=
t the return type ought to be, though.=C2=A0 gcd(INT_MIN, 0) =3D=3D -INT_MI=
N, which would be undefined if the return type is int.=C2=A0 Perhaps make_u=
nsigned_t<common_type_t<T, U, V, W...>> ?<br><br>I don't kn=
ow whether it's worthwhile, but we could define gcd and lcm for zero an=
d one parameter as well.=C2=A0 gcd() =3D=3D 0, lcm() =3D=3D 1 I suppose; gc=
d(x) =3D=3D x and lcm(x) =3D=3D x, obviously.<br><br>Melissa<br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_5978_1705726041.1440618155834--
------=_Part_5977_1731344511.1440618155833--
.
Author: Bengt Gustafsson <bengt.gustafsson@beamways.com>
Date: Thu, 27 Aug 2015 01:53:24 -0700 (PDT)
Raw View
------=_Part_192_584032461.1440665604607
Content-Type: multipart/alternative;
boundary="----=_Part_193_1002446060.1440665604607"
------=_Part_193_1002446060.1440665604607
Content-Type: text/plain; charset=UTF-8
I think this is a good idea. It should also be available for std::min and
std::max and possibly others. In C++11 we got min and max for
initializer_list<T> instead. Checking the reason for this may reveal
objections to a variadic template solution that apply to gcd and lcm as
well. As for the return type, why not just use auto except for the 2
parameter case?
Den onsdag 26 augusti 2015 kl. 21:42:36 UTC+2 skrev Myriachan:
>
> On Saturday, August 22, 2015 at 9:51:09 PM UTC-7, Daryle Walker wrote:
>>
>> I recently saw descriptions of GCD and LCM as part of the Library
>> Fundamentals v2 paper (N4529), the original paper (N4601), and
>> cppreference.com. I'm wondering if those functions should take variable
>> numbers of arguments. I mean like:
>>
>> <typename T>
>> constexpr T gcd(T x); // returns |x|
>> <typename T, typename U>
>> constexpr common_type_t<T, U> gcd(T x, U y); // current definition
>> <typename T, typename U, typename V, typename... W>
>> constexpr common_type_t<T, U, V, W...> gcd(T x, U y, V z, W ...a); //
>> returns gcd(x, gcd(y, z, a...))
>>
>>
>> // And similar for "lcm"
>>
>>
>> Good idea?...
>>
>>
> That seems reasonable; implementations could potentially have a faster
> version for multiple inputs if they wanted.
>
> I'm wondering about what the return type ought to be, though.
> gcd(INT_MIN, 0) == -INT_MIN, which would be undefined if the return type is
> int. Perhaps make_unsigned_t<common_type_t<T, U, V, W...>> ?
>
> I don't know whether it's worthwhile, but we could define gcd and lcm for
> zero and one parameter as well. gcd() == 0, lcm() == 1 I suppose; gcd(x)
> == x and lcm(x) == x, obviously.
>
> Melissa
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_193_1002446060.1440665604607
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I think this is a good idea. It should also be available f=
or std::min and std::max and possibly others. In C++11 we got min and max f=
or initializer_list<T> instead. Checking the reason for this may reve=
al objections to a variadic template solution that apply to gcd and lcm as =
well. As for the return type, why not just use auto except for the 2 parame=
ter case?<div><br><br>Den onsdag 26 augusti 2015 kl. 21:42:36 UTC+2 skrev M=
yriachan:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: =
0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Saturday, August 2=
2, 2015 at 9:51:09 PM UTC-7, Daryle Walker wrote:<blockquote class=3D"gmail=
_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div dir=3D"ltr">I recently saw descriptions of GCD and LCM a=
s part of the Library Fundamentals v2 paper (N4529), the original paper (N4=
601), and <a href=3D"http://cppreference.com" rel=3D"nofollow" target=3D"_b=
lank" onmousedown=3D"this.href=3D'http://www.google.com/url?q\75http%3A=
%2F%2Fcppreference.com\46sa\75D\46sntz\0751\46usg\75AFQjCNFfMEVsriPdiSLWG49=
XWK9zX_c5Ug';return true;" onclick=3D"this.href=3D'http://www.googl=
e.com/url?q\75http%3A%2F%2Fcppreference.com\46sa\75D\46sntz\0751\46usg\75AF=
QjCNFfMEVsriPdiSLWG49XWK9zX_c5Ug';return true;">cppreference.com</a>. I=
'm wondering if those functions should take variable numbers of argumen=
ts. I mean like:<div><br></div><div style=3D"background-color:rgb(250,250,2=
50);border:1px solid rgb(187,187,187);word-wrap:break-word"><code><div><spa=
n style=3D"color:#008"><typename</span><span style=3D"color:#000"> </spa=
n><span style=3D"color:#606">T</span><span style=3D"color:#008">></span>=
<span style=3D"color:#000"><br>constexpr T gcd(T x); =C2=A0// returns |x|<b=
r></span><span style=3D"color:#008"><typename</span><span style=3D"color=
:#000"> </span><span style=3D"color:#606">T</span><span style=3D"color:#000=
">, </span><span style=3D"color:#606">typename</span><span style=3D"color:#=
000"> </span><span style=3D"color:#606">U</span><span style=3D"color:#008">=
></span><span style=3D"color:#000"><br>constexpr common_type_t</span><sp=
an style=3D"color:#008"><T</span><span style=3D"color:#000">, </span><sp=
an style=3D"color:#606">U</span><span style=3D"color:#008">></span><span=
style=3D"color:#000"> gcd(T x, U y); =C2=A0// current definition<br></span=
><span style=3D"color:#008"><typename</span><span style=3D"color:#000"> =
</span><span style=3D"color:#606">T</span><span style=3D"color:#000">, </sp=
an><span style=3D"color:#606">typename</span><span style=3D"color:#000"> </=
span><span style=3D"color:#606">U</span><span style=3D"color:#000">, </span=
><span style=3D"color:#606">typename</span><span style=3D"color:#000"> </sp=
an><span style=3D"color:#606">V</span><span style=3D"color:#000">, </span><=
span style=3D"color:#606">typename</span><span style=3D"color:#000">... </s=
pan><span style=3D"color:#606">W</span><span style=3D"color:#008">></spa=
n><span style=3D"color:#000"><br>constexpr common_type_t</span><span style=
=3D"color:#008"><T</span><span style=3D"color:#000">, </span><span style=
=3D"color:#606">U</span><span style=3D"color:#000">, </span><span style=3D"=
color:#606">V</span><span style=3D"color:#000">, </span><span style=3D"colo=
r:#606">W</span><span style=3D"color:#000">...</span><span style=3D"color:#=
008">></span><span style=3D"color:#000"> gcd(T x, U y, V z, W ...a); =C2=
=A0// returns gcd(x, gcd(y, z, a...))<br><br><br>// And similar for "l=
cm"<br></span></div></code></div><div><br><br></div><div>Good idea?...=
</div><div><br></div></div></blockquote><div><br>That seems reasonable; imp=
lementations could potentially have a faster version for multiple inputs if=
they wanted.<br><br>I'm wondering about what the return type ought to =
be, though.=C2=A0 gcd(INT_MIN, 0) =3D=3D -INT_MIN, which would be undefined=
if the return type is int.=C2=A0 Perhaps make_unsigned_t<common_type_t&=
lt;<wbr>T, U, V, W...>> ?<br><br>I don't know whether it's wo=
rthwhile, but we could define gcd and lcm for zero and one parameter as wel=
l.=C2=A0 gcd() =3D=3D 0, lcm() =3D=3D 1 I suppose; gcd(x) =3D=3D x and lcm(=
x) =3D=3D x, obviously.<br><br>Melissa<br></div></blockquote></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_193_1002446060.1440665604607--
------=_Part_192_584032461.1440665604607--
.