So i wrote a little extension with the help of php-cpp. In this function, i just simply calculate the pow, which works perfectly fine. After calculating the result, i return the string from the function. When i call this extensions function in php, i receive an integer which contains the strlen of my original string, instead of my string:
Php::Value Math::some_pow(Php::Parameters ¶ms)
{
mpfr_t base, exponent, result;
mpfr_set_emin(mpfr_get_emin_min());
mpfr_init2(base, 256);
mpfr_init2(exponent, 256);
mpfr_init2(result, 10);
mpfr_set_str(base, params[0], 10, GMP_RNDN);
mpfr_set_d(exponent, params[1], GMP_RNDN);
mpfr_pow(result, base, exponent, GMP_RNDN);
char data[255];
mpfr_snprintf(data, 254, "%.20Ff", result);
return data;
}
The mpfr_printf
output, to verify that everything inside the function works well:
base=1e+02 exponent=8.3999999999999996891375531049561686813831329345703125e-01
Result=4.7875e+01
So the function itself is supposed to return the following: Result=4.7875e+01
In PHP; calling the function, which looks like this:
$result = $math->some_pow(100, 0.84);
Output via var_dump($result);
shows 17 -> the strlen of "Result=4.7875e+01"
According to the docs (and comparing it to regular printf), your function works as expected:
mpfr_printf
returns the number of characters printed to the stdout.If you want to get the text as a string, instead of printing it to stdout, you need to use something like: