vendredi 4 janvier 2019

boost::multiprecision: What's the cheapest way to multiply or divide by a huge power of 10? Something like bitshift op for powers of 10?

Consider the following MCVE:

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>

int main()
{
    boost::multiprecision::cpp_int x = 10;
    x *= 10000000000000000000000000000000000000000000000000000000000000;
    std::cout<<x<<std::endl;
    return 0;
}

It produces a wrong result, due to the obvious overflow from that int. How can I do this correctly, assuming I don't want to involve strings? Is there something like a "digit shift operator" or a power function that can do this cheaply (or cheapest possible)?

Why? Because I have a fixed-precision library that I wrote, and scaling the internal integer requires such operations to be 100% safe.

Find the example here.

Aucun commentaire:

Enregistrer un commentaire