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.
If you only need to do something with values of
ithat are zero modulo two, this is easily accomplished by initializingito zero (or other desired start value) and adding two to it in each iteration.If you need to iterate
ithrough all integers but selectively do just some code wheniis even or odd, then a way to do this is to keep both thempfrcounter and a separate ordinary variable:Another solution is to unroll the loop so two iterations are written out, while putting the code for each iteration into a function: