1 /* Copyright (c) 2008 Jérémie Laval, Marine Jourdain
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "biginteger.h"
29 #include "biginteger_utils.h"
31 // Uses a version of Knuth LongDivision algorithm found in the Art of Computer Programming vol2
32 BigInteger
quotientBigInt(BigInteger u
, BigInteger v
)
34 // Special cases due to comparison
35 int comparison
= compareBigInt(u
, v
);
37 // Since we are only interested in integer number if
38 // b is greater than a the result is automatically rounded to 0
40 return comparison
< 0 ? CreateZeroBigInt() : newBigInteger("1");
42 BigInteger quotient
= CreateZeroBigInt();
43 quotient
.Sign
= (Sign
)(u
.Sign
* v
.Sign
);
45 // Special case of u.Count == 1 and v.Count == 1
46 if (u
.Count
== 1 && v
.Count
== 1) {
47 unsigned int result
= u
.AbsValStart
->Value
/ v
.AbsValStart
->Value
;
48 PushFrontNode("ient
, result
);
52 // Special case of u.Count > 1 and v.Count == 1
55 BigUnsignedInteger
* unode
= u
.AbsValEnd
;
58 for (; j
<= u
.Count
; j
++) {
59 PushFrontNode("ient
, (r
* BASE
+ unode
->Value
) / v
.AbsValEnd
->Value
);
60 r
= (r
* BASE
+ unode
->Value
) % v
.AbsValEnd
->Value
;
61 unode
= unode
->Previous
;
70 unsigned int divisor
= BASE
/ (v
.AbsValEnd
->Value
+ 1);
72 BigInteger uprime
= mulBigIntByScalar(u
.AbsValStart
, divisor
);
73 BigInteger vprime
= mulBigIntByScalar(v
.AbsValStart
, divisor
);
76 if (uprime
.Count
== u
.Count
)
78 PushEndNode(&uprime
, 0);
79 // Normalize vprime (unecessary due to how d is computed)
80 if (vprime
.Count
> v
.Count
)
81 // It's only a supplementary node
85 unsigned int v1
= vprime
.AbsValEnd
->Value
;
86 unsigned int v2
= vprime
.AbsValEnd
->Previous
->Value
;
88 // Used to get sub-biginteger of uprime in the loop
90 int m
= u
.Count
- v
.Count
;
93 BigUnsignedInteger
* ujNode
= uprime
.AbsValEnd
;
96 unsigned int q
= BASE
- 1;
97 unsigned int uj
= ujNode
->Value
;
100 q
= (uj
* BASE
+ ujNode
->Previous
->Value
) / v1
;
103 while (v2
* q
> (uj
* BASE
+ ujNode
->Previous
->Value
- q
* v1
) * BASE
+ ujNode
->Previous
->Previous
->Value
)
106 BigInteger slice
= CreateZeroBigInt();
107 slice
.Sign
= Positive
;
108 CopyBigIntegerFromEnd(uprime
, &slice
, ujNode
, n
+ 1);
110 BigInteger qv
= mulBigIntByScalar(vprime
.AbsValStart
, q
);
111 slice
= diffBigInt(slice
, qv
);
113 ujNode
= ReplaceFromEnd(&uprime
, slice
, ujNode
->Next
, n
);
115 PushFrontNode("ient
, q
);