Question

Let say I have a snippet of code like this:

typedef double My_fp_t;

My_fp_t my_fun( My_fp_t input )
{
// some fp computation, it uses operator+, operator- and so on for type My_fp_t
}

My_fp_t input = 0.;
My_fp_t output = my_fun( input );

Is it possible to retrofit my existing code with a floating point arbitrary precision C++ library?

I would like to simple add #include <cpp_arbitrary_precision_fp>, change my typedef double My_fp_t; into typedef arbitrary_double_t My_fp_t; and let the operator overloading of C++ doing its job...

My main problem is that actually my code do NOT have the typedef :-( and so maybe my plan is doomed to failure.

Assuming that my code had the typedef, what other problems would I face?

Was it helpful?

Solution

This might be tough. I used a template approach in my PhD thesis code do deal with different numerical types. You might want to take a look at it to see the problems I encountered.

The thing is you are fine if all you do with your numbers is use the standard arithmetic operators. However, as soon as you use a square root or some other non operator function you need to create helper objects to detect your object's type (at compile time as it is too slow to do this at run time; see the boost metaprogramming library for help on that) and then call the correct function and return it as the correct type. It is all totally doable, but is likely to take longer than you think and will add considerably to the complexity of your code.

In my experience, (I was using GMP which must be the fastest arbitrary precision library available for C++) after all of the effort and complexity I had introduced, I found that GMP was just too slow for the sorts of computation that I was doing; so it was academically interesting, but practically useless. Before you start on this do some speed tests to see whether your library will still be usable if you use arbitrary precision arithmetic.

OTHER TIPS

If the library defines a type that correctly overloads the operators you use, I don't see any problem...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top