# deleting operators to forbid mixed arithmetic? [multiprecision]

7 messages
Open this post in threaded view
|
Report Content as Inappropriate

## 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 BrakeUniversity of Wisconsin Eau Claire _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: deleting operators to forbid mixed arithmetic? [multiprecision]

 From: Boost-users [mailto:[hidden email]] On Behalf Of Dani Brake via Boost-usersSent: 12 August 2017 00:54To: [hidden email]Cc: Dani BrakeSubject: [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 BrakeUniversity 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. BristowPrizet FarmhouseKendal UK LA8 8AB+44 (0) 1539 561830    _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: deleting operators to forbid mixed arithmetic? [multiprecision]

 In reply to this post by Boost - Users mailing list 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::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 struct is_compatible_arithmetic_type     : public mpl::bool_<           is_convertible::value           && !is_same::value           && !is_number_expression::value> {}; So in principle you just define a full or partial specialization, for example: template struct is_compatible_arithmetic_type > : 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_______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: deleting operators to forbid mixed arithmetic? [multiprecision]

 In reply to this post by Boost - Users mailing list So in principle you just define a full or partial specialization, for example:template struct is_compatible_arithmetic_type > : public mpl::false_ {};and then cross your fingers and hope for the best ;)Thanks for the suggestion, i'll give it a try.  \e crosses fingers~dani _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: deleting operators to forbid mixed arithmetic? [multiprecision]

 In reply to this post by Boost - Users mailing list Been there, done that – fallen into the big pit awaiting the unwary mixing double and multiprecision – again and again… LI am being bit by this one hard.  Converted a bunch of multipliers and tolerances from mpfr_floats to doubles in an effort to simplify some code, and now mixed precision is introducing error. 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).Multiplying by 0.1 is a common desire in my household (adaptive precision solving of polynomials). If only the compiler provided a trait bool is_exactly_representable(double) so that a warning could be issued…This is a bit tricky, because any double is by definition exactly representable by a double, right?.  You would want is_exactly_representable to get the value before being transformed into a double? Another issue I foresee having to deal with is runtime parsing.  Forbid the runtime user from feeding doubles problematic numbers. Thanks for the help,Dani  _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: deleting operators to forbid mixed arithmetic? [multiprecision]

 In reply to this post by Boost - Users mailing list   From: Boost-users [mailto:[hidden email]] On Behalf Of Dani Brake via Boost-usersSent: 13 August 2017 18:39To: [hidden email]Cc: Dani BrakeSubject: Re: [Boost-users] deleting operators to forbid mixed arithmetic? [multiprecision] So in principle you just define a full or partial specialization, for example:template struct is_compatible_arithmetic_type > : public mpl::false_ {};and then cross your fingers and hope for the best ;) Thanks for the suggestion, i'll give it a try.  \e crosses fingers Putting a macro bracket around this might make it generally useful, perhaps something like: #ifdef BOOST_MULTIPRECISION_NO_DOUBLE_OPS  (BUILTIN_OPS would include float and long double?) … template struct is_compatible_arithmetic_type > : public mpl::false_ {}; say#endif If you find this useful, you could create a GIT Pull Request, or just email the file changed.  You won’t be the only one J.  Defining the macro BOOST_MULTIPRECISION_NO_DOUBLE_OPS could be a way to detect dodgy conversions that need to be changed to keep full precision. Good luck Paul ---Paul A. BristowPrizet FarmhouseKendal UK LA8 8AB+44 (0) 1539 561830     _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users