summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2010-11-27 13:41:39 -0500
committerRich Hickey <richhickey@gmail.com>2010-11-27 13:41:39 -0500
commit3e644c768b1f769217ecd484c019580384c7a670 (patch)
tree218cf2601b732aa09e9236b8f8a1026f299f829a
parentcbd789d1a5b472d92b91f2fe0e273f48c2583483 (diff)
Normalize uncheckeds, quotient, remainder. Inline quot and rem. unchecked-foo-long are now just unchecked-foo.
-rw-r--r--src/clj/clojure/core.clj70
-rw-r--r--src/jvm/clojure/lang/Numbers.java104
2 files changed, 100 insertions, 74 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 58cb0a82..6c603b48 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1066,12 +1066,12 @@
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_int_inc x)))
-(defn unchecked-inc-long
+(defn unchecked-inc
"Returns a number one greater than x, a long.
Note - uses a primitive operator subject to overflow."
- {:inline (fn [x] `(. clojure.lang.Numbers (unchecked_long_inc ~x)))
+ {:inline (fn [x] `(. clojure.lang.Numbers (unchecked_inc ~x)))
:added "1.0"}
- [x] (. clojure.lang.Numbers (unchecked_long_inc x)))
+ [x] (. clojure.lang.Numbers (unchecked_inc x)))
(defn unchecked-dec-int
"Returns a number one less than x, an int.
@@ -1080,12 +1080,12 @@
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_int_dec x)))
-(defn unchecked-dec-long
+(defn unchecked-dec
"Returns a number one less than x, a long.
Note - uses a primitive operator subject to overflow."
- {:inline (fn [x] `(. clojure.lang.Numbers (unchecked_long_dec ~x)))
+ {:inline (fn [x] `(. clojure.lang.Numbers (unchecked_dec ~x)))
:added "1.0"}
- [x] (. clojure.lang.Numbers (unchecked_long_dec x)))
+ [x] (. clojure.lang.Numbers (unchecked_dec x)))
(defn unchecked-negate-int
"Returns the negation of x, an int.
@@ -1094,12 +1094,12 @@
:added "1.0"}
[x] (. clojure.lang.Numbers (unchecked_int_negate x)))
-(defn unchecked-negate-long
+(defn unchecked-negate
"Returns the negation of x, a long.
Note - uses a primitive operator subject to overflow."
- {:inline (fn [x] `(. clojure.lang.Numbers (unchecked_long_negate ~x)))
+ {:inline (fn [x] `(. clojure.lang.Numbers (unchecked_minus ~x)))
:added "1.0"}
- [x] (. clojure.lang.Numbers (unchecked_long_negate x)))
+ [x] (. clojure.lang.Numbers (unchecked_minus x)))
(defn unchecked-add-int
"Returns the sum of x and y, both int.
@@ -1108,12 +1108,12 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_add x y)))
-(defn unchecked-add-long
+(defn unchecked-add
"Returns the sum of x and y, both long.
Note - uses a primitive operator subject to overflow."
- {:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_add ~x ~y)))
+ {:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_add ~x ~y)))
:added "1.0"}
- [x y] (. clojure.lang.Numbers (unchecked_long_add x y)))
+ [x y] (. clojure.lang.Numbers (unchecked_add x y)))
(defn unchecked-subtract-int
"Returns the difference of x and y, both int.
@@ -1122,12 +1122,12 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_subtract x y)))
-(defn unchecked-subtract-long
+(defn unchecked-subtract
"Returns the difference of x and y, both long.
Note - uses a primitive operator subject to overflow."
- {:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_subtract ~x ~y)))
+ {:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_minus ~x ~y)))
:added "1.0"}
- [x y] (. clojure.lang.Numbers (unchecked_long_subtract x y)))
+ [x y] (. clojure.lang.Numbers (unchecked_minus x y)))
(defn unchecked-multiply-int
"Returns the product of x and y, both int.
@@ -1136,12 +1136,12 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_multiply x y)))
-(defn unchecked-multiply-long
+(defn unchecked-multiply
"Returns the product of x and y, both long.
Note - uses a primitive operator subject to overflow."
- {:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_multiply ~x ~y)))
+ {:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_multiply ~x ~y)))
:added "1.0"}
- [x y] (. clojure.lang.Numbers (unchecked_long_multiply x y)))
+ [x y] (. clojure.lang.Numbers (unchecked_multiply x y)))
(defn unchecked-divide-int
"Returns the division of x by y, both int.
@@ -1150,13 +1150,6 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_divide x y)))
-(defn unchecked-divide-long
- "Returns the division of x by y, both long.
- Note - uses a primitive operator subject to truncation."
- {:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_divide ~x ~y)))
- :added "1.0"}
- [x y] (. clojure.lang.Numbers (unchecked_long_divide x y)))
-
(defn unchecked-remainder-int
"Returns the remainder of division of x by y, both int.
Note - uses a primitive operator subject to truncation."
@@ -1164,13 +1157,6 @@
:added "1.0"}
[x y] (. clojure.lang.Numbers (unchecked_int_remainder x y)))
-(defn unchecked-remainder-long
- "Returns the remainder of division of x by y, both long.
- Note - uses a primitive operator subject to truncation."
- {:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_long_remainder ~x ~y)))
- :added "1.0"}
- [x y] (. clojure.lang.Numbers (unchecked_long_remainder x y)))
-
(defn pos?
"Returns true if num is greater than zero, else false"
{
@@ -1188,14 +1174,16 @@
(defn quot
"quot[ient] of dividing numerator by denominator."
{:added "1.0"
- :static true}
+ :static true
+ :inline (fn [x y] `(. clojure.lang.Numbers (quotient ~x ~y)))}
[num div]
(. clojure.lang.Numbers (quotient num div)))
(defn rem
"remainder of dividing numerator by denominator."
{:added "1.0"
- :static true}
+ :static true
+ :inline (fn [x y] `(. clojure.lang.Numbers (remainder ~x ~y)))}
[num div]
(. clojure.lang.Numbers (remainder num div)))
@@ -2354,7 +2342,7 @@
(loop [~i 0]
(when (< ~i n#)
~@body
- (recur (unchecked-inc-long ~i)))))))
+ (recur (unchecked-inc ~i)))))))
(defn map
"Returns a lazy sequence consisting of the result of applying f to the
@@ -2726,7 +2714,7 @@
needrec (steppair 0)
subform (steppair 1)
recform-chunk
- `(recur ~seq- ~chunk- ~count- (unchecked-inc-long ~i-))
+ `(recur ~seq- ~chunk- ~count- (unchecked-inc ~i-))
steppair-chunk (step recform-chunk (nnext exprs))
subform-chunk (steppair-chunk 1)]
[true
@@ -2832,7 +2820,7 @@
(loop [~i 0]
(when (< ~i n#)
~@body
- (recur (unchecked-inc-long ~i)))))))
+ (recur (unchecked-inc ~i)))))))
#_(defn into
"Returns a new coll consisting of to-coll with all of the items of
@@ -4073,12 +4061,12 @@
(= k :when) `(if ~v
~(do-cmod etc)
(recur
- (unchecked-inc-long ~gi)))
+ (unchecked-inc ~gi)))
(keyword? k)
(err "Invalid 'for' keyword " k)
:else
`(do (chunk-append ~gb ~body-expr)
- (recur (unchecked-inc-long ~gi)))))]
+ (recur (unchecked-inc ~gi)))))]
`(fn ~giter [~gxs]
(lazy-seq
(loop [~gxs ~gxs]
@@ -4583,7 +4571,7 @@
(if (< ~idx (alength a#))
(do
(aset ~ret ~idx ~expr)
- (recur (unchecked-inc-long ~idx)))
+ (recur (unchecked-inc ~idx)))
~ret))))
(defmacro areduce
@@ -4595,7 +4583,7 @@
`(let [a# ~a]
(loop [~idx 0 ~ret ~init]
(if (< ~idx (alength a#))
- (recur (unchecked-inc-long ~idx) ~expr)
+ (recur (unchecked-inc ~idx) ~expr)
~ret))))
(defn float-array
diff --git a/src/jvm/clojure/lang/Numbers.java b/src/jvm/clojure/lang/Numbers.java
index c68d393a..39962dbb 100644
--- a/src/jvm/clojure/lang/Numbers.java
+++ b/src/jvm/clojure/lang/Numbers.java
@@ -185,21 +185,24 @@ static public Number divide(Object x, Object y){
return ops(x).combine(yops).divide((Number)x, (Number)y);
}
-static public Number quotient(Number x, Number y){
+static public Number quotient(Object x, Object y){
Ops yops = ops(y);
- if(yops.isZero(y))
+ if(yops.isZero((Number) y))
throw new ArithmeticException("Divide by zero");
- return ops(x).combine(yops).quotient(x, y);
+ return ops(x).combine(yops).quotient((Number)x, (Number)y);
}
-static public Number remainder(Number x, Number y){
+static public Number remainder(Object x, Object y){
Ops yops = ops(y);
- if(yops.isZero(y))
+ if(yops.isZero((Number) y))
throw new ArithmeticException("Divide by zero");
- return ops(x).combine(yops).remainder(x, y);
+ return ops(x).combine(yops).remainder((Number)x, (Number)y);
}
-static double quotient(double n, double d){
+static public double quotient(double n, double d){
+ if(d == 0)
+ throw new ArithmeticException("Divide by zero");
+
double q = n / d;
if(q <= Long.MAX_VALUE && q >= Long.MIN_VALUE)
{
@@ -211,7 +214,10 @@ static double quotient(double n, double d){
}
}
-static double remainder(double n, double d){
+static public double remainder(double n, double d){
+ if(d == 0)
+ throw new ArithmeticException("Divide by zero");
+
double q = n / d;
if(q <= Long.MAX_VALUE && q >= Long.MIN_VALUE)
{
@@ -1717,29 +1723,61 @@ static public Number num(long x){
return Long.valueOf(x);
}
-static public long unchecked_long_add(long x, long y){
- return x + y;
-}
-
-static public long unchecked_long_subtract(long x, long y){
- return x - y;
-}
-
-static public long unchecked_long_negate(long x){
- return -x;
-}
-
-static public long unchecked_long_inc(long x){
- return x + 1;
-}
-
-static public long unchecked_long_dec(long x){
- return x - 1;
-}
-
-static public long unchecked_long_multiply(long x, long y){
- return x * y;
-}
+static public long unchecked_add(long x, long y){return x + y;}
+static public long unchecked_minus(long x, long y){return x - y;}
+static public long unchecked_multiply(long x, long y){return x * y;}
+static public long unchecked_minus(long x){return -x;}
+static public long unchecked_inc(long x){return x + 1;}
+static public long unchecked_dec(long x){return x - 1;}
+
+static public Number unchecked_add(Object x, Object y){return add(x,y);}
+static public Number unchecked_minus(Object x, Object y){return minus(x,y);}
+static public Number unchecked_multiply(Object x, Object y){return multiply(x,y);}
+static public Number unchecked_minus(Object x){return minus(x);}
+static public Number unchecked_inc(Object x){return inc(x);}
+static public Number unchecked_dec(Object x){return dec(x);}
+
+static public double unchecked_add(double x, double y){return add(x,y);}
+static public double unchecked_minus(double x, double y){return minus(x,y);}
+static public double unchecked_multiply(double x, double y){return multiply(x,y);}
+static public double unchecked_minus(double x){return minus(x);}
+static public double unchecked_inc(double x){return inc(x);}
+static public double unchecked_dec(double x){return dec(x);}
+
+static public double unchecked_add(double x, Object y){return add(x,y);}
+static public double unchecked_minus(double x, Object y){return minus(x,y);}
+static public double unchecked_multiply(double x, Object y){return multiply(x,y);}
+static public double unchecked_add(Object x, double y){return add(x,y);}
+static public double unchecked_minus(Object x, double y){return minus(x,y);}
+static public double unchecked_multiply(Object x, double y){return multiply(x,y);}
+
+static public double unchecked_add(double x, long y){return add(x,y);}
+static public double unchecked_minus(double x, long y){return minus(x,y);}
+static public double unchecked_multiply(double x, long y){return multiply(x,y);}
+static public double unchecked_add(long x, double y){return add(x,y);}
+static public double unchecked_minus(long x, double y){return minus(x,y);}
+static public double unchecked_multiply(long x, double y){return multiply(x,y);}
+
+static public Number unchecked_add(long x, Object y){return add(x,y);}
+static public Number unchecked_minus(long x, Object y){return minus(x,y);}
+static public Number unchecked_multiply(long x, Object y){return multiply(x,y);}
+static public Number unchecked_add(Object x, long y){return add(x,y);}
+static public Number unchecked_minus(Object x, long y){return minus(x,y);}
+static public Number unchecked_multiply(Object x, long y){return multiply(x,y);}
+
+static public Number quotient(double x, Object y){return quotient((Object)x,y);}
+static public Number quotient(Object x, double y){return quotient(x,(Object)y);}
+static public Number quotient(long x, Object y){return quotient((Object)x,y);}
+static public Number quotient(Object x, long y){return quotient(x,(Object)y);}
+static public double quotient(double x, long y){return quotient(x,(double)y);}
+static public double quotient(long x, double y){return quotient((double)x,y);}
+
+static public Number remainder(double x, Object y){return remainder((Object)x,y);}
+static public Number remainder(Object x, double y){return remainder(x,(Object)y);}
+static public Number remainder(long x, Object y){return remainder((Object)x,y);}
+static public Number remainder(Object x, long y){return remainder(x,(Object)y);}
+static public double remainder(double x, long y){return remainder(x,(double)y);}
+static public double remainder(long x, double y){return remainder((double)x,y);}
static public long add(long x, long y){
long ret = x + y;
@@ -1820,11 +1858,11 @@ static public Number multiplyP(long x, long y){
return num(ret);
}
-static public long unchecked_long_divide(long x, long y){
+static public long quotient(long x, long y){
return x / y;
}
-static public long unchecked_long_remainder(long x, long y){
+static public long remainder(long x, long y){
return x % y;
}