Search code examples
cprecisiongmp

Gmp mpf_pow_ui function


I am using gmp's mpf_t to try and get very high precision.

My range of precision ranges from negative trillions to positive trillions, as well as 1 over these numbers. However mpf doesn't support a power function that allows negative exponents, is there a way to get around this if I want to raise my value to 10^-30?

http://gmplib.org/manual/Float-Arithmetic.html#Float-Arithmetic

my gdp output when I try to use mpf_pow_ui, when my exp is negative:

(gdb) p exp_multiplier
$9 = {{_mp_prec = 2, _mp_size = 3, _mp_exp = 957480584338323631, _mp_d = 0x605070}}

This eventually will cause a segfault.


Solution

  • When in doubt, apply math:

    10^-30 = 1 / 10^30
    

    Just raise it to the positive power and take the reciprocal.

    There's a division function mpf_ui_div() that takes an integer numerator for that.