deleting operators to forbid mixed arithmetic? [multiprecision]

Hi, I'd like to forbid multiplication of Boost.Multiprecision floating point numbers by doubles. Any tips? I think I want to delete a family of operator*s, but am having a hard time coming up with which ones. Thanks, Dani Brake University of Wisconsin Eau Claire

From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Dani Brake via Boost-users Sent: 12 August 2017 00:54 To: boost-users@lists.boost.org Cc: Dani Brake Subject: [Boost-users] deleting operators to forbid mixed arithmetic? [multiprecision] Hi, I'd like to forbid multiplication of Boost.Multiprecision floating point numbers by doubles. Any tips? I think I want to delete a family of operator*s, but am having a hard time coming up with which ones. Thanks, Dani Brake University of Wisconsin Eau Claire Been there, done that – fallen into the big pit awaiting the unwary mixing double and multiprecision – again and again… L I think that you don’t really want to delete a family of operator*s – what you DO want to get a warning when there is loss of significance, similar to the warning when you fit a double into a float. It’s OK to multiply (or other ops) with a double as long as this doesn’t lose any bits or digits of precision. So multiplying by 0.5 is OK, but not by multiplying 0.1 (the multiprecision type needs FP_type(1)/10 to represent 0.1 as accurately as possible for the FP type). If only the compiler provided a trait bool is_exactly_representable(double) so that a warning could be issued… The compiler knows … Paul --- Paul A. Bristow Prizet Farmhouse Kendal UK LA8 8AB +44 (0) 1539 561830

On 12/08/2017 00:53, Dani Brake via Boost-users wrote:
Hi, I'd like to forbid multiplication of Boost.Multiprecision floating point numbers by doubles. Any tips? I think I want to delete a family of operator*s, but am having a hard time coming up with which ones.
All the operators are enable_if'ed on "is_compatible_arithmetic_type<V, M>::value" where V is the arithmetic type and M is the multiprecision type. The trait is defined in boost/multiprecision/detail/number_base.hpp as: template <class T, class Num> struct is_compatible_arithmetic_type : public mpl::bool_< is_convertible<T, Num>::value && !is_same<T, Num>::value && !is_number_expression<T>::value> {}; So in principle you just define a full or partial specialization, for example: template <class Backend> struct is_compatible_arithmetic_type<double, number<Backend> > : public mpl::false_ {}; and then cross your fingers and hope for the best ;) This is untried, and it's quite likely there will be collateral damage - things which should compile but don't any more because they multiply by 0.5 or something. HTH, John. --- This email has been checked for viruses by AVG. http://www.avg.com
participants (3)
-
Dani Brake
-
John Maddock
-
Paul A. Bristow