gmp

Multiplication between big integers and doubles

牧云@^-^@ 提交于 2019-12-11 02:13:29
问题 I am managing some big (128~256bits) integers with gmp. It has come a point were I would like to multiply them for a double close to 1 (0.1 < double < 10), the result being still an approximated integer. A good example of the operation I need to do is the following: int i = 1000000000000000000 * 1.23456789 I searched in the gmp documentation but I didn't find a function for this, so I ended up writing this code which seems to work well: mpz_mult_d(mpz_class & r, const mpz_class & i, double d,

GMP Bit shift doesn't work on negative numbers

十年热恋 提交于 2019-12-11 01:57:20
问题 I found this function at php.net. It seems to work on positive numbers, but fails on negative ones: function gmp_shiftr($x,$n) { // shift right return(gmp_div($x,gmp_pow(2,$n))); } echo -1 >> 8; //returns -1, presumably correctly echo "<br />"; echo gmp_strval(gmp_shiftr(-1,8)); //returns 0, presumably incorrectly How could I fix up the function to work with negatives? Two ideas I have: Maybe I could do something along the lines of if (whatever) { $a >> $b} else{ gmp_shiftr($a, $b) }? Or,

Undefined symbols error related to “__mpf_struct” for OS X 10.10.2

让人想犯罪 __ 提交于 2019-12-10 20:40:50
问题 I am trying to compile a program from https://github.com/davidsd/sdpb with gcc-4.9, boost 1.57.0, gmp-6.0.0a, and mpfr-3.1.2 on OS X 10.10.2, but I keep getting errors seemingly related to the gmp and mpfr packages. I know somebody who successfully compiled on 10.9.5. Can anybody suggest a fix? Undefined symbols for architecture x86_64: "operator<<(std::basic_ostream >&, __mpf_struct const*)", referenced from: operator<<(std::basic_ostream<char, std::char_traits<char> >&, Matrix const&) in

How does one calculate 2 ^ -18 using GMP?

懵懂的女人 提交于 2019-12-10 06:16:01
问题 I've just discovered, to my embarrassment, that feeding negative exponents to mpz_pow_ui doesn't work very well. ("The manual does say unsigned long, you know.") For the other mpz_pow functions, the manual uses concepts I don't understand. For example " base ^ exp mod mod " in the following: void mpz_powm (mpz_t rop, mpz_t base, mpz_t exp, mpz_t mod) void mpz_powm_ui (mpz_t rop, mpz_t base, unsigned long int exp, mpz_t mod) Set _rop_ to _base_^_exp_ mod _mod_. Negative exp is supported if an

Some questions about a single-instance array in typedef

跟風遠走 提交于 2019-12-10 03:36:45
问题 I was perusing some code using arbitrary-length integers using the GNU Multi-Precision (GMP) library code. The type for a MP integer is mpz_t as defined in gmp.h header file. But, I've some questions about the lower-level definition of this library-defined mpz_t type. In the header code: /* THIS IS FROM THE GNU MP LIBRARY gmp.h HEADER FILE */ typedef struct { /* SOME OTHER STUFF HERE */ } __mpz_struct; typedef __mpz_struct mpz_t[1]; First question: Does the [1] associate with the __mpz_struct

Calculating the n-th root of an integer using PHP/GMP

 ̄綄美尐妖づ 提交于 2019-12-08 00:07:00
问题 How can I calculate the n-th root of an integer using PHP/GMP? Although I found a function called gmp_root(a, nth) in the PHP source, it seems that this function has not been published in any release yet*: http://3v4l.org/8FjU7 *) 5.6.0alpha2 being the most recent one at the time of writing 回答1: Original source: Calculating Nth root with bcmath in PHP – thanks and credits to HamZa! I've rewritten the code to use GMP instead of BCMath: function gmp_nth_root($num, $n) { if ($n < 1) return 0; //

How to convert GMP C parameter convention into something more natural?

☆樱花仙子☆ 提交于 2019-12-07 21:44:42
问题 For example, I would like to do something like this: #include <gmp.h> typedef mpz_t Integer; // Integer F(Integer a,Integer b,Integer c,Integer d) { Integer ret = times(plus(a,b),plus(c,d)); } But, GMP doesn't let me do this, apparently mpz_t is an array, so I get the error: error: ‘F’ declared as function returning an array So instead I would have to do something like this: void F(Integer ret,Integer a,Integer b,Integer c,Integer d) { Integer tmp1,tmp2; plus(tmp1,a,b); plus(tmp2,c,d); times

GMP mpz_array_init is an obsolete function - How should we initialize mpz arrays?

二次信任 提交于 2019-12-07 07:23:51
问题 Having only used the GNU MP Bignum Library a few times, I was interested to see that the way I had previously allocated/initiated arrays is now obsolete. From Integer Special Functions: 5.16 Special Functions The functions in this section are for various special purposes. Most applications will not need them. — Function: void mpz_array_init (mpz_t integer_array, mp_size_t array_size, mp_size_t fixed_num_bits) This is an obsolete function. Do not use it. This is how I would allocate and

C++ gmp and homebrew

这一生的挚爱 提交于 2019-12-07 05:07:56
问题 I'm on a mac and I've installed gcc and gmp through homebrew . To test out my installation I've tried out the simple example from here: #include <iostream> #include <gmpxx.h> using namespace std; int main (void) { mpz_class a, b, c; a = 1234; b = "-5678"; c = a+b; cout << "sum is " << c << "\n"; cout << "absolute value is " << abs(c) << "\n"; return 0; } First of all, if I try g++ test.cpp -lgmpxx -lgmp it complains test.cpp:9:19: fatal error: gmpxx.h: No such file or directory #include

avoiding abort in libgmp

。_饼干妹妹 提交于 2019-12-07 03:37:24
问题 I have some code that uses libgmp. At some point the user may request a factorial of a very large number. Unfortunately, this results in libgmp raising an abort signal. For example the following code: #include <cmath> #include <gmp.h> #include <iostream> int main() { mpz_t result; mpz_init(result); mpz_fac_ui(result, 20922789888000); std::cout << mpz_get_si(result) << std::endl; } Results in: $ ./test gmp: overflow in mpz type Aborted Apparently, the number produced is REALLY big. Is there