Do you know how to do this simple line of code without error using Boost::multiprecison ?
boost::multiprecision::cpp_int v, uMax, candidate;
//...
v += 6 * ceil((sqrt(uMax * uMax - candidate) - v) / 6);
Using MSVC there is an error for "sqrt" and it's possible to fix it with:
v += 6 * ceil((sqrt(static_cast<boost::multiprecision::cpp_int>(uMax * uMax - candidate)) - v) / 6);
Then there is an error for "ceil" and it's possible to fix it with:
namespace bmp = boost::multiprecision;
typedef bmp::number<bmp::cpp_dec_float<0>> float_bmp;
v += 6 * ceil(static_cast<float_bmp>((sqrt(static_cast<bmp::cpp_int>(uMax * uMax - candidate)) - v) / 6));
Then there is an error of "generic interconvertion" !?!
I think there should be a more elegant way to realize a so simple line of code, isn't it? Let me know if you have some ideas about it please.
Regards.
The "problem" (it's actually a feature) is that you are using the
number<>
frontend with template expressions enabled.This means that many operations can be greatly optimized or even eliminated before code is generated by the compiler.
You have two options:
break things down
So you could write
Which works by forcing evaluation of expression templates (and potentially lossy conversion from float -> integer using
convert_to<>
).In general you could switch to non-expression-template versions of the types:
In this particular case it doesn't change much because you still have to do type "coercions" from integer -> float -> integer:
By simplifying, if you make all types float instead (e.g. cpp_dec_float) you can get rid of these complicating artefacts:
Here's a demo program showing all three approaches:
Live On Coliru
Use
boost::multiprecision::sqrt
andboost::multiprecision::ceil
.