2018-06-14 Jonathan Wakely <jwakely@redhat.com>
+ LWG 3074 make scalar types non-deduced in valarray non-member functions
+ * include/bits/valarray_after.h (_DEFINE_EXPR_BINARY_FUNCTION): Change
+ scalar parameters to be a non-deduced context.
+ * include/std/valarray (_DEFINE_BINARY_OPERATOR): Likewise. Adjust
+ whitespace.
+ * testsuite/26_numerics/valarray/operators.cc: Test scalar operands.
+ * testsuite/26_numerics/valarray/transcend.cc: New.
+
* include/std/tuple (__cpp_lib_tuple_element_t, tuple_element_t):
Move back to <utility>.
* include/std/utility (__cpp_lib_tuple_element_t. tuple_element_t):
<listitem><para>Add noexcept.
</para></listitem></varlistentry>
+ <varlistentry xml:id="manual.bugs.dr3074"><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="&DR;#3074">3074</link>:
+ <emphasis>Non-member functions for <code>valarray</code> should only deduce from the <code>valarray</code>
+ </emphasis>
+ </term>
+ <listitem><para>Change scalar operands to be non-deduced context, so that
+ they will allow conversions from other types to the value_type.
+ </para></listitem></varlistentry>
+
</variablelist>
</section>
\
template<typename _Tp> \
inline _Expr<_BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp>, _Tp> \
- _Fun(const valarray<_Tp>& __v, const _Tp& __t) \
+ _Fun(const valarray<_Tp>& __v, \
+ const typename valarray<_Tp>::value_type& __t) \
{ \
typedef _BinClos<_UFun, _ValArray, _Constant, _Tp, _Tp> _Closure;\
return _Expr<_Closure, _Tp>(_Closure(__v, __t)); \
\
template<typename _Tp> \
inline _Expr<_BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp>, _Tp> \
- _Fun(const _Tp& __t, const valarray<_Tp>& __v) \
+ _Fun(const typename valarray<_Tp>::value_type& __t, \
+ const valarray<_Tp>& __v) \
{ \
typedef _BinClos<_UFun, _Constant, _ValArray, _Tp, _Tp> _Closure;\
return _Expr<_Closure, _Tp>(_Closure(__t, __v)); \
#define _DEFINE_VALARRAY_UNARY_OPERATOR(_Op, _Name) \
template<typename _Tp> \
- inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
+ inline typename valarray<_Tp>::template _UnaryOp<_Name>::_Rt \
valarray<_Tp>::operator _Op() const \
{ \
- typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
- typedef typename __fun<_Name, _Tp>::result_type _Rt; \
+ typedef _UnClos<_Name, _ValArray, _Tp> _Closure; \
+ typedef typename __fun<_Name, _Tp>::result_type _Rt; \
return _Expr<_Closure, _Rt>(_Closure(*this)); \
}
#define _DEFINE_BINARY_OPERATOR(_Op, _Name) \
template<typename _Tp> \
- inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
- typename __fun<_Name, _Tp>::result_type> \
+ inline _Expr<_BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp>, \
+ typename __fun<_Name, _Tp>::result_type> \
operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
{ \
- __glibcxx_assert(__v.size() == __w.size()); \
- typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
- typedef typename __fun<_Name, _Tp>::result_type _Rt; \
- return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
+ __glibcxx_assert(__v.size() == __w.size()); \
+ typedef _BinClos<_Name, _ValArray, _ValArray, _Tp, _Tp> _Closure; \
+ typedef typename __fun<_Name, _Tp>::result_type _Rt; \
+ return _Expr<_Closure, _Rt>(_Closure(__v, __w)); \
} \
\
template<typename _Tp> \
- inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
- typename __fun<_Name, _Tp>::result_type> \
- operator _Op(const valarray<_Tp>& __v, const _Tp& __t) \
+ inline _Expr<_BinClos<_Name, _ValArray,_Constant, _Tp, _Tp>, \
+ typename __fun<_Name, _Tp>::result_type> \
+ operator _Op(const valarray<_Tp>& __v, \
+ const typename valarray<_Tp>::value_type& __t) \
{ \
typedef _BinClos<_Name, _ValArray, _Constant, _Tp, _Tp> _Closure; \
- typedef typename __fun<_Name, _Tp>::result_type _Rt; \
- return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
+ typedef typename __fun<_Name, _Tp>::result_type _Rt; \
+ return _Expr<_Closure, _Rt>(_Closure(__v, __t)); \
} \
\
template<typename _Tp> \
- inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
- typename __fun<_Name, _Tp>::result_type> \
- operator _Op(const _Tp& __t, const valarray<_Tp>& __v) \
+ inline _Expr<_BinClos<_Name, _Constant, _ValArray, _Tp, _Tp>, \
+ typename __fun<_Name, _Tp>::result_type> \
+ operator _Op(const typename valarray<_Tp>::value_type& __t, \
+ const valarray<_Tp>& __v) \
{ \
- typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
- typedef typename __fun<_Name, _Tp>::result_type _Rt; \
- return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
+ typedef _BinClos<_Name, _Constant, _ValArray, _Tp, _Tp> _Closure; \
+ typedef typename __fun<_Name, _Tp>::result_type _Rt; \
+ return _Expr<_Closure, _Rt>(_Closure(__t, __v)); \
}
_DEFINE_BINARY_OPERATOR(+, __plus)
VERIFY( (u>=v)[0] == (1>=3) );
}
+void test03() // check binary operators with scalar operands
+{
+ std::valarray<int> u(1);
+ u[0]=1;
+ long v = 3; // LWG 3074 allows scalar operand to be different to value_type.
+
+ VERIFY( (u+ v)[0] == (1+ 3) );
+ VERIFY( (u- v)[0] == (1- 3) );
+ VERIFY( (u* v)[0] == (1* 3) );
+ VERIFY( (u/ v)[0] == (1/ 3) );
+ VERIFY( (u% v)[0] == (1% 3) );
+ VERIFY( (u^ v)[0] == (1^ 3) );
+ VERIFY( (u& v)[0] == (1& 3) );
+ VERIFY( (u| v)[0] == (1| 3) );
+ VERIFY( (u<<v)[0] == (1<<3) );
+ VERIFY( (u>>v)[0] == (1>>3) );
+ VERIFY( (u&&v)[0] == (1&&3) );
+ VERIFY( (u||v)[0] == (1||3) );
+ VERIFY( (u==v)[0] == (1==3) );
+ VERIFY( (u!=v)[0] == (1!=3) );
+ VERIFY( (u< v)[0] == (1< 3) );
+ VERIFY( (u> v)[0] == (1> 3) );
+ VERIFY( (u<=v)[0] == (1<=3) );
+ VERIFY( (u>=v)[0] == (1>=3) );
+
+ VERIFY( (v+ u)[0] == (3+ 1) );
+ VERIFY( (v- u)[0] == (3- 1) );
+ VERIFY( (v* u)[0] == (3* 1) );
+ VERIFY( (v/ u)[0] == (3/ 1) );
+ VERIFY( (v% u)[0] == (3% 1) );
+ VERIFY( (v^ u)[0] == (3^ 1) );
+ VERIFY( (v& u)[0] == (3& 1) );
+ VERIFY( (v| u)[0] == (3| 1) );
+ VERIFY( (v<<u)[0] == (3<<1) );
+ VERIFY( (v>>u)[0] == (3>>1) );
+ VERIFY( (v&&u)[0] == (3&&1) );
+ VERIFY( (v||u)[0] == (3||1) );
+ VERIFY( (v==u)[0] == (3==1) );
+ VERIFY( (v!=u)[0] == (3!=1) );
+ VERIFY( (v< u)[0] == (3< 1) );
+ VERIFY( (v> u)[0] == (3> 1) );
+ VERIFY( (v<=u)[0] == (3<=1) );
+ VERIFY( (v>=u)[0] == (3>=1) );
+}
+
int main()
{
test01();
test02();
- return 0;
+ test03();
}
--- /dev/null
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run }
+
+#include <valarray>
+#include <testsuite_hooks.h>
+
+bool eq(double d, double e)
+{
+ return (int)(d * 100) == (int)(e * 100);
+}
+
+void
+test01()
+{
+ std::valarray<double> v(2);
+ v[0] = -0.5;
+ v[1] = 0.25;
+ std::valarray<double> v_abs = abs(v);
+ VERIFY( v_abs[0] == 0.5 );
+ VERIFY( v_abs[1] == 0.25 );
+
+ std::valarray<double> v_acos = acos(v);
+ VERIFY( eq( v_acos[0], 2.09 ) );
+ VERIFY( eq( v_acos[1], 1.31 ) );
+
+ std::valarray<double> v_asin = asin(v);
+ VERIFY( eq( v_asin[0], -0.52 ) );
+ VERIFY( eq( v_asin[1], 0.25 ) );
+
+ std::valarray<double> v_atan = atan(v);
+ VERIFY( eq( v_atan[0], -0.46 ) );
+ VERIFY( eq( v_atan[1], 0.24 ) );
+
+ std::valarray<double> v2(2);
+ v2[0] = 4;
+ v2[1] = 3;
+ std::valarray<double> v_atan2 = atan2(v, v2);
+ VERIFY( eq( v_atan2[0], -0.12 ) );
+ VERIFY( eq( v_atan2[1], 0.08 ) );
+
+ v_atan2 = atan2(v, 4); // LWG 3074 allows mixed types
+ VERIFY( eq( v_atan2[0], -0.12 ) );
+ VERIFY( eq( v_atan2[1], 0.06 ) );
+
+ v_atan2 = atan2(4, v); // LWG 3074 allows mixed types
+ VERIFY( eq( v_atan2[0], 1.69 ) );
+ VERIFY( eq( v_atan2[1], 1.50 ) );
+
+ std::valarray<double> v_cos = cos(v);
+ VERIFY( eq( v_cos[0], 0.87 ) );
+ VERIFY( eq( v_cos[1], 0.96 ) );
+
+ std::valarray<double> v_cosh = cosh(v);
+ VERIFY( eq( v_cosh[0], 1.12 ) );
+ VERIFY( eq( v_cosh[1], 1.03 ) );
+
+ std::valarray<double> v_exp = exp(v);
+ VERIFY( eq( v_exp[0], 0.60 ) );
+ VERIFY( eq( v_exp[1], 1.28 ) );
+
+ std::valarray<double> v_log = log(v);
+ VERIFY( eq( v_log[1], -1.38 ) );
+
+ std::valarray<double> v_log10 = log10(v);
+ VERIFY( eq( v_log10[1], -0.60 ) );
+
+ std::valarray<double> v_pow = pow(v, v2);
+ VERIFY( eq( v_pow[0], 0.06 ) );
+ VERIFY( eq( v_pow[1], 0.01 ) );
+
+ v_pow = pow(v, 3); // LWG 3074 allows mixed types
+ VERIFY( eq( v_pow[0], -0.12 ) );
+ VERIFY( eq( v_pow[1], 0.01 ) );
+
+ v_pow = pow(4, v); // LWG 3074 allows mixed types
+ VERIFY( eq( v_pow[0], 0.5 ) );
+ VERIFY( eq( v_pow[1], 1.41 ) );
+}
+
+int
+main()
+{
+ test01();
+}