boostorg / multiprecision

Boost.Multiprecision
Boost Software License 1.0
194 stars 111 forks source link

Computing the square of a number #547

Open afabri opened 1 year ago

afabri commented 1 year ago

I am wondering if there is a function for computing the square of an integer. I ask because GMP has mpn_sqr() .

ckormanyos commented 1 year ago

If you have a large number of limbs, you can improve efficiencs by computing only half the triad of coefficients and doubling them - or something like that. I believe this reduces the computational complexity.

Every time that i try for it i miss something and multiprecision fails. but I#m really prone to typos and mistakes.

I would almost want to take this on in a GSoC 24 with some other optimizations unless it gets done sooner.

On a practical note, it is a bit harder to see the clean C++ interface to catch many use cases. You could provide a squaring functoin and clients knowing they need a square could use it. internally i think you could see if &left == &right in left * right = result, but I'm not sure if that is robust enough.