gmp 0.2.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +33 -0
- data/README.rdoc +117 -17
- data/benchmark/COPYING +674 -0
- data/benchmark/README +75 -0
- data/benchmark/divide +34 -0
- data/benchmark/gcd +38 -0
- data/benchmark/gexpr +0 -0
- data/benchmark/gexpr.c +359 -0
- data/benchmark/multiply +44 -0
- data/benchmark/rsa +93 -0
- data/benchmark/runbench +147 -0
- data/benchmark/version +1 -0
- data/ext/gmp.c +10 -8
- data/ext/gmpbench_timing.c +80 -0
- data/ext/gmprandstate.c +224 -0
- data/ext/gmpz.c +170 -61
- data/ext/ruby_gmp.h +43 -1
- data/manual.pdf +0 -0
- data/manual.tex +214 -20
- data/test/README +4 -11
- data/test/tc_division.rb +109 -0
- data/test/tc_random.rb +54 -0
- data/test/tc_z_gcd_lcm_invert.rb +57 -0
- data/test/test-12.rb +14 -0
- data/test/test-19.rb +13 -0
- data/test/test-20.rb +29 -0
- data/test/test-21.rb +37 -0
- data/test/test-22.rb +12 -0
- data/test/test-23.rb +11 -0
- data/test/test_helper.rb +6 -0
- data/test/unit_tests.rb +9 -94
- metadata +31 -10
data/manual.pdf
CHANGED
Binary file
|
data/manual.tex
CHANGED
@@ -19,6 +19,8 @@
|
|
19
19
|
\def\qquad{\quad\quad}
|
20
20
|
\def\qqqquad{\quad\quad\quad\quad}
|
21
21
|
\def\cc{\colon\colon}
|
22
|
+
\def\gmpz{\textit{GMP::Z}}
|
23
|
+
\def\gmprandstate{\textit{GMP::RandState\ }}
|
22
24
|
\frenchspacing
|
23
25
|
\begin{document}
|
24
26
|
|
@@ -26,8 +28,8 @@
|
|
26
28
|
\huge{gmp} &\\
|
27
29
|
\midrule[3pt]
|
28
30
|
\multicolumn{2}{r}{\large{Ruby bindings to the GMP library}}\\
|
29
|
-
\multicolumn{2}{r}{\large{Edition 0.
|
30
|
-
\multicolumn{2}{r}{\large{
|
31
|
+
\multicolumn{2}{r}{\large{Edition 0.4.0}}\\
|
32
|
+
\multicolumn{2}{r}{\large{26 February 2010}}
|
31
33
|
\end{tabular}
|
32
34
|
|
33
35
|
\vfill
|
@@ -37,7 +39,7 @@
|
|
37
39
|
|
38
40
|
\vfill
|
39
41
|
This manual describes how to use the gmp Ruby gem, which provides bindings to
|
40
|
-
the GNU multiple precision arithmetic library, version 4.3.x.\\
|
42
|
+
the GNU multiple precision arithmetic library, version 4.3.x or 5.0.x.\\
|
41
43
|
\\
|
42
44
|
Copyright 2009 Sam Rawlins.\\
|
43
45
|
No license yet.
|
@@ -114,8 +116,7 @@ of the following versions of Ruby:
|
|
114
116
|
\begin{itemize}
|
115
117
|
\item (MRI) Ruby 1.8.6 - tested lightly.
|
116
118
|
\item (MRI) Ruby 1.8.7 - tested seriously.
|
117
|
-
\item (MRI) Ruby 1.9.1 -
|
118
|
-
bugs.
|
119
|
+
\item (MRI) Ruby 1.9.1 - tested seriously.
|
119
120
|
\end{itemize}
|
120
121
|
As you can see only Matz's Ruby Interpreter (MRI) is supported. I haven't even
|
121
122
|
put a thought into trying other interpreters/VMs. I intend to look into FFI,
|
@@ -126,14 +127,19 @@ Next is the platform, the combination of the architecture (processor) and OS.
|
|
126
127
|
As far as I can tell, if you can compile GMP and Ruby on a given platform, you
|
127
128
|
can use the gmp gem there too. Please report problems with that hypothesis.\\
|
128
129
|
|
129
|
-
Lastly, GMP. GMP must be compiled and working. "and working" means you ran "make
|
130
|
+
Lastly, GMP. GMP must be compiled and working. "and working" means you ran "make
|
131
|
+
check" while installing GMP. The following versions of GMP have been tested:
|
130
132
|
\begin{itemize}
|
131
133
|
\item GMP 4.3.1
|
134
|
+
\item GMP 4.3.2
|
135
|
+
\item GMP 5.0.0
|
136
|
+
\item GMP 5.0.1
|
132
137
|
\end{itemize}
|
133
138
|
|
134
|
-
That's all. I don't intend to test any older versions, maybe 4.3.0 for
|
139
|
+
That's all. I don't intend to test any older versions, maybe 4.3.0 for
|
140
|
+
completeness.\\
|
135
141
|
|
136
|
-
Here is a table of the exact environments I have tested the gmp gem
|
142
|
+
Here is a table of the exact environments on which I have tested the gmp gem:\\\\
|
137
143
|
|
138
144
|
\begin{tabular}{lrr} \hline
|
139
145
|
Platform & Ruby & GMP \\ \midrule[1pt]
|
@@ -141,6 +147,7 @@ Here is a table of the exact environments I have tested the gmp gem on:\\\\
|
|
141
147
|
Linux (LinuxMint 7) on x86 & (MRI) Ruby 1.8.7 & GMP 4.3.1 \\ \hline
|
142
148
|
Mac OS X 10.5.7 on x86 (32-bit) & (MRI) Ruby 1.8.6 & GMP 4.3.1 \\ \hline
|
143
149
|
Mac OS X 10.5.7 on x86 (32-bit) & (MRI) Ruby 1.9.1 & GMP 4.3.1 \\ \hline
|
150
|
+
Windows XP on x86 (32-bit) & (MRI) Ruby 1.9.1 & GMP 5.0.1 \\ \hline
|
144
151
|
\end{tabular}
|
145
152
|
|
146
153
|
\subsection{Installing}
|
@@ -148,23 +155,39 @@ You may clone the gmp gem's git repository with:\\
|
|
148
155
|
\\
|
149
156
|
\texttt{git clone git://github.com/srawlins/gmp.git}\\
|
150
157
|
|
151
|
-
Or you may install the gem from
|
158
|
+
Or you may install the gem from gemcutter:\\
|
159
|
+
\\
|
160
|
+
\texttt{gem install gmp}\\
|
161
|
+
|
162
|
+
Or you may install the gem from github (old):\\
|
152
163
|
\\
|
153
164
|
\texttt{gem install srawlins-gmp}\\
|
154
165
|
|
155
166
|
At this time, the gem does not self-compile (how does that work?). To compile the C
|
156
167
|
extensions, do the following:\\
|
157
168
|
\\
|
158
|
-
\texttt{cd <
|
169
|
+
\texttt{cd <gmp gem directory>/ext}\\
|
159
170
|
\texttt{ruby extconf.rb}\\
|
160
171
|
\texttt{make}\\
|
161
172
|
|
162
173
|
There shouldn't be any errors, or warnings.
|
163
174
|
|
175
|
+
\section{Testing the gmp gem}
|
176
|
+
|
177
|
+
Testing the gmp gem is quite simple. The test/unit\_tests.rb suite uses Unit::Test.
|
178
|
+
You can run this test suite with:\\
|
179
|
+
\\
|
180
|
+
\texttt{cd <gmp gem directory>/test}\\
|
181
|
+
\texttt{ruby unit\_tests.rb}\\
|
182
|
+
|
183
|
+
All tests should pass. If you don't have the test-unit gem installed, then you may
|
184
|
+
run into one error. I'm not sure why this is... I suspect a bug in Ruby's Test::Unit
|
185
|
+
that the test-unit gem monkey patches.
|
186
|
+
|
164
187
|
\section{GMP and gmp gem basics}
|
165
188
|
|
166
189
|
\subsection{Classes}
|
167
|
-
The gmp gem includes the namespace \texttt{GMP} and
|
190
|
+
The gmp gem includes the namespace \texttt{GMP} and four classes within \texttt{GMP}:
|
168
191
|
\begin{itemize}
|
169
192
|
\item \texttt{GMP::Z} - Methods for signed integer arithmetic. There are at least 45
|
170
193
|
methods here (still accounting).
|
@@ -172,9 +195,11 @@ The gmp gem includes the namespace \texttt{GMP} and three classes within \texttt
|
|
172
195
|
methods here (still accounting).
|
173
196
|
\item \texttt{GMP::F} - Methods for floating-point arithmetic. There are at least 6
|
174
197
|
methods here (still accounting).
|
198
|
+
\item \texttt{GMP::RandState} - Methods for random number generation. There are 3
|
199
|
+
methods here.
|
175
200
|
\end{itemize}
|
176
201
|
|
177
|
-
In addition to the above
|
202
|
+
In addition to the above four classes, there is also one constant within \texttt{GMP}:
|
178
203
|
\begin{itemize}
|
179
204
|
\item \texttt{GMP::GMP\_VERSION} - The version of GMP compiled into the gmp gem.
|
180
205
|
\end{itemize}
|
@@ -415,7 +440,7 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
415
440
|
\cmidrule(r){2-3}
|
416
441
|
& \multicolumn{2}{p{\defnwidth}}{
|
417
442
|
Returns the remainder after truncated division of $integer$ by $numeric$. $numeric$ can
|
418
|
-
be an instance of $GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always
|
443
|
+
be an instance of $GMP::Z$, $Fixnum$, or $Bignum$. The return object's class is always
|
419
444
|
$GMP::Z$.
|
420
445
|
}
|
421
446
|
\end{tabular}
|
@@ -427,7 +452,7 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
427
452
|
\cmidrule(r){2-3}
|
428
453
|
& \multicolumn{2}{p{\defnwidth}}{
|
429
454
|
Returns the remainder after floored division of $integer$ by $numeric$. $numeric$ can
|
430
|
-
be an instance of $GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always
|
455
|
+
be an instance of $GMP::Z$, $Fixnum$, or $Bignum$. The return object's class is always
|
431
456
|
$GMP::Z$.
|
432
457
|
}
|
433
458
|
\end{tabular}
|
@@ -439,10 +464,21 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
439
464
|
\cmidrule(r){2-3}
|
440
465
|
& \multicolumn{2}{p{\defnwidth}}{
|
441
466
|
Returns the remainder after ceilinged division of $integer$ by $numeric$. $numeric$ can
|
442
|
-
be an instance of $GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always
|
467
|
+
be an instance of $GMP::Z$, $Fixnum$, or $Bignum$. The return object's class is always
|
443
468
|
$GMP::Z$.
|
444
469
|
}
|
445
470
|
\end{tabular}
|
471
|
+
\newline\newline
|
472
|
+
|
473
|
+
\begin{tabular}{p{\methwidth} l r}
|
474
|
+
\toprule
|
475
|
+
\textbf{\%} & & \textit{integer} \% \textit{numeric} $\rightarrow$ \textit{integer}\\
|
476
|
+
\cmidrule(r){2-3}
|
477
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
478
|
+
Returns $integer$ modulo $numeric$. $numeric$ can be an instance of \gmpz, $Fixnum$,
|
479
|
+
or $Bignum$. The return object's class is always \gmpz.
|
480
|
+
}
|
481
|
+
\end{tabular}
|
446
482
|
|
447
483
|
\subsection{Integer Roots}
|
448
484
|
|
@@ -568,6 +604,36 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
568
604
|
\end{tabular}
|
569
605
|
\newline\newline
|
570
606
|
|
607
|
+
\begin{tabular}{p{\methwidth} l r}
|
608
|
+
\toprule
|
609
|
+
\textbf{gcd} & & $a$.gcd($b$) $\rightarrow$ $g$ \\
|
610
|
+
\cmidrule(r){2-3}
|
611
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
612
|
+
Computes the greatest common divisor of $a$ and $b$. $g$ will always be positive, even
|
613
|
+
if $a$ or $b$ is negative. $b$ can be an instance of \gmpz, $Fixnum$, or $Bignum$.
|
614
|
+
\newline\newline
|
615
|
+
\texttt{GMP::Z(24).gcd(GMP::Z(8)) \quad \#=> GMP::Z(8) \newline
|
616
|
+
GMP::Z(24).gcd(8) \quad \#=> GMP::Z(8) \newline
|
617
|
+
GMP::Z(24).gcd(2**32) \quad \#=> GMP::Z(8)}
|
618
|
+
}
|
619
|
+
\end{tabular}
|
620
|
+
\newline\newline
|
621
|
+
|
622
|
+
\begin{tabular}{p{\methwidth} l r}
|
623
|
+
\toprule
|
624
|
+
\textbf{invert} & & $a$.invert($m$) $\rightarrow$ $integer$ \\
|
625
|
+
\cmidrule(r){2-3}
|
626
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
627
|
+
Computes the inverse of $a$ mod $m$. $m$ can be an instance of \gmpz, $Fixnum$, or
|
628
|
+
$Bignum$.
|
629
|
+
\newline\newline
|
630
|
+
\texttt{GMP::Z(2).invert(GMP::Z(11)) \quad \#=> GMP::Z(6) \newline
|
631
|
+
GMP::Z(3).invert(11) \quad \#=> GMP::Z(4) \newline
|
632
|
+
GMP::Z(5).invert(11) \quad \#=> GMP::Z(9)}
|
633
|
+
}
|
634
|
+
\end{tabular}
|
635
|
+
\newline\newline
|
636
|
+
|
571
637
|
\begin{tabular}{p{\methwidth} l r}
|
572
638
|
\toprule
|
573
639
|
\textbf{jacobi} & & $a$.jacobi($b$) $\rightarrow$ $integer$ \\
|
@@ -577,10 +643,10 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
577
643
|
Returns the Jacobi symbol $(a/b)$. This is defined only for $b$ odd. If $b$ is even, a
|
578
644
|
range exception will be raised.
|
579
645
|
\newline\newline
|
580
|
-
|
646
|
+
\textit{GMP::Z.jacobi} (the instance method) requires $b$ to be an instance of \gmpz.
|
581
647
|
\newline
|
582
|
-
|
583
|
-
|
648
|
+
\textit{GMP::Z\#jacobi} (the class method) requires $a$ and $b$ each to be an instance of
|
649
|
+
\gmpz, $Fixnum$, or $Bignum$.
|
584
650
|
}
|
585
651
|
\end{tabular}
|
586
652
|
\newline\newline
|
@@ -602,8 +668,136 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
602
668
|
\cmidrule(r){2-3}
|
603
669
|
& \multicolumn{2}{p{\defnwidth}}{
|
604
670
|
Remove all occurrences of the factor $factor$ from $n$. $factor$ can be an instance of
|
605
|
-
|
606
|
-
|
671
|
+
\gmpz, $Fixnum$, or $Bignum$. $integer$ is the resulting integer, an instance of
|
672
|
+
\gmpz. $times$ is how many times $factor$ was removed, a $Fixnum$.
|
673
|
+
}
|
674
|
+
\end{tabular}
|
675
|
+
\newline\newline
|
676
|
+
|
677
|
+
\begin{tabular}{p{\methwidth} l r}
|
678
|
+
\toprule
|
679
|
+
\textbf{fac} & & GMP::Z.fac($n$) $\rightarrow$ $integer$ \\
|
680
|
+
\cmidrule(r){2-3}
|
681
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
682
|
+
Returns $n!$, or, $n$ factorial.
|
683
|
+
}
|
684
|
+
\end{tabular}
|
685
|
+
\newline\newline
|
686
|
+
|
687
|
+
\begin{tabular}{p{\methwidth} l r}
|
688
|
+
\toprule
|
689
|
+
\textbf{fib} & & GMP::Z.fib($n$) $\rightarrow$ $integer$ \\
|
690
|
+
\cmidrule(r){2-3}
|
691
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
692
|
+
Returns $F[n]$, or, the $n$th Fibonacci number.
|
693
|
+
}
|
694
|
+
\end{tabular}
|
695
|
+
|
696
|
+
|
697
|
+
|
698
|
+
\subsection{Integer Logic and Bit Fiddling}
|
699
|
+
|
700
|
+
\begin{tabular}{p{\methwidth} l r}
|
701
|
+
\toprule
|
702
|
+
\textbf{and} & & $a$ \& $b$ $\rightarrow$ $integer$ \\
|
703
|
+
\cmidrule(r){2-3}
|
704
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
705
|
+
Returns $integer$, the bitwise and of $a$ and $b$.
|
706
|
+
}
|
707
|
+
\end{tabular}
|
708
|
+
\newline\newline
|
709
|
+
|
710
|
+
\begin{tabular}{p{\methwidth} l r}
|
711
|
+
\toprule
|
712
|
+
\textbf{ior} & & $a$ \textbar\ $b$ $\rightarrow$ $integer$ \\
|
713
|
+
\cmidrule(r){2-3}
|
714
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
715
|
+
Returns $integer$, the bitwise inclusive or of $a$ and $b$.
|
716
|
+
}
|
717
|
+
\end{tabular}
|
718
|
+
\newline\newline
|
719
|
+
|
720
|
+
\begin{tabular}{p{\methwidth} l r}
|
721
|
+
\toprule
|
722
|
+
\textbf{xor} & & $a$ \textasciicircum\ $b$ $\rightarrow$ $integer$ \\
|
723
|
+
\cmidrule(r){2-3}
|
724
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
725
|
+
Returns $integer$, the bitwise exclusive or of $a$ and $b$.
|
726
|
+
}
|
727
|
+
\end{tabular}
|
728
|
+
\newline\newline
|
729
|
+
|
730
|
+
\begin{tabular}{p{\methwidth} l r}
|
731
|
+
\toprule
|
732
|
+
\textbf{scan0} & & $n$.scan0($i$) $\rightarrow$ $integer$ \\
|
733
|
+
\cmidrule(r){2-3}
|
734
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
735
|
+
Scans $n$, starting from bit $i$, towards more significant bits, until the first 0 bit
|
736
|
+
is found. Return the index of the found bit.
|
737
|
+
\newline\newline
|
738
|
+
If the bit at $i$ is already what's sought, then $i$ is returned.
|
739
|
+
\newline\newline
|
740
|
+
If there's no bit found, then $INT2FIX(ULONG\_MAX)$ is returned. This will happen in
|
741
|
+
scan0 past the end of a negative number.
|
742
|
+
}
|
743
|
+
\end{tabular}
|
744
|
+
|
745
|
+
\newpage
|
746
|
+
\section{Random Number Functions}
|
747
|
+
|
748
|
+
\subsection{Random State Initialization}
|
749
|
+
|
750
|
+
\begin{tabular}{p{\methwidth} l r}
|
751
|
+
\toprule
|
752
|
+
\textbf{new} & & GMP::RandState.new $\rightarrow$ \textit{mersenne twister state} \\
|
753
|
+
& & GMP::RandState.new(:default) $\rightarrow$ \textit{mersenne twister state} \\
|
754
|
+
& & GMP::RandState(:mt) $\rightarrow$ \textit{mersenne twister random state} \\
|
755
|
+
& & GMP::RandState.new(:lc\_2exp, a, c, m2exp) $\rightarrow$ \textit{linear congruential state} \\
|
756
|
+
& & GMP::RandState.new(:lc\_2exp\_size, size) $\rightarrow$ \textit{linear congruential state} \\
|
757
|
+
\cmidrule(r){2-3}
|
758
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
759
|
+
This method creates a new \gmprandstate instance. The first argument defaults
|
760
|
+
to \textit{:default} (also \textit{:mt}), which initializes the \gmprandstate for a Mersenne
|
761
|
+
Twister algorithm. No other arguments should be given if \textit{:default} or \textit{:mt} is
|
762
|
+
specified.
|
763
|
+
\newline\newline
|
764
|
+
If the first argument given is \textit{:lc\_2exp}, then the \gmprandstate is
|
765
|
+
initialized for a linear congruential algorithm. \textit{:lc\_2exp} must be followed with $a$,
|
766
|
+
$c$, and $m2exp$. The algorithm can then proceed as ($X = (a*X + c) \mod 2^{m2exp}$).
|
767
|
+
\newline\newline
|
768
|
+
\gmprandstate can also be initialized for a linear congruential algorithm with
|
769
|
+
\textit{:lc\_2exp\_size}. This initializer instead takes just one argument, $size$. $a$, $c$,
|
770
|
+
and $m2exp$ are then chosen from a table, with $m2exp/2 > size$. The maximum size
|
771
|
+
currently supported is 128.\newline
|
772
|
+
|
773
|
+
\texttt{GMP::RandState.new \newline
|
774
|
+
GMP::RandState.new(:mt) \newline
|
775
|
+
GMP::RandState.new(:lc\_2exp, 1103515245, 12345, 15) \quad \#=> Perl's old rand() \newline
|
776
|
+
GMP::RandState.new(:lc\_2exp, 25\_214\_903\_917, 11, 48) \quad \#=> drand48}
|
777
|
+
}
|
778
|
+
\end{tabular}
|
779
|
+
|
780
|
+
\subsection{Random State Seeding}
|
781
|
+
|
782
|
+
\begin{tabular}{p{\methwidth} l r}
|
783
|
+
\toprule
|
784
|
+
\textbf{seed} & & $state$.seed($integer$) $\rightarrow$ $integer$ \\
|
785
|
+
\cmidrule(r){2-3}
|
786
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
787
|
+
Set an initial seed value into $state$. $integer$ can be an instance of \gmpz,
|
788
|
+
$Fixnum$, or $Bignum$.
|
789
|
+
}
|
790
|
+
\end{tabular}
|
791
|
+
|
792
|
+
\subsection{Integer Random Numbers}
|
793
|
+
|
794
|
+
\begin{tabular}{p{\methwidth} l r}
|
795
|
+
\toprule
|
796
|
+
\textbf{urandomb} & & $state$.urandomb($n$) $\rightarrow$ $n$ \\
|
797
|
+
\cmidrule(r){2-3}
|
798
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
799
|
+
Generates a uniformly distributed random integer in the range $0$ to $2^n -1$,
|
800
|
+
inclusive.
|
607
801
|
}
|
608
802
|
\end{tabular}
|
609
803
|
|
data/test/README
CHANGED
@@ -1,10 +1,3 @@
|
|
1
|
-
TODO: Port everything to Test::Unit framework
|
2
|
-
Test files 1-6 ported.
|
3
|
-
|
4
|
-
Current version passes:
|
5
|
-
1-23
|
6
|
-
Current version fails on these tests because:
|
7
|
-
none
|
8
1
|
Tests:
|
9
2
|
1* initializations and to_s
|
10
3
|
2* basic integer arithmetics and coersion to Z
|
@@ -24,18 +17,18 @@ Tests:
|
|
24
17
|
16* floating point init with and without explicit precision
|
25
18
|
17* basic floating point arithmetics and coersion to F
|
26
19
|
18* default_prec and default_prec=
|
27
|
-
19
|
20
|
+
19* integer division methods
|
28
21
|
20 mpfr - exp and log
|
29
22
|
21 mpfr - trigonometry
|
30
23
|
22 mpfr - type tests
|
31
24
|
23 mpfr - sqrt and power
|
32
|
-
24
|
25
|
+
24* jacobi, legendre, remove
|
26
|
+
25* gcd, gcdext, lcm, invert
|
33
27
|
TODO:
|
34
28
|
gcd, lcm
|
35
29
|
cmpabs
|
36
30
|
integer and integer->rational division, modulo
|
37
31
|
more number theoretic functions
|
38
32
|
random numbers
|
39
|
-
range
|
33
|
+
range errors
|
40
34
|
full coverage?
|
41
|
-
use more assert_in_delta
|
data/test/tc_division.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TC_division < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@a = GMP::Z.new(5)
|
6
|
+
@b = GMP::Z.new(7)
|
7
|
+
@c = GMP::Z.new(25)
|
8
|
+
@d = GMP::Q.new(3,11)
|
9
|
+
@e = GMP::F.new(3.14)
|
10
|
+
@f = 2**32
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_z_div
|
14
|
+
assert_equal(GMP::Q, (@a / @b ).class, "GMP::Z / GMP::Z should be GMP::Q.")
|
15
|
+
assert_equal(GMP::Q, (@a / 3 ).class, "GMP::Z / Fixnum should be GMP::Q.")
|
16
|
+
assert_equal(GMP::Q, (@a / 2**32).class, "GMP::Z / Bignum should be GMP::Q.")
|
17
|
+
assert_equal(GMP::Q, (@a / @c ).class, "GMP::Z / GMP::Z should be GMP::Q.")
|
18
|
+
assert_in_delta(0.7142857142, @a / @b, 1e-7, "GMP::Z./ should work.")
|
19
|
+
assert_in_delta(1.4 , @b / @a, 1e-7, "GMP::Z./ should work.")
|
20
|
+
assert_in_delta(1.6666666667, @a / 3, 1e-7, "GMP::Z./ should work.")
|
21
|
+
assert_in_delta(0.6 , 3 / @a, 1e-7, "GMP::Z./ should work.")
|
22
|
+
assert_in_delta(0.2 , @a / @c, 1e-7, "GMP::Z./ should work.")
|
23
|
+
assert_in_delta(5.0 , @c / @a, 1e-7, "GMP::Z./ should work.")
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_z_tdiv
|
27
|
+
assert_equal(GMP::Z, @a.tdiv(@b).class, "GMP::Z.tdiv GMP::Z should be GMP::Z.")
|
28
|
+
assert_equal(GMP::Z, @a.tdiv(3).class, "GMP::Z.tdiv Fixnum should be GMP::Z.")
|
29
|
+
assert_equal(GMP::Z, @a.tdiv(2**32).class, "GMP::Z.tdiv Bignum should be GMP::Z.")
|
30
|
+
assert_equal(GMP::Z, @a.tdiv(@c).class, "GMP::Z.tdiv GMP::Z should be GMP::Z.")
|
31
|
+
assert_equal(0, @a.tdiv(@b), "GMP::Z.tdiv should work.")
|
32
|
+
assert_equal(1, @b.tdiv(@a), "GMP::Z.tdiv should work.")
|
33
|
+
assert_equal(1, @a.tdiv( 3), "GMP::Z.tdiv should work.")
|
34
|
+
assert_equal(0, @a.tdiv(@c), "GMP::Z.tdiv should work.")
|
35
|
+
assert_equal(5, @c.tdiv(@a), "GMP::Z.tdiv should work.")
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_z_fdiv
|
39
|
+
assert_equal(GMP::Z, @a.fdiv(@b).class, "GMP::Z.fdiv GMP::Z should be GMP::Z.")
|
40
|
+
assert_equal(GMP::Z, @a.fdiv(3).class, "GMP::Z.fdiv Fixnum should be GMP::Z.")
|
41
|
+
assert_equal(GMP::Z, @a.fdiv(2**32).class, "GMP::Z.fdiv Bignum should be GMP::Z.")
|
42
|
+
assert_equal(0, @a.fdiv(@b), "GMP::Z.fdiv should work.")
|
43
|
+
assert_equal(1, @b.fdiv(@a), "GMP::Z.fdiv should work.")
|
44
|
+
assert_equal(1, @a.fdiv( 3), "GMP::Z.fdiv should work.")
|
45
|
+
assert_equal(0, @a.fdiv(@c), "GMP::Z.fdiv should work.")
|
46
|
+
assert_equal(5, @c.fdiv(@a), "GMP::Z.fdiv should work.")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_z_cdiv
|
50
|
+
assert_equal(GMP::Z, @a.cdiv(@b).class, "GMP::Z.cdiv GMP::Z should be GMP::Z.")
|
51
|
+
assert_equal(GMP::Z, @a.cdiv(3).class, "GMP::Z.cdiv Fixnum should be GMP::Z.")
|
52
|
+
assert_equal(GMP::Z, @a.cdiv(2**32).class, "GMP::Z.cdiv Bignum should be GMP::Z.")
|
53
|
+
assert_equal(1, @a.cdiv(@b), "GMP::Z.cdiv should work.")
|
54
|
+
assert_equal(2, @b.cdiv(@a), "GMP::Z.cdiv should work.")
|
55
|
+
assert_equal(2, @a.cdiv( 3), "GMP::Z.cdiv should work.")
|
56
|
+
assert_equal(1, @a.cdiv(@c), "GMP::Z.cdiv should work.")
|
57
|
+
assert_equal(5, @c.cdiv(@a), "GMP::Z.cdiv should work.")
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_z_tmod
|
61
|
+
assert_equal(GMP::Z, @a.tmod(@b).class, "GMP::Z.tmod GMP::Z should be GMP::Z.")
|
62
|
+
assert_equal(GMP::Z, @a.tmod(3).class, "GMP::Z.tmod Fixnum should be GMP::Z.")
|
63
|
+
assert_equal(GMP::Z, @a.tmod(2**32).class, "GMP::Z.tmod Bignum should be GMP::Z.")
|
64
|
+
assert_equal(GMP::Z, @a.tmod(@c).class, "GMP::Z.tmod GMP::Z should be GMP::Z.")
|
65
|
+
assert_equal(5, @a.tmod(@b), "GMP::Z.tmod should work.")
|
66
|
+
assert_equal(2, @b.tmod(@a), "GMP::Z.tmod should work.")
|
67
|
+
assert_equal(2, @a.tmod( 3), "GMP::Z.tmod should work.")
|
68
|
+
assert_equal(5, @a.tmod(@c), "GMP::Z.tmod should work.")
|
69
|
+
assert_equal(0, @c.tmod(@a), "GMP::Z.tmod should work.")
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_z_fmod
|
73
|
+
assert_equal(GMP::Z, @a.fmod(@b).class, "GMP::Z.fmod GMP::Z should be GMP::Z.")
|
74
|
+
assert_equal(GMP::Z, @a.fmod(3).class, "GMP::Z.fmod Fixnum should be GMP::Z.")
|
75
|
+
assert_equal(GMP::Z, @a.fmod(2**32).class, "GMP::Z.fmod Bignum should be GMP::Z.")
|
76
|
+
assert_equal(GMP::Z, @a.fmod(@c).class, "GMP::Z.fmod GMP::Z should be GMP::Z.")
|
77
|
+
assert_equal(5, @a.fmod(@b), "GMP::Z.fmod should work.")
|
78
|
+
assert_equal(2, @b.fmod(@a), "GMP::Z.fmod should work.")
|
79
|
+
assert_equal(2, @a.fmod( 3), "GMP::Z.fmod should work.")
|
80
|
+
assert_equal(5, @a.fmod(@c), "GMP::Z.fmod should work.")
|
81
|
+
assert_equal(0, @c.fmod(@a), "GMP::Z.fmod should work.")
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_z_cmod
|
85
|
+
assert_equal(GMP::Z, @a.cmod(@b).class, "GMP::Z.cmod GMP::Z should be GMP::Z.")
|
86
|
+
assert_equal(GMP::Z, @a.cmod(3).class, "GMP::Z.cmod Fixnum should be GMP::Z.")
|
87
|
+
assert_equal(GMP::Z, @a.cmod(2**32).class, "GMP::Z.cmod Bignum should be GMP::Z.")
|
88
|
+
assert_equal(GMP::Z, @a.cmod(@c).class, "GMP::Z.cmod GMP::Z should be GMP::Z.")
|
89
|
+
assert_equal( -2, @a.cmod(@b), "GMP::Z.cmod should work.")
|
90
|
+
assert_equal( -3, @b.cmod(@a), "GMP::Z.cmod should work.")
|
91
|
+
assert_equal( -1, @a.cmod( 3), "GMP::Z.cmod should work.")
|
92
|
+
assert_equal(-20, @a.cmod(@c), "GMP::Z.cmod should work.")
|
93
|
+
assert_equal( 0, @c.cmod(@a), "GMP::Z.cmod should work.")
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_z_mod
|
97
|
+
assert_equal(5, @a % @b, "GMP::Z should % GMP::Z correctly.")
|
98
|
+
assert_equal(2, @b % @a, "GMP::Z should % GMP::Z correctly.")
|
99
|
+
assert_equal(2, @b % 5, "GMP::Z should % Fixnum correctly.")
|
100
|
+
assert_equal(0, @b % @b, "GMP::Z should % to 0 correctly.")
|
101
|
+
assert_equal(0, @c % @a, "GMP::Z should % to 0 correctly.")
|
102
|
+
assert_equal(0, @f % @f, "GMP::Z should % Bignum correctly.")
|
103
|
+
assert_equal(0, 2*@f % @f, "GMP::Z should % Bignum correctly.")
|
104
|
+
assert_equal(5, (5*@f+5) % @f, "GMP::Z should % Bignum correctly.")
|
105
|
+
assert_raise(TypeError) { @a % 3.14 }
|
106
|
+
assert_raise(TypeError) { @a % GMP::Q(22,3) }
|
107
|
+
assert_raise(TypeError) { @a % GMP::F(3.14) }
|
108
|
+
end
|
109
|
+
end
|