-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathBigInt.java
More file actions
186 lines (153 loc) · 4.21 KB
/
BigInt.java
File metadata and controls
186 lines (153 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/* chouser Jun 23, 2010 */
package clojure.lang;
import java.math.BigInteger;
import java.math.BigDecimal;
public final class BigInt extends Number implements IHashEq{
private static final long serialVersionUID = 5097771279236135022L;
final public long lpart;
final public BigInteger bipart;
final public static BigInt ZERO = new BigInt(0,null);
final public static BigInt ONE = new BigInt(1,null);
//must follow Long
public int hashCode(){
if(bipart == null)
return (int) (this.lpart ^ (this.lpart >>> 32));
return bipart.hashCode();
}
public int hasheq(){
if(bipart == null)
return Murmur3.hashLong(lpart);
return bipart.hashCode();
}
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj instanceof BigInt)
{
BigInt o = (BigInt) obj;
if(bipart == null)
return o.bipart == null && this.lpart == o.lpart;
return o.bipart != null && this.bipart.equals(o.bipart);
}
return false;
}
private BigInt(long lpart, BigInteger bipart){
this.lpart = lpart;
this.bipart = bipart;
}
public static BigInt fromBigInteger(BigInteger val){
if(val.bitLength() < 64)
return new BigInt(val.longValue(), null);
else
return new BigInt(0, val);
}
public static BigInt fromLong(long val){
return new BigInt(val, null);
}
public BigInteger toBigInteger(){
if(bipart == null)
return BigInteger.valueOf(lpart);
else
return bipart;
}
public BigDecimal toBigDecimal(){
if(bipart == null)
return BigDecimal.valueOf(lpart);
else
return new BigDecimal(bipart);
}
///// java.lang.Number:
public int intValue(){
if(bipart == null)
return (int) lpart;
else
return bipart.intValue();
}
public long longValue(){
if(bipart == null)
return lpart;
else
return bipart.longValue();
}
public float floatValue(){
if(bipart == null)
return lpart;
else
return bipart.floatValue();
}
public double doubleValue(){
if(bipart == null)
return lpart;
else
return bipart.doubleValue();
}
public byte byteValue(){
if(bipart == null)
return (byte) lpart;
else
return bipart.byteValue();
}
public short shortValue(){
if(bipart == null)
return (short) lpart;
else
return bipart.shortValue();
}
public static BigInt valueOf(long val){
return new BigInt(val, null);
}
public String toString(){
if(bipart == null)
return String.valueOf(lpart);
return bipart.toString();
}
public int bitLength(){
return toBigInteger().bitLength();
}
public BigInt add(BigInt y) {
if ((bipart == null) && (y.bipart == null)) {
long ret = lpart + y.lpart;
if ((ret ^ lpart) >= 0 || (ret ^ y.lpart) >= 0)
return BigInt.valueOf(ret);
}
return BigInt.fromBigInteger(this.toBigInteger().add(y.toBigInteger()));
}
public BigInt multiply(BigInt y) {
if ((bipart == null) && (y.bipart == null)) {
long ret = lpart * y.lpart;
if (y.lpart == 0 ||
(ret / y.lpart == lpart && lpart != Long.MIN_VALUE))
return BigInt.valueOf(ret);
}
return BigInt.fromBigInteger(this.toBigInteger().multiply(y.toBigInteger()));
}
public BigInt quotient(BigInt y) {
if ((bipart == null) && (y.bipart == null)) {
if (lpart == Long.MIN_VALUE && y.lpart == -1)
return BigInt.fromBigInteger(this.toBigInteger().negate());
return BigInt.valueOf(lpart / y.lpart);
}
return BigInt.fromBigInteger(this.toBigInteger().divide(y.toBigInteger()));
}
public BigInt remainder(BigInt y) {
if ((bipart == null) && (y.bipart == null)) {
return BigInt.valueOf(lpart % y.lpart);
}
return BigInt.fromBigInteger(this.toBigInteger().remainder(y.toBigInteger()));
}
public boolean lt(BigInt y) {
if ((bipart == null) && (y.bipart == null)) {
return lpart < y.lpart;
}
return this.toBigInteger().compareTo(y.toBigInteger()) < 0;
}
}