Expression Handling in GMP

I recently became familiar with the GMP library for high precision arithmetic. This seems simple enough, but in my first program I run into practical problems. How to evaluate expressions. For example, if I have "1 + 8 * z ^ 2" and z is the "large integer" mpz_t variable, how can I quickly evaluate it? (I have large expressions in a program I am writing.) I currently perform each individual operation manually and store the results in temporary variables, like this for the expression "1 + 8 * z ^ 2":

1) first let's make mpt_mul (z, z, z) into square z

2), then define the variable mpz_t named "figure eight" with the value 8.

3) multiply the result from the first step by this 8 and store it in the temp variable.

4) define the mpz_t variable called "one" with the value 1.

5) add this to the result in step 3 to find the final answer.

Is this what I have to do? Or is there a better way? It would really help if there was a GMP user guide for people to get started, but it was only a reference guide.

+2


source to share


2 answers


GMP comes with a C ++ class interface that provides an easier way to express arithmetic expressions. This interface uses C ++ operator overloading so that you can write:

mpz_class z;
1 + 8 * z**2

      



This assumes, of course, that you are using C ++. If you are only using C, you may need to use the GMP C interface, which does not provide operator overloading.

+3


source


It turns out that there is an unsupported expression parser that is distributed with GMP in the "expr" subdirectory. It is not part of GMP and is subject to change, but it is discussed in the README file in this directory. It is not guaranteed to complete the settlement in the fastest way, therefore the buyer is wary.



So the user has to manually evaluate all expressions when using GMP if they don't want to use this library or make their own parser.

+2


source







All Articles