How to write c language modulo 2 with use mpfr function?

61 views Asked by At

Here is the example of code in c language:

int i;

for (i=0; i < 100; i++)
{
if (i % 2 == 0) 
{
// do something
}
}

But i variable is standard integer. I need this code of modulo 2 with use mpfr_t type and proper function. MPFR library.

I could use while loop and increment i variable in the loop:

mpfr_t i;
mpfr_init2(i, 100);

mpfr_set_si (i, 0, MPFR_RNDD);

while(mpfr_cmpabs(i,100)<0)

{
mpfr_add_si(i, i, 1, MPFR_RNDD);

// how to write modulo 2 ?

}

Any help appreciated.

2

There are 2 answers

0
Eric Postpischil On

If you only need to do something with values of i that are zero modulo two, this is easily accomplished by initializing i to zero (or other desired start value) and adding two to it in each iteration.

If you need to iterate i through all integers but selectively do just some code when i is even or odd, then a way to do this is to keep both the mpfr counter and a separate ordinary variable:

mpfr_t i;
mpfr_init2(i, 100);

//  Define r to hold the residue of i modulo 2.
int r;

for (
    // Start loop with i = 0 and r = 0.
    mpfr_set_si(i, 0, MPFR_RNDD), r = 0;

    // Iterate until i reaches endpoint.
    mpfr_cmpabs(i, 100) < 0;

    // Increment both i and residue r.
    mpfr_add_si(i, i, 1, MPFR_RNDD), r = (r+1) % 2)
{
    // Put code for each iteration here.

    // Test whether the residue modulo 2 is 0, equivalent to i is even.
    {
        // Put code for when i is even here.
    }
}

Another solution is to unroll the loop so two iterations are written out, while putting the code for each iteration into a function:

mpfr_t i;
mpfr_init2(i, 100);

mpfr_set_si(i, 0, MPFR_RNDD);

while (mpfr_cmpabs(i, 100) < 0)
{
    //  Do things for a first value of i per loop.
    DoStuff(i);

    // Put code for when i is even here.

    //  Increment i to odd value.
    mpfr_add_si(i, i, 1, MPFR_RNDD);

    //  Do things for a second value of i per loop..
    DoStuff(i);

    //  Increment i to even value.
    mpfr_add_si(i, i, 1, MPFR_RNDD);
}
2
vinc17 On

Assuming that this is a general question (i.e. you are not necessarily in a loop like in your example) and you know that the variable i is an integer, then a solution could be to do mpfr_fmod_ui(r,i,2,MPFR_RNDN) with the output precision 1 for r (the rounding mode doesn't matter since the exact result is either 0 or 1, thus exactly representable in any precision).

An alternate solution (probably faster) would be to divide i by 2 with mpfr_div_2ui(i,i,1,MPFR_RNDN) and use mpfr_integer_p on the result: true means that the modulo is 0, and false means that the modulo is 1. You can restore i with mpfr_mul_2ui(i,i,1,MPFR_RNDN). Note that these functions are very fast when reusing the input as the result, since in this case, just the exponent is modified.