Class: GMP::Z
- Inherits:
-
Integer
- Object
- Integer
- GMP::Z
- Defined in:
- ext/gmpz.c,
ext/gmp.c,
ext/gmpz.c,
ext/gmprandstate.c
Overview
GMP Multiple Precision Integer.
Instances of this class can store variables of the type ‘gmp_randstate_t`. This class also contains many methods that act as the functions for `gmp_randstate_t` variables, as well as a few methods that attempt to make this library more Ruby-ish.
The following list is just a simple checklist for me, really. A better reference should be found in the rdocs.
Ruby method C Extension function GMP function
new r_gmprandstatesg_new gmp_randinit_default
seed r_gmprandstate_seed gmp_randseed
\--- \------------------ gmp_randseed_ui
urandomb r_gmprandstate_urandomb mpz_urandomb
Class Method Summary collapse
- .2fac ⇒ Object
-
.abs ⇒ Object
call-seq: a.abs.
-
.add ⇒ Object
call-seq: GMP::Z.add(rop, op1, op2).
- .addmul ⇒ Object
- .cdiv_q_2exp ⇒ Object
- .cdiv_r_2exp ⇒ Object
-
.com ⇒ Object
Returns the one’s complement of a.
-
.congruent?(c, d) ⇒ Boolean
Returns true if n is congruent to c modulo d.
-
.divexact ⇒ Object
Functional Mappings.
-
.divisible?(b) ⇒ Boolean
Returns true if a is divisible by b.
-
.double_fac ⇒ Object
Returns _n!!_, the double factorial of n.
-
.GMP::Z.fac(n) ⇒ Object
Returns n!, the factorial of n.
- .fdiv_q_2exp ⇒ Object
- .fdiv_r_2exp ⇒ Object
-
.GMP::Z.fib(n) ⇒ Object
Returns _F [n]_, the nth Fibonacci number.
-
.GMP::Z.fib2(n) ⇒ Object
Returns [_F [n]_, _F [n-1]_], the nth and n-1th Fibonacci numbers.
-
.GMP::Z.import(str, order = -1) ⇒ Object
Return a GMP::Z from a String, ‘str`.
-
.GMP::Z.inp_raw(a, stream) ⇒ Object
Input from IO object stream in the format written by ‘GMP::Z#out_raw`, and put the result in a.
-
.GMP::Z.jacobi(a, b) ⇒ Object
Calculate the Jacobi symbol _(a/b)_.
-
.lcm(b) ⇒ Object
Returns the least common multiple of a and b.
- .lucnum ⇒ Object
-
.GMP::Z.mfac(n, m) ⇒ Object
Returns _n!^(m)_, the m-multi-factorial of n.
- .mul ⇒ Object
- .mul_2exp ⇒ Object
-
.neg ⇒ Object
Returns -a.
-
.GMP::Z.new(value = 0) ⇒ Object
Creates a new GMP::Z integer, with ‘value` as its value, converting where necessary.
-
.nextprime ⇒ Object
Returns the next prime greater than n.
-
.GMP::Z.pow(a, b) ⇒ Object
Returns a raised to b.
-
.GMP::Z.primorial(n) ⇒ Object
Returns the primorial of n.
-
.sqrt ⇒ Object
Returns the truncated integer part of the square root of a.
- .sub ⇒ Object
- .submul ⇒ Object
- .tdiv_q_2exp ⇒ Object
- .tdiv_r_2exp ⇒ Object
Instance Method Summary collapse
-
#%(b) ⇒ Object
Returns a mod b.
-
#&(b) ⇒ Object
Returns a bitwise-and b.
-
#*(b) ⇒ Object
Multiplies a with b.
-
#**(b) ⇒ Object
Returns a raised to b.
-
#+(b) ⇒ Object
Adds a to b.
-
#-(b) ⇒ Object
Subtracts b from a.
- #-@ ⇒ Object
-
#/(b) ⇒ Object
Divides a by b.
-
#<(b) ⇒ Object
Returns whether a is strictly less than b.
-
#<<(n) ⇒ Object
Returns a times 2 raised to n.
-
#<=(b) ⇒ Object
Returns whether a is less than or equal to b.
-
#<=>(b) ⇒ Object
Returns negative if a is less than b.
-
#==(b) ⇒ Object
Returns true if a is equal to b, and false otherwise.
-
#>(b) ⇒ Object
Returns whether a is strictly greater than b.
-
#>=(b) ⇒ Object
Returns whether a is greater than or equal to b.
-
#>> ⇒ Object
unsorted.
-
#[](index) ⇒ Object
Gets the bit at index, returned as either true or false.
-
#[]=(index) ⇒ Object
Sets the bit at index to x.
-
#^(b) ⇒ Object
Returns a bitwise exclusive-or b.
-
#abs ⇒ Object
call-seq: a.abs.
-
#abs! ⇒ Object
Sets a to its absolute value.
-
#add!(_b_) ⇒ Object
Adds a to b in-place, setting a to the sum.
-
#addmul!(b, c) ⇒ Object
Sets a to a plus b times c.
-
#cdiv(d) ⇒ Object
Divide n by d, forming a quotient q.
-
#cmod(d) ⇒ Object
Divides n by d, forming a remainder r.
-
#cmpabs(b) ⇒ Object
Returns negative if _abs(a)_ is less than _abs(b)_.
- #coerce(arg) ⇒ Object
-
#com ⇒ Object
Returns the one’s complement of a.
-
#com! ⇒ Object
Sets a to its one’s complement.
-
#congruent?(c, d) ⇒ Boolean
Returns true if n is congruent to c modulo d.
-
#divisible?(b) ⇒ Boolean
Returns true if a is divisible by b.
-
#eql?(b) ⇒ Boolean
Returns true if a is equal to b.
-
#even? ⇒ Boolean
Determines whether a is even.
-
#export(order = -1) ⇒ Object
Return a String with word data from a.
-
#fdiv(d) ⇒ Object
Divide n by d, forming a quotient q.
-
#fmod(d) ⇒ Object
Divides n by d, forming a remainder r.
-
#gcd(b) ⇒ Object
Returns the greatest common divisor of a and b.
-
#gcdext(b) ⇒ Object
Returns the greatest common divisor of a and b, in addition to s and t, the coefficients satisfying _a*s + b*t = g_.
-
#gcdext2(b) ⇒ Object
Returns the greatest common divisor of a and b, in addition to s, the coefficient satisfying _a*s + b*t = g_.
-
#hamdist(b) ⇒ Object
If a and b are both >= 0 or both < 0, calculate the hamming distance between a and b.
-
#hash ⇒ Object
Returns the computed hash value of a.
- #initialize(*args) ⇒ Object constructor
- #initialize_copy(orig_val) ⇒ Object
-
#invert(b) ⇒ Object
Returns the inverse of a modulo b.
-
#jacobi(b) ⇒ Object
Calculate the Jacobi symbol _(a/b)_.
- #lastbits_pos ⇒ Object
- #lastbits_sgn ⇒ Object
-
#lcm(b) ⇒ Object
Returns the least common multiple of a and b.
-
#legendre(p) ⇒ Object
Calculate the Legendre symbol _(a/p)_.
-
#neg ⇒ Object
Returns -a.
-
#neg! ⇒ Object
Sets a to -a.
-
#nextprime ⇒ Object
(also: #next_prime)
Returns the next prime greater than n.
-
#nextprime! ⇒ Object
(also: #next_prime!)
Sets n to the next prime greater than n.
-
#odd? ⇒ Boolean
Determines whether a is odd.
-
#out_raw(stream) ⇒ Object
Output a on IO object stream, in raw binary format.
-
#popcount ⇒ Object
If a >= 0, return the population count of a, which is the number of 1 bits in the binary representation.
-
#power? ⇒ Boolean
Returns true if p is a perfect power, i.e., if there exist integers a and b, with _b > 1_, such that p equals a raised to the power b.
-
#powmod(b, c) ⇒ Object
Returns a raised to b modulo c.
-
#probab_prime?(reps = 5) ⇒ Boolean
Determine whether n is prime.
-
#remove(f) ⇒ Object
Remove all occurrences of the factor f from n, returning the result as r.
-
#root(b) ⇒ Object
Returns the truncated integer part of the _b_th root of a.
-
#rootrem(b) ⇒ Object
Returns the truncated integer part of the _b_th root of a, and the remainder, _a - root**b_.
-
#scan0(starting_bit) ⇒ Object
Scan a, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found.
-
#scan1(starting_bit) ⇒ Object
Scan a, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found.
-
#sgn ⇒ Object
Returns +1 if a > 0, 0 if a == 0, and -1 if a < 0.
-
#size ⇒ Object
Return the size of a measured in number of limbs.
-
#size_in_bin ⇒ Object
Return the size of a measured in number of digits in binary.
-
#sizeinbase ⇒ Object
(also: #size_in_base)
Return the size of a measured in number of digits in ‘base`.
-
#sqrt ⇒ Object
Returns the truncated integer part of the square root of a.
-
#sqrt! ⇒ Object
Sets a to the truncated integer part of its square root.
-
#sqrtrem ⇒ Object
Returns the truncated integer part of the square root of a as s and the remainder _a - s * s_ as r, which will be zero if a is a perfect square.
-
#square? ⇒ Boolean
Returns true if p is a perfect square, i.e., if the square root of p is an integer.
-
#sub!(b) ⇒ Object
Subtracts b from a in-place, setting a to the difference.
-
#submul!(b, c) ⇒ Object
Sets a to a minus b times c.
-
#swap(b) ⇒ Object
Efficiently swaps the contents of a with b.
-
#tdiv(d) ⇒ Object
Divides n by d, forming a quotient q.
-
#tmod(d) ⇒ Object
Divides n by d, forming a remainder r.
-
#to_d ⇒ Object
Returns a as a Float if a fits in a Float.
-
#to_i ⇒ Object
Returns a as an Fixnum if a fits into a Fixnum.
-
#to_s(*args) ⇒ Object
(also: #inspect)
Returns a, as a String.
-
#tshr(d) ⇒ Object
Divides n by _2^d_, forming a quotient q.
-
#|(b) ⇒ Object
Returns a bitwise inclusive-or b.
Constructor Details
#initialize(*args) ⇒ Object
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'ext/gmpz.c', line 689
VALUE r_gmpz_initialize(int argc, VALUE *argv, VALUE self)
{
MP_INT *self_val;
int base = 0;
/* Set up the base if 2 arguments are passed */
if (argc == 2) { /* only ok if String, Fixnum */
if (STRING_P(argv[0])) { /* first arg must be a String */
if (FIXNUM_P(argv[1])) { /* second arg must be a Fixnum */
base = FIX2INT(argv[1]);
if ( base != 0 && ( base < 2 || base > 62) )
rb_raise (rb_eRangeError, "base must be either 0 or between 2 and 62");
} else {
rb_raise (rb_eTypeError, "base must be a Fixnum between 2 and 62, not a %s.", rb_class2name (rb_class_of (argv[1])));
}
} else {
rb_raise(
rb_eTypeError,
"GMP::Z.new() must be passed a String as the 1st argument (not a %s), if a base is passed as the 2nd argument.",
rb_class2name (rb_class_of (argv[0]))
);
}
}
if (argc != 0) {
mpz_get_struct (self,self_val);
mpz_set_value (self_val, argv[0], base);
}
return Qnil;
}
|
Class Method Details
.2fac ⇒ Object
.abs ⇒ Object
call-seq:
a.abs
Returns the absolute value of a.
.addmul ⇒ Object
.cdiv_q_2exp ⇒ Object
.cdiv_r_2exp ⇒ Object
.com ⇒ Object
Returns the one’s complement of a.
.congruent?(c, d) ⇒ Boolean
Returns true if n is congruent to c modulo d. c and d can be an instance any of the following:
-
GMP::Z
-
Fixnum
-
Bignum
.divexact ⇒ Object
Functional Mappings
.divisible?(b) ⇒ Boolean
Returns true if a is divisible by b. b can be an instance any of the following:
-
GMP::Z
-
Fixnum
-
Bignum
.GMP::Z.double_fac(n) ⇒ Object .GMP::Z.send(: "2fac", n) ⇒ Object
Returns _n!!_, the double factorial of n.
.GMP::Z.fac(n) ⇒ Object
Returns n!, the factorial of n.
.fdiv_q_2exp ⇒ Object
.fdiv_r_2exp ⇒ Object
.GMP::Z.fib(n) ⇒ Object
Returns _F [n]_, the nth Fibonacci number.
.GMP::Z.fib2(n) ⇒ Object
Returns [_F [n]_, _F [n-1]_], the nth and n-1th Fibonacci numbers.
.GMP::Z.import(str, order = -1) ⇒ Object
Return a GMP::Z from a String, ‘str`.
‘order` can be 1 for most significant word first or -1 for least significant first.
There is no sign taken from the data, the result will simply be a positive integer. An application can handle any sign itself, and apply it for instance with ‘GMP::Z#neg`.
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 |
# File 'ext/gmpz.c', line 2926
VALUE r_gmpzsg_import(int argc, VALUE *argv, VALUE klass)
{
MP_INT *res;
VALUE string_val, order_val, res_val;
char *string;
int order, endian;
size_t nails;
(void)klass;
endian = 0;
nails = 0;
order = 1;
rb_scan_args (argc, argv, "11", &string_val, &order_val);
if (NIL_P (order_val))
order = -1;
else if (! FIXNUM_P (order_val))
typeerror_as (X, "order");
else
order = FIX2INT (order_val);
mpz_make_struct(res_val, res);
mpz_init(res);
string = StringValuePtr (string_val);
mpz_import (res, RSTRING_LEN(string_val), order, sizeof(char), endian, nails, string);
return res_val;
}
|
.GMP::Z.inp_raw(a, stream) ⇒ Object
Input from IO object stream in the format written by ‘GMP::Z#out_raw`, and put the result in a. Return the number of bytes read, or if an error occurred, return 0.
2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 |
# File 'ext/gmpz.c', line 2889
VALUE r_gmpzsg_inp_raw(VALUE klass, VALUE a_val, VALUE stream_val)
{
MP_INT *a;
FILE *stream;
(void)klass;
if (! GMPZ_P(a_val))
typeerror_as(Z, "a");
if (TYPE (stream_val) != T_FILE)
rb_raise (rb_eTypeError, "stream must be an IO.");
mpz_get_struct(a_val, a);
stream = rb_io_stdio_file (RFILE (stream_val)->fptr);
return INT2FIX (mpz_inp_raw (a, stream));
}
|
.GMP::Z.jacobi(a, b) ⇒ Object
Calculate the Jacobi symbol _(a/b)_. This is defined only for b odd and positive.
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 |
# File 'ext/gmpz.c', line 2141
VALUE r_gmpzsg_jacobi(VALUE klass, VALUE a, VALUE b)
{
MP_INT *a_val = NULL, *b_val = NULL;
int res_val;
int free_a_val = 0;
int free_b_val = 0;
(void)klass;
if (GMPZ_P(a)) {
mpz_get_struct(a, a_val);
} else if (FIXNUM_P(a)) {
mpz_temp_alloc(a_val);
mpz_init_set_ui(a_val, FIX2NUM(a));
free_a_val = 1;
} else if (BIGNUM_P(a)) {
mpz_temp_from_bignum(a_val, a);
free_a_val = 1;
} else {
typeerror_as(ZXB, "a");
}
if (GMPZ_P(b)) {
mpz_get_struct(b, b_val);
if (mpz_sgn(b_val) != 1)
rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is non-positive.");
if (mpz_even_p(b_val))
rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is even.");
} else if (FIXNUM_P(b)) {
if (FIX2NUM(b) <= 0)
rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is non-positive.");
if (FIX2NUM(b) % 2 == 0)
rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is even.");
mpz_temp_alloc(b_val);
mpz_init_set_ui(b_val, FIX2NUM(b));
free_b_val = 1;
} else if (BIGNUM_P(b)) {
mpz_temp_from_bignum(b_val, b);
if (mpz_sgn(b_val) != 1) {
mpz_temp_free(b_val);
rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is non-positive.");
}
if (mpz_even_p(b_val)) {
mpz_temp_free(b_val);
rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is even.");
}
free_b_val = 1;
} else {
typeerror_as(ZXB, "b");
}
res_val = mpz_jacobi(a_val, b_val);
if (free_a_val) { mpz_temp_free(a_val); }
if (free_b_val) { mpz_temp_free(b_val); }
return INT2FIX(res_val);
}
|
.lcm(b) ⇒ Object
Returns the least common multiple of a and b. The result is always positive even if one or both of a or b are negative.
.lucnum ⇒ Object
.GMP::Z.mfac(n, m) ⇒ Object
Returns _n!^(m)_, the m-multi-factorial of n.
.mul ⇒ Object
.mul_2exp ⇒ Object
.neg ⇒ Object .- ⇒ Object
Returns -a.
.GMP::Z.new(value = 0) ⇒ Object
Creates a new GMP::Z integer, with ‘value` as its value, converting where necessary. `value` must be an instance of one of the following classes:
-
GMP::Z
-
Fixnum
-
String
-
Bignum
.nextprime ⇒ Object .next_prime ⇒ Object
Returns the next prime greater than n.
This function uses a probabilistic algorithm to identify primes. For practical purposes it’s adequate, the chance of a composite passing will be extremely small.
.GMP::Z.pow(a, b) ⇒ Object
Returns a raised to b. The case 0^0 yields 1.
.GMP::Z.primorial(n) ⇒ Object
Returns the primorial of n.
.sqrt ⇒ Object
Returns the truncated integer part of the square root of a.
.sub ⇒ Object
.submul ⇒ Object
.tdiv_q_2exp ⇒ Object
.tdiv_r_2exp ⇒ Object
Instance Method Details
#%(b) ⇒ Object
Returns a mod b. b can be an instance any of the following:
-
GMP::Z
-
Fixnum
-
Bignum
#&(b) ⇒ Object
Returns a bitwise-and b. b must be an instance of one of the following:
-
GMP::Z
-
Fixnum
-
Bignum
#*(b) ⇒ Object
Multiplies a with b. a must be an instance of one of
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 |
# File 'ext/gmpz.c', line 1109
VALUE r_gmpz_mul(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val, *res_val;
VALUE res = 0;
mpz_get_struct (self, self_val);
if (GMPZ_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_get_struct (arg,arg_val);
mpz_mul (res_val, self_val, arg_val);
} else if (FIXNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
if (FIX2NUM (arg) >= 0)
mpz_mul_ui (res_val, self_val, FIX2NUM (arg));
else
mpz_mul_si (res_val, self_val, FIX2NUM (arg));
} else if (GMPQ_P (arg)) {
return r_gmpq_mul (arg, self);
} else if (GMPF_P (arg)) {
#ifndef MPFR
return r_gmpf_mul (arg, self);
#else
return rb_funcall (arg, rb_intern ("*"), 1, self);
#endif
} else if (BIGNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_set_bignum (res_val, arg);
mpz_mul (res_val, res_val, self_val);
} else {
typeerror (ZQFXB);
}
return res;
}
|
#**(b) ⇒ Object
Returns a raised to b. The case 0^0 yields 1.
#+(b) ⇒ Object
Adds a to b. b must be an instance of one of:
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 |
# File 'ext/gmpz.c', line 931
VALUE r_gmpz_add(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val, *res_val;
VALUE res = 0;
mpz_get_struct(self,self_val);
if (GMPZ_P(arg)) {
mpz_get_struct(arg,arg_val);
mpz_make_struct_init(res, res_val);
mpz_add(res_val, self_val, arg_val);
} else if (FIXNUM_P(arg)) {
mpz_make_struct_init(res, res_val);
if (FIX2NUM(arg) > 0)
mpz_add_ui(res_val, self_val, FIX2NUM(arg));
else
mpz_sub_ui(res_val, self_val, -FIX2NUM(arg));
} else if (GMPQ_P(arg)) {
return r_gmpq_add(arg, self);
} else if (GMPF_P(arg)) {
#ifndef MPFR
return r_gmpf_add(arg, self);
#else
return rb_funcall(arg, rb_intern("+"), 1, self);
#endif
} else if (BIGNUM_P(arg)) {
mpz_make_struct_init(res, res_val);
mpz_init(res_val);
mpz_set_bignum(res_val, arg);
mpz_add(res_val, res_val, self_val);
} else {
typeerror(ZQFXB);
}
return res;
}
|
#-(b) ⇒ Object
Subtracts b from a. b must be an instance of one of:
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'ext/gmpz.c', line 1017
VALUE r_gmpz_sub(VALUE self, VALUE arg)
{
MP_RAT *res_val_q, *arg_val_q;
MP_INT *self_val, *arg_val, *res_val;
MP_FLOAT *arg_val_f, *res_val_f;
unsigned long prec;
VALUE res = 0;
mpz_get_struct(self,self_val);
if (GMPZ_P(arg)) {
mpz_make_struct_init(res, res_val);
mpz_get_struct(arg,arg_val);
mpz_sub (res_val, self_val, arg_val);
} else if (FIXNUM_P(arg)) {
mpz_make_struct_init(res, res_val);
if (FIX2NUM(arg) > 0)
mpz_sub_ui (res_val, self_val, FIX2NUM(arg));
else
mpz_add_ui (res_val, self_val, -FIX2NUM(arg));
} else if (GMPQ_P(arg)) {
mpq_make_struct_init(res, res_val_q);
mpq_get_struct(arg,arg_val_q);
mpz_set (mpq_denref(res_val_q), mpq_denref(arg_val_q));
mpz_mul (mpq_numref(res_val_q), mpq_denref(arg_val_q), self_val);
mpz_sub (mpq_numref(res_val_q), mpq_numref(res_val_q), mpq_numref(arg_val_q));
} else if (GMPF_P(arg)) {
mpf_get_struct_prec (arg, arg_val_f, prec);
mpf_make_struct_init(res, res_val_f, prec);
mpf_set_z (res_val_f, self_val);
mpf_sub (res_val_f, res_val_f, arg_val_f);
} else if (BIGNUM_P(arg)) {
mpz_make_struct_init(res, res_val);
mpz_set_bignum (res_val, arg);
mpz_sub (res_val, self_val, res_val);
} else {
typeerror (ZQFXB);
}
return res;
}
|
#-@ ⇒ Object
#/(b) ⇒ Object
Divides a by b. Combines the different GMP division functions to provide what one is hopefully looking for. The result will either be an instance of GMP::Q or GMP::F, depending on b. b must be an instance of one of the following:
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
If b is a GMP::Z, Fixnum, GMP::Q, or Bignum, then no division is actually performed. Instead, we simply construct a new GMP::Q number, using a and b as the numerator and denominator (not exactly true for the GMP::Q case).
If b is a GMP::F, then the result is calculated via ‘mpf_div`.
#<(b) ⇒ Object
Returns whether a is strictly less than b.
#<<(n) ⇒ Object
Returns a times 2 raised to n. This operation can also be defined as a left shift by n bits.
#<=(b) ⇒ Object
Returns whether a is less than or equal to b.
#<=>(b) ⇒ Object
Returns negative if a is less than b.
Returns 0 if a is equal to b.
Returns positive if a is greater than b.
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 |
# File 'ext/gmpz.c', line 2482
VALUE r_gmpz_cmp(VALUE self, VALUE arg)
{
MP_INT *self_val;
int res;
mpz_get_struct(self,self_val);
res = mpz_cmp_value(self_val, arg);
if (res > 0)
return INT2FIX(1);
else if (res == 0)
return INT2FIX(0);
else
return INT2FIX(-1);
}
|
#==(b) ⇒ Object
Returns true if a is equal to b, and false otherwise.
#>(b) ⇒ Object
Returns whether a is strictly greater than b.
#>=(b) ⇒ Object
Returns whether a is greater than or equal to b.
#>> ⇒ Object
unsorted
#[](index) ⇒ Object
Gets the bit at index, returned as either true or false.
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 |
# File 'ext/gmpz.c', line 2834
VALUE r_gmpz_getbit(VALUE self, VALUE bitnr)
{
MP_INT *self_val;
unsigned long bitnr_val = 0;
mpz_get_struct (self, self_val);
if (FIXNUM_P (bitnr)) {
bitnr_val = FIX2NUM (bitnr);
} else {
typeerror_as (X, "index");
}
return mpz_tstbit (self_val, bitnr_val) ? Qtrue : Qfalse;
}
|
#[]=(index) ⇒ Object
Sets the bit at index to x.
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 |
# File 'ext/gmpz.c', line 2803
VALUE r_gmpz_setbit(VALUE self, VALUE bitnr, VALUE set_to)
{
MP_INT *self_val;
unsigned long bitnr_val = 0;
mpz_get_struct (self, self_val);
if (FIXNUM_P (bitnr)) {
if (FIX2NUM (bitnr) < 0) {
rb_raise (rb_eRangeError, "index must be nonnegative");
}
bitnr_val = FIX2NUM (bitnr);
} else {
typeerror_as (X, "index");
}
if (RTEST (set_to)) {
mpz_setbit (self_val, bitnr_val);
} else {
mpz_clrbit (self_val, bitnr_val);
}
return Qnil;
}
|
#^(b) ⇒ Object
Returns a bitwise exclusive-or b. b must be an instance of one of the following:
-
GMP::Z
-
Fixnum
-
Bignum
#abs ⇒ Object
call-seq:
a.abs
Returns the absolute value of a.
#abs! ⇒ Object
Sets a to its absolute value.
#add!(_b_) ⇒ Object
Adds a to b in-place, setting a to the sum. b must be an instance of one of:
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 |
# File 'ext/gmpz.c', line 980
VALUE r_gmpz_add_self(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val;
mpz_get_struct(self,self_val);
if (GMPZ_P(arg)) {
mpz_get_struct(arg,arg_val);
mpz_add(self_val, self_val, arg_val);
} else if (FIXNUM_P(arg)) {
if (FIX2NUM(arg) > 0)
mpz_add_ui(self_val, self_val, FIX2NUM(arg));
else
mpz_sub_ui(self_val, self_val, -FIX2NUM(arg));
} else if (BIGNUM_P(arg)) {
mpz_temp_from_bignum(arg_val, arg);
mpz_add(self_val, self_val, arg_val);
mpz_temp_free(arg_val);
} else {
typeerror(ZXB);
}
return Qnil;
}
|
#addmul!(b, c) ⇒ Object
Sets a to a plus b times c. b and c must each be an instance of one of
-
GMP::Z
-
Fixnum
-
Bignum
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 |
# File 'ext/gmpz.c', line 1158
static VALUE r_gmpz_addmul_self(VALUE self, VALUE b, VALUE c)
{
MP_INT *self_val, *b_val, *c_val;
int free_b_val = 0;
if (GMPZ_P (b)) {
mpz_get_struct (b, b_val);
} else if (FIXNUM_P (b)) {
mpz_temp_alloc (b_val);
mpz_init_set_si (b_val, FIX2NUM (b));
free_b_val = 1;
} else if (BIGNUM_P (b)) {
mpz_temp_from_bignum (b_val, b);
free_b_val = 1;
} else {
typeerror_as (ZXB, "addend");
}
mpz_get_struct (self, self_val);
if (GMPZ_P (c)) {
mpz_get_struct (c, c_val);
mpz_addmul (self_val, b_val, c_val);
} else if (FIXNUM_P (c) && FIX2NUM (c) >= 0) {
mpz_addmul_ui (self_val, b_val, FIX2NUM (c));
} else if (FIXNUM_P (c) || BIGNUM_P (c)) {
mpz_temp_from_bignum (c_val, c);
mpz_addmul (self_val, b_val, c_val);
mpz_temp_free (c_val);
} else {
if (free_b_val)
mpz_temp_free (b_val);
typeerror_as (ZXB, "multiplicand");
}
if (free_b_val)
mpz_temp_free (b_val);
return self;
}
|
#cdiv(d) ⇒ Object
Divide n by d, forming a quotient q. cdiv rounds q up towards _+infinity_. The c stands for “ceil”.
q will satisfy _n=q*d+r_.
This function calculates only the quotient.
#cmod(d) ⇒ Object
Divides n by d, forming a remainder r. r will have the opposite sign of d. The c stands for “ceil”.
r will satisfy _n=q*d+r_, and r will satisfy _0 <= abs(r ) < abs(d)_.
This function calculates only the remainder.
#cmpabs(b) ⇒ Object
Returns negative if _abs(a)_ is less than _abs(b)_.
Returns 0 if _abs(a)_ is equal to _abs(b)_.
Returns positive if _abs(a)_ is greater than _abs(b)_.
#coerce(arg) ⇒ Object
23 24 25 26 |
# File 'ext/gmp.c', line 23
static VALUE r_gmpz_coerce(VALUE self, VALUE arg)
{
return rb_assoc_new(r_gmpzsg_new(1, &arg, cGMP_Z), self);
}
|
#com ⇒ Object
Returns the one’s complement of a.
#com! ⇒ Object
Sets a to its one’s complement.
#congruent?(c, d) ⇒ Boolean
Returns true if n is congruent to c modulo d. c and d can be an instance any of the following:
-
GMP::Z
-
Fixnum
-
Bignum
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 |
# File 'ext/gmpz.c', line 1575
static VALUE r_gmpz_congruent(VALUE self_val, VALUE c_val, VALUE d_val)
{
MP_INT *self, *c = NULL, *d = NULL;
int res, free_c, free_d;
mpz_get_struct (self_val, self);
free_c = free_d = 0;
if (FIXNUM_P (c_val) && FIX2NUM (c_val) > 0 &&
FIXNUM_P (d_val) && FIX2NUM (d_val) > 0) {
res = mpz_congruent_ui_p (self, FIX2NUM (c_val), FIX2NUM (d_val));
} else {
if (FIXNUM_P (c_val)) {
mpz_make_struct_init (c_val, c);
mpz_init_set_si (c, FIX2NUM (c_val));
} else if (BIGNUM_P (c_val)) {
mpz_temp_from_bignum (c, c_val);
free_c = 1;
} else if (GMPZ_P (c_val)) {
mpz_get_struct (c_val, c);
} else {
typeerror_as (ZXB, "c");
}
if (FIXNUM_P (d_val)) {
mpz_make_struct_init (d_val, d);
mpz_init_set_si (d, FIX2NUM (d_val));
} else if (BIGNUM_P (d_val)) {
mpz_temp_from_bignum (d, d_val);
free_d = 1;
} else if (GMPZ_P (d_val)) {
mpz_get_struct (d_val, d);
} else {
if (free_c) { mpz_temp_free (c); }
typeerror_as (ZXB, "d");
}
res = mpz_congruent_p (self, c, d);
if (free_c) { mpz_temp_free (c); }
if (free_d) { mpz_temp_free (d); }
}
return (res != 0) ? Qtrue : Qfalse;
}
|
#divisible?(b) ⇒ Boolean
Returns true if a is divisible by b. b can be an instance any of the following:
-
GMP::Z
-
Fixnum
-
Bignum
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 |
# File 'ext/gmpz.c', line 1531
static VALUE r_gmpz_divisible(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val;
int res = 0;
mpz_get_struct (self, self_val);
if (FIXNUM_P (arg) && FIX2NUM (arg) > 0) {
mpz_temp_alloc (arg_val);
mpz_init_set_ui (arg_val, FIX2NUM (arg));
res = mpz_divisible_ui_p (self_val, FIX2NUM (arg));
mpz_temp_free (arg_val);
} else if (TYPE (arg) == T_FIXNUM) {
mpz_temp_alloc (arg_val);
mpz_make_struct_init (arg, arg_val);
mpz_init_set_si (arg_val, FIX2NUM (arg));
res = mpz_divisible_p (self_val, arg_val);
mpz_temp_free (arg_val);
} else if (BIGNUM_P (arg)) {
mpz_temp_from_bignum (arg_val, arg);
res = mpz_divisible_p (self_val, arg_val);
mpz_temp_free (arg_val);
} else if (GMPZ_P (arg)) {
mpz_get_struct (arg, arg_val);
res = mpz_divisible_p (self_val, arg_val);
} else {
typeerror_as (ZXB, "argument");
}
return (res != 0) ? Qtrue : Qfalse;
}
|
#eql?(b) ⇒ Boolean
Returns true if a is equal to b. a and b must then be equal in cardinality, and both be instances of GMP::Z. Otherwise, returns false. ‘a.eql?(b)` if and only if `b.class == GMP::Z`, and `a.hash == b.hash`.
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 |
# File 'ext/gmpz.c', line 2606
VALUE r_gmpz_eql(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val;
mpz_get_struct(self,self_val);
if (GMPZ_P(arg)) {
mpz_get_struct(arg, arg_val);
return (mpz_cmp (self_val, arg_val) == 0) ? Qtrue : Qfalse;
}
else {
return Qfalse;
}
}
|
#even? ⇒ Boolean
Determines whether a is even. Returns true or false.
#export(order = -1) ⇒ Object
Return a String with word data from a.
‘order` can be 1 for most significant word first or -1 for least significant first.
If ‘a` is non-zero then the most significant word produced will be non-zero. `GMP::Z(0).export` returns `“”`.
The sign of a is ignored, just the absolute value is exported. An application can use ‘GMP::Z#sgn` to get the sign and handle it as desired.
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 |
# File 'ext/gmpz.c', line 2973
VALUE r_gmpz_export(int argc, VALUE *argv, VALUE self_val)
{
MP_INT *self;
VALUE order_val, res;
int order, endian;
size_t countp, nails;
char *string;
endian = 0;
nails = 0;
order = 1;
mpz_get_struct(self_val, self);
rb_scan_args (argc, argv, "01", &order_val);
if (NIL_P (order_val))
order = -1;
else if (! FIXNUM_P (order_val))
typeerror_as (X, "order");
else
order = FIX2INT (order_val);
string = mpz_export (NULL, &countp, order, sizeof(char), endian, nails, self);
res = rb_str_new (string, countp);
free (string);
return res;
}
|
#fdiv(d) ⇒ Object
Divide n by d, forming a quotient q. fdiv rounds q down towards -infinity. The f stands for “floor”.
q will satisfy _n=q*d+r_.
This function calculates only the quotient.
#fmod(d) ⇒ Object
Divides n by d, forming a remainder r. r will have the same sign as d. The f stands for “floor”.
r will satisfy _n=q*d+r_, and r will satisfy _0 <= abs(r ) < abs(d)_.
This function calculates only the remainder.
The remainder can be negative, so the return value is the absolute value of the remainder.
#gcd(b) ⇒ Object
Returns the greatest common divisor of a and b. The result is always positive even if one or both of a or b are negative.
#gcdext(b) ⇒ Object
Returns the greatest common divisor of a and b, in addition to s and t, the coefficients satisfying _a*s + b*t = g_. g is always positive, even if one or both of a and b are negative. s and t are chosen such that _abs(s) <= abs(b)_ and _abs(t) <= abs(a)_.
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 |
# File 'ext/gmpz.c', line 1953
VALUE r_gmpz_gcdext(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val, *res_val, *s_val, *t_val;
VALUE res = 0, s = 0, t = 0, ary;
int free_arg_val = 0;
mpz_get_struct (self,self_val);
if (GMPZ_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_make_struct_init (s, s_val);
mpz_make_struct_init (t, t_val);
mpz_get_struct (arg, arg_val);
mpz_gcdext (res_val, s_val, t_val, self_val, arg_val);
} else if (FIXNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_make_struct_init (s, s_val);
mpz_make_struct_init (t, t_val);
mpz_temp_alloc (arg_val);
mpz_init_set_ui (arg_val, FIX2NUM (arg));
free_arg_val = 1;
mpz_gcdext (res_val, s_val, t_val, self_val, arg_val);
} else if (BIGNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_make_struct_init (s, s_val);
mpz_make_struct_init (t, t_val);
mpz_set_bignum (res_val, arg);
mpz_gcdext (res_val, s_val, t_val, res_val, self_val);
} else {
typeerror (ZXB);
}
if (free_arg_val)
mpz_temp_free (arg_val);
ary = rb_ary_new3 (3, res, s, t);
return ary;
}
|
#gcdext2(b) ⇒ Object
Returns the greatest common divisor of a and b, in addition to s, the coefficient satisfying _a*s + b*t = g_. g is always positive, even if one or both of a and b are negative. s and t are chosen such that _abs(s) <= abs(b)_ and _abs(t) <= abs(a)_.
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 |
# File 'ext/gmpz.c', line 2004
VALUE r_gmpz_gcdext2(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val, *res_val, *s_val;
VALUE res = 0, s = 0, ary;
int free_arg_val = 0;
mpz_get_struct (self,self_val);
if (GMPZ_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_make_struct_init (s, s_val);
mpz_get_struct (arg, arg_val);
mpz_gcdext (res_val, s_val, NULL, self_val, arg_val);
} else if (FIXNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_make_struct_init (s, s_val);
mpz_temp_alloc (arg_val);
mpz_init_set_ui (arg_val, FIX2NUM(arg));
free_arg_val = 1;
mpz_gcdext (res_val, s_val, NULL, self_val, arg_val);
} else if (BIGNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_make_struct_init (s, s_val);
mpz_set_bignum (res_val, arg);
mpz_gcdext (res_val, s_val, NULL, res_val, self_val);
} else {
typeerror (ZXB);
}
if (free_arg_val)
mpz_temp_free (arg_val);
ary = rb_ary_new3 (2, res, s);
return ary;
}
|
#hamdist(b) ⇒ Object
If a and b are both >= 0 or both < 0, calculate the hamming distance between a and b. If one operand is >= 0 and the other is less than 0, then return “infinity” (the largest possible ‘mp_bitcnt_t`).
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 |
# File 'ext/gmpz.c', line 2726
VALUE r_gmpz_hamdist(VALUE self_val, VALUE b_val)
{
MP_INT *self, *b;
mpz_get_struct (self_val, self);
mpz_get_struct ( b_val, b);
if (! GMPZ_P (b_val)) {
typeerror_as (Z, "b");
}
return INT2FIX (mpz_hamdist(self, b));
}
|
#hash ⇒ Object
Returns the computed hash value of a. This method first converts a into a String (base 10), then calls String#hash on the result, returning the hash value. ‘a.eql?(b)` if and only if `b.class == GMP::Z`, and `a.hash == b.hash`.
2632 2633 2634 2635 2636 2637 |
# File 'ext/gmpz.c', line 2632
VALUE r_gmpz_hash(VALUE self)
{
ID to_s_sym = rb_intern("to_s");
ID hash_sym = rb_intern("hash");
return rb_funcall(rb_funcall(self, to_s_sym, 0), hash_sym, 0);
}
|
#initialize_copy(orig_val) ⇒ Object
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 |
# File 'ext/gmpz.c', line 720
static VALUE r_gmpz_initialize_copy(VALUE copy_val, VALUE orig_val) {
MP_INT *orig, *copy;
if (copy_val == orig_val) return copy_val;
if (TYPE(orig_val) != T_DATA) {
rb_raise(rb_eTypeError, "wrong argument type");
}
mpz_get_struct (orig_val, orig);
mpz_get_struct (copy_val, copy);
mpz_set (copy, orig);
return copy_val;
}
|
#invert(b) ⇒ Object
Returns the inverse of a modulo b. If the inverse exists, the return value is non-zero and the result will be non-negative and less than b. If an inverse doesn’t exist, the result is undefined.
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 |
# File 'ext/gmpz.c', line 2085
VALUE r_gmpz_invert(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val, *res_val;
VALUE res = 0;
mpz_get_struct (self,self_val);
if (GMPZ_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_get_struct (arg, arg_val);
mpz_invert (res_val, self_val, arg_val);
} else if (FIXNUM_P (arg)) {
mpz_temp_alloc(arg_val);
mpz_init_set_ui(arg_val, FIX2NUM(arg));
mpz_make_struct_init (res, res_val);
mpz_invert (res_val, self_val, arg_val);
} else if (BIGNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_set_bignum (res_val, arg);
mpz_invert (res_val, res_val, self_val);
} else {
typeerror (ZXB);
}
return res;
}
|
#jacobi(b) ⇒ Object
Calculate the Jacobi symbol _(a/b)_. This is defined only for b odd and positive.
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 |
# File 'ext/gmpz.c', line 2119
VALUE r_gmpz_jacobi(VALUE self, VALUE b)
{
MP_INT *self_val, *b_val;
int res_val;
mpz_get_struct(self, self_val);
mpz_get_struct( b, b_val);
if (mpz_sgn(b_val) != 1)
rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is non-positive.");
if (mpz_even_p(b_val))
rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is even.");
res_val = mpz_jacobi(self_val, b_val);
return INT2FIX(res_val);
}
|
#lastbits_pos ⇒ Object
#lastbits_sgn ⇒ Object
#lcm(b) ⇒ Object
Returns the least common multiple of a and b. The result is always positive even if one or both of a or b are negative.
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 |
# File 'ext/gmpz.c', line 2050
VALUE r_gmpz_lcm(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val, *res_val;
VALUE res = 0;
mpz_get_struct (self,self_val);
if (GMPZ_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_get_struct (arg, arg_val);
mpz_lcm (res_val, self_val, arg_val);
} else if (FIXNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_lcm_ui (res_val, self_val, FIX2NUM(arg));
} else if (BIGNUM_P (arg)) {
mpz_make_struct_init (res, res_val);
mpz_set_bignum (res_val, arg);
mpz_lcm (res_val, res_val, self_val);
} else {
typeerror (ZXB);
}
return res;
}
|
#legendre(p) ⇒ Object
Calculate the Legendre symbol _(a/p)_. This is defined only for p an odd positive prime, and for such p it’s identical to the Jacobi symbol.
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 |
# File 'ext/gmpz.c', line 2205
VALUE r_gmpz_legendre(VALUE self, VALUE p)
{
MP_INT *self_val, *p_val;
int res_val;
mpz_get_struct(self, self_val);
mpz_get_struct( p, p_val);
if (mpz_sgn(p_val) != 1)
rb_raise(rb_eRangeError, "Cannot take Legendre symbol (a/p) where p is non-positive.");
if (mpz_even_p(p_val))
rb_raise(rb_eRangeError, "Cannot take Legendre symbol (a/p) where p is even.");
if (mpz_probab_prime_p(p_val, 5) == 0)
rb_raise(rb_eRangeError, "Cannot take Legendre symbol (a/p) where p is composite.");
res_val = mpz_legendre(self_val, p_val);
return INT2FIX(res_val);
}
|
#neg ⇒ Object #- ⇒ Object
Returns -a.
#neg! ⇒ Object
Sets a to -a.
#nextprime ⇒ Object #next_prime ⇒ Object Also known as: next_prime
Returns the next prime greater than n.
This function uses a probabilistic algorithm to identify primes. For practical purposes it’s adequate, the chance of a composite passing will be extremely small.
#nextprime! ⇒ Object #next_prime! ⇒ Object Also known as: next_prime!
Sets n to the next prime greater than n.
This function uses a probabilistic algorithm to identify primes. For practical purposes it’s adequate, the chance of a composite passing will be extremely small.
#odd? ⇒ Boolean
Determines whether a is odd. Returns true or false.
#out_raw(stream) ⇒ Object
Output a on IO object stream, in raw binary format. The integer is written in a portable format, with 4 bytes of size information, and that many bytes of limbs. Both the size and the limbs are written in decreasing significance order (i.e., in big-endian).
The output can be read with ‘GMP::Z.inp_raw`.
Return the number of bytes written, or if an error occurred, return 0.
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 |
# File 'ext/gmpz.c', line 2868
VALUE r_gmpz_out_raw(VALUE self, VALUE stream)
{
MP_INT *self_val;
FILE *fd;
mpz_get_struct(self, self_val);
if (TYPE (stream) != T_FILE) {
rb_raise (rb_eTypeError, "stream must be an IO.");
}
fd = rb_io_stdio_file (RFILE (stream)->fptr);
return INT2FIX (mpz_out_raw (fd, self_val));
}
|
#popcount ⇒ Object
If a >= 0, return the population count of a, which is the number of 1 bits in the binary representation. If a < 0, the number of 1s is infinite, and the return value is ‘INT2FIX(ULONG_MAX)`, the largest possible unsigned long.
#power? ⇒ Boolean
Returns true if p is a perfect power, i.e., if there exist integers a and b, with _b > 1_, such that p equals a raised to the power b.
Under this definition both 0 and 1 are considered to be perfect powers. Negative values of integers are accepted, but of course can only be odd perfect powers.
#powmod(b, c) ⇒ Object
Returns a raised to b modulo c.
Negative b is supported if an inverse _a^-1_ mod c exists. If an inverse doesn’t exist then a divide by zero is raised.
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 |
# File 'ext/gmpz.c', line 1667
VALUE r_gmpz_powm(VALUE self, VALUE exp, VALUE mod)
{
MP_INT *self_val, *res_val, *mod_val, *exp_val;
VALUE res;
int free_mod_val = 0;
if (GMPZ_P(mod)) {
mpz_get_struct(mod, mod_val);
if (mpz_sgn(mod_val) <= 0) {
rb_raise(rb_eRangeError, "modulus must be positive");
}
} else if (FIXNUM_P(mod)) {
if (FIX2NUM(mod) <= 0) {
rb_raise(rb_eRangeError, "modulus must be positive");
}
mpz_temp_alloc(mod_val);
mpz_init_set_ui(mod_val, FIX2NUM(mod));
free_mod_val = 1;
} else if (BIGNUM_P(mod)) {
mpz_temp_from_bignum(mod_val, mod);
if (mpz_sgn(mod_val) <= 0) {
mpz_temp_free(mod_val);
rb_raise(rb_eRangeError, "modulus must be positive");
}
free_mod_val = 1;
} else {
typeerror_as(ZXB, "modulus");
}
mpz_make_struct_init(res, res_val);
mpz_get_struct(self, self_val);
if (GMPZ_P(exp)) {
mpz_get_struct(exp, exp_val);
if (mpz_sgn(mod_val) < 0) {
rb_raise(rb_eRangeError, "exponent must be nonnegative");
}
mpz_powm(res_val, self_val, exp_val, mod_val);
} else if (FIXNUM_P(exp)) {
if (FIX2NUM(exp) < 0)
{
if (free_mod_val)
mpz_temp_free(mod_val);
rb_raise(rb_eRangeError, "exponent must be nonnegative");
}
mpz_powm_ui(res_val, self_val, FIX2NUM(exp), mod_val);
} else if (BIGNUM_P(exp)) {
mpz_temp_from_bignum(exp_val, exp);
mpz_powm(res_val, self_val, exp_val, mod_val);
mpz_temp_free(exp_val);
} else {
if (free_mod_val)
mpz_temp_free(mod_val);
typeerror_as(ZXB, "exponent");
}
if (free_mod_val)
mpz_temp_free(mod_val);
return res;
}
|
#probab_prime?(reps = 5) ⇒ Boolean
Determine whether n is prime. Returns 2 if n is definitely prime, returns 1 if n is probably prime (without being certain), or returns 0 if n is definitely composite.
This function does some trial divisions, then some Miller-Rabin probabilistic primality tests. ‘reps` controls how many such tests are done, 5 to 10 is a reasonable number, more will reduce the chances of a composite being returned as “probably prime”.
Miller-Rabin and similar tests can be more properly called compositeness tests. Numbers which fail are known to be composite but those which pass might be prime or might be composite. Only a few composites pass, hence those which pass are considered probably prime.
#remove(f) ⇒ Object
Remove all occurrences of the factor f from n, returning the result as r. t, the number of occurrences that were removed, is also returned.
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 |
# File 'ext/gmpz.c', line 2229
VALUE r_gmpz_remove(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val, *res_val;
VALUE res;
#if __GNU_MP_VERSION>2
unsigned long removed_val;
#else
int removed_val;
#endif
int free_arg_val = 0;
arg_val = NULL;
mpz_get_struct (self, self_val);
if (GMPZ_P (arg)) {
mpz_get_struct (arg,arg_val);
if (mpz_sgn (arg_val) != 1)
rb_raise (rb_eRangeError, "argument must be positive");
} else if (FIXNUM_P (arg)) {
if (FIX2NUM (arg) <= 0)
rb_raise (rb_eRangeError, "argument must be positive");
mpz_temp_alloc (arg_val);
mpz_init_set_ui (arg_val, FIX2NUM (arg));
} else if (BIGNUM_P (arg)) {
mpz_temp_from_bignum (arg_val, arg);
if (mpz_sgn (arg_val) != 1) {
mpz_temp_free (arg_val);
rb_raise (rb_eRangeError, "argument must be positive");
}
} else {
typeerror (ZXB);
}
mpz_make_struct_init (res, res_val);
removed_val = mpz_remove (res_val, self_val, arg_val);
if (free_arg_val)
mpz_temp_free (arg_val);
return rb_assoc_new (res, INT2FIX (removed_val));
}
|
#root(b) ⇒ Object
Returns the truncated integer part of the _b_th root of a.
#rootrem(b) ⇒ Object
Returns the truncated integer part of the _b_th root of a, and the remainder, _a - root**b_.
#scan0(starting_bit) ⇒ Object
Scan a, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found. Return the index of the found bit.
If the bit at starting_bit is already what’s sought, then starting_bit is returned.
If there’s no bit found, then ‘INT2FIX(ULONG_MAX)` is returned. This will happen in #scan0 past the end of a negative number.
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 |
# File 'ext/gmpz.c', line 2752
VALUE r_gmpz_scan0(VALUE self, VALUE bitnr)
{
MP_INT *self_val;
int bitnr_val = 0;
mpz_get_struct (self, self_val);
if (FIXNUM_P (bitnr)) {
bitnr_val = FIX2INT (bitnr);
} else {
typeerror_as (X, "index");
}
return INT2FIX (mpz_scan0 (self_val, bitnr_val));
}
|
#scan1(starting_bit) ⇒ Object
Scan a, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found. Return the index of the found bit.
If the bit at starting_bit is already what’s sought, then starting_bit is returned.
If there’s no bit found, then ‘INT2FIX(ULONG_MAX)` is returned. This will happen in scan1 past the end of a nonnegative number.
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 |
# File 'ext/gmpz.c', line 2780
VALUE r_gmpz_scan1(VALUE self, VALUE bitnr)
{
MP_INT *self_val;
int bitnr_val = 0;
mpz_get_struct (self, self_val);
if (FIXNUM_P (bitnr)) {
bitnr_val = FIX2INT (bitnr);
} else {
typeerror_as (X, "index");
}
return INT2FIX (mpz_scan1 (self_val, bitnr_val));
}
|
#sgn ⇒ Object
Returns +1 if a > 0, 0 if a == 0, and -1 if a < 0.
2588 2589 2590 2591 2592 2593 |
# File 'ext/gmpz.c', line 2588
VALUE r_gmpz_sgn(VALUE self)
{
MP_INT *self_val;
mpz_get_struct(self, self_val);
return INT2FIX(mpz_sgn(self_val));
}
|
#size ⇒ Object
Return the size of a measured in number of limbs. If a is zero, the returned value will be zero.
3079 3080 3081 3082 3083 3084 |
# File 'ext/gmpz.c', line 3079
VALUE r_gmpz_size(VALUE self)
{
MP_INT *self_val;
mpz_get_struct(self, self_val);
return INT2FIX(mpz_size(self_val));
}
|
#size_in_bin ⇒ Object
Return the size of a measured in number of digits in binary. The sign of a is ignored, just the absolute value is used. If a is zero the return value is 1.
3057 3058 3059 3060 3061 3062 |
# File 'ext/gmpz.c', line 3057
VALUE r_gmpz_size_in_bin(VALUE self)
{
MP_INT *self_val;
mpz_get_struct (self, self_val);
return INT2FIX (mpz_sizeinbase (self_val, 2));
}
|
#sizeinbase(base) ⇒ Object #size_in_base(base) ⇒ Object Also known as: size_in_base
Return the size of a measured in number of digits in ‘base`. `base` can vary from 2 to 62. The sign of a is ignored, just the absolute value is used. The result will be either exact or 1 too big. If `base` is a power of 2, the result is always exact. If a is zero the return value is always 1.
#sqrt ⇒ Object
Returns the truncated integer part of the square root of a.
#sqrt! ⇒ Object
Sets a to the truncated integer part of its square root.
#sqrtrem ⇒ Object
Returns the truncated integer part of the square root of a as s and the remainder _a - s * s_ as r, which will be zero if a is a perfect square.
#square? ⇒ Boolean
Returns true if p is a perfect square, i.e., if the square root of p is an integer. Under this definition both 0 and 1 are considered to be perfect squares.
#sub!(b) ⇒ Object
Subtracts b from a in-place, setting a to the difference. b must be an instance of one of:
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 |
# File 'ext/gmpz.c', line 1072
VALUE r_gmpz_sub_self(VALUE self, VALUE arg)
{
MP_INT *self_val, *arg_val;
mpz_get_struct(self,self_val);
if (GMPZ_P(arg)) {
mpz_get_struct(arg, arg_val);
mpz_sub (self_val, self_val, arg_val);
} else if (FIXNUM_P(arg)) {
if (FIX2NUM(arg) > 0)
mpz_sub_ui (self_val, self_val, FIX2NUM(arg));
else
mpz_add_ui (self_val, self_val, -FIX2NUM(arg));
} else if (BIGNUM_P(arg)) {
mpz_temp_from_bignum(arg_val, arg);
mpz_sub (self_val, self_val, arg_val);
mpz_temp_free (arg_val);
} else {
typeerror (ZXB);
}
return Qnil;
}
|
#submul!(b, c) ⇒ Object
Sets a to a minus b times c. b and c must each be an instance of one of
-
GMP::Z
-
Fixnum
-
Bignum
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 |
# File 'ext/gmpz.c', line 1208
static VALUE r_gmpz_submul_self(VALUE self, VALUE b, VALUE c)
{
MP_INT *self_val, *b_val, *c_val;
int free_b_val = 0;
if (GMPZ_P (b)) {
mpz_get_struct (b, b_val);
} else if (FIXNUM_P (b)) {
mpz_temp_alloc (b_val);
mpz_init_set_si (b_val, FIX2NUM (b));
free_b_val = 1;
} else if (BIGNUM_P (b)) {
mpz_temp_from_bignum (b_val, b);
free_b_val = 1;
} else {
typeerror_as (ZXB, "addend");
}
mpz_get_struct (self, self_val);
if (GMPZ_P (c)) {
mpz_get_struct (c, c_val);
mpz_submul (self_val, b_val, c_val);
} else if (FIXNUM_P (c) && FIX2NUM (c) >= 0) {
mpz_submul_ui (self_val, b_val, FIX2NUM (c));
} else if (FIXNUM_P (c) || BIGNUM_P (c)) {
mpz_temp_from_bignum (c_val, c);
mpz_submul (self_val, b_val, c_val);
mpz_temp_free (c_val);
} else {
if (free_b_val)
mpz_temp_free (b_val);
/* TODO?: rb_raise (rb_eTypeError, "base must be a Fixnum between 2 and 62, not a %s.", rb_class2name (rb_class_of (c)));*/
typeerror_as (ZXB, "multiplicand");
}
if (free_b_val)
mpz_temp_free (b_val);
return self;
}
|
#swap(b) ⇒ Object
Efficiently swaps the contents of a with b. b must be an instance of GMP::Z.
793 794 795 796 797 798 799 800 801 802 803 804 |
# File 'ext/gmpz.c', line 793
VALUE r_gmpz_swap(VALUE self_val, VALUE arg_val)
{
MP_INT *self, *arg;
if (!GMPZ_P(arg_val))
rb_raise(rb_eTypeError, "Can't swap GMP::Z with object of other class");
mpz_get_struct(self_val, self);
mpz_get_struct(arg_val, arg);
mpz_swap(self, arg);
return Qnil;
}
|
#tdiv(d) ⇒ Object
Divides n by d, forming a quotient q. tdiv rounds q towards zero. The t stands for “truncate”.
q will satisfy _n=q*d+r_, and r will satisfy _0 <= abs(r ) < abs(d)_.
This function calculates only the quotient.
#tmod(d) ⇒ Object
Divides n by d, forming a remainder r. r will have the same sign as n. The t stands for “truncate”.
r will satisfy _n=q*d+r_, and r will satisfy _0 <= abs(r ) < abs(d)_.
This function calculates only the remainder.
The remainder can be negative, so the return value is the absolute value of the remainder.
#to_d ⇒ Object
Implement mpz_fits_slong_p
Returns a as a Float if a fits in a Float.
Otherwise returns the least significant part of a, with the same sign as a.
If a is too big to fit in a Float, the returned result is probably not very useful. To find out if the value will fit, use the function ‘mpz_fits_slong_p` (Unimplemented).
863 864 865 866 867 868 869 |
# File 'ext/gmpz.c', line 863
VALUE r_gmpz_to_d(VALUE self)
{
MP_INT *self_val;
mpz_get_struct(self, self_val);
return rb_float_new(mpz_get_d(self_val));
}
|
#to_i ⇒ Object
Implement mpz_fits_slong_p
Returns a as an Fixnum if a fits into a Fixnum.
Otherwise returns the least significant part of a, with the same sign as a.
If a is too big to fit in a Fixnum, the returned result is probably not very useful. To find out if the value will fit, use the function ‘mpz_fits_slong_p` (Unimplemented).
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 |
# File 'ext/gmpz.c', line 826
VALUE r_gmpz_to_i(VALUE self)
{
MP_INT *self_val;
char *str;
VALUE res;
mpz_get_struct (self, self_val);
if (mpz_fits_slong_p (self_val)) {
#ifdef RUBY_ENGINE_JRUBY
/* JRuby has this as INT2FIX which is no good. Patch. */
return FIXABLE (mpz_get_si (self_val)) ? LONG2FIX (mpz_get_si (self_val)) : rb_ll2inum (mpz_get_si (self_val));
#else
return rb_int2inum (mpz_get_si (self_val));
#endif
}
str = mpz_get_str (NULL, 0, self_val);
res = rb_cstr2inum (str, 10);
free (str);
return res;
}
|
#to_s(base = 10) ⇒ Object #to_s(: bin) ⇒ Object #to_s(: oct) ⇒ Object #to_s(: dec) ⇒ Object #to_s(: hex) ⇒ Object Also known as: inspect
Returns a, as a String. If ‘base` is not provided, then the decimal representation will be returned.
From the GMP Manual:
Convert a to a string of digits in base ‘base`. The `base` argument may vary from 2 to 62 or from -2 to -36.
For ‘base` in the range 2..36, digits and lower-case letters are used; for -2..-36, digits and upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters (in that significance order) are used.
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
# File 'ext/gmpz.c', line 893
VALUE r_gmpz_to_s(int argc, VALUE *argv, VALUE self_val)
{
MP_INT *self;
char *str;
VALUE res;
VALUE base_val;
unsigned int base;
rb_scan_args(argc, argv, "01", &base_val);
if (NIL_P(base_val)) { base = 10; } /* default value */
else { base = get_base(base_val); }
Data_Get_Struct(self_val, MP_INT, self);
str = mpz_get_str(NULL, base, self);
res = rb_str_new2(str);
free (str);
return res;
}
|
#tshr(d) ⇒ Object
Divides n by _2^d_, forming a quotient q. tshr rounds q towards zero. The t stands for “truncate”.
q will satisfy _n=q*d+r_, and r will satisfy _0 <= abs(r ) < abs(d)_.
This function calculates only the quotient.
#|(b) ⇒ Object
Returns a bitwise inclusive-or b. b must be an instance of one of the following:
-
GMP::Z
-
Fixnum
-
Bignum