Permalink
Browse files

Integer operators now return the proper type

  • Loading branch information...
chochos committed Apr 19, 2012
1 parent 39710e0 commit 04ec8ce68c82c901554286e6b2c62385db66c7f4
Showing with 33 additions and 8 deletions.
  1. +33 −8 src/main/js/numbers.js
View
@@ -14,22 +14,47 @@ function Integer(value) {
initTypeProto(Integer, 'ceylon.language.Integer', Object$, Castable, Integral, Numeric);
var Integer$proto = Integer.$$.prototype;
Integer$proto.getString = function() { return String$(this.value.toString()) }
-Integer$proto.plus = function(other) { return Integer(this.value+other.value) }
-Integer$proto.minus = function(other) { return Integer(this.value-other.value) }
-Integer$proto.times = function(other) { return Integer(this.value*other.value) }
+Integer$proto.plus = function(other) {
+ if (isOfType(other, 'ceylon.language.Integer') === $true) {
+ return Integer(this.value+other.value);
+ }
+ return Float(this.value+other.value);
+}
+Integer$proto.minus = function(other) {
+ if (isOfType(other, 'ceylon.language.Integer') === $true) {
+ return Integer(this.value-other.value);
+ }
+ return Float(this.value-other.value);
+}
+Integer$proto.times = function(other) {
+ if (isOfType(other, 'ceylon.language.Integer') === $true) {
+ return Integer(this.value*other.value);
+ }
+ return Float(this.value*other.value);
+}
Integer$proto.divided = function(other) {
var exact = this.value/other.value;
- return Integer((exact<0) ? Math.ceil(exact) : Math.floor(exact));
+ if (isOfType(other, 'ceylon.language.Integer') === $true) {
+ return Integer((exact<0) ? Math.ceil(exact) : Math.floor(exact));
+ }
+ return Float((exact<0) ? Math.ceil(exact) : Math.floor(exact));
}
Integer$proto.remainder = function(other) { return Integer(this.value%other.value) }
Integer$proto.power = function(exp) {
- if (exp.getSign().value < 0) {
- if (!(this.value===1 || this.value===-1)) {
- throw Exception(String$("Negative exponent"));
+ var isint = isOfType(exp, 'ceylon.language.Integer') === $true;
+ if (isint) {
+ if (exp.getSign().value < 0) {
+ if (!(this.value===1 || this.value===-1)) {
+ throw Exception(String$("Negative exponent"));
+ }
}
}
var exact = Math.pow(this.value, exp.value);
- return Integer((exact<0) ? Math.ceil(exact) : Math.floor(exact));
+ if (isint) {
+ return Integer((exact<0) ? Math.ceil(exact) : Math.floor(exact));
+ } else {
+ return Float(exact);
+ }
}
Integer$proto.getNegativeValue = function() { return Integer(-this.value) }
Integer$proto.getPositiveValue = function() { return this }

0 comments on commit 04ec8ce

Please sign in to comment.