XNS extends the JavaScript Number() and String() constructor functions by giving their prototype properties specific methods and properties. The intention is to allow usage of the same method names across XNS, starting with Number().
Number.prototype.xnsType = "Number"
Number.prototype.xnsVersion = 0.5
Number.prototype.isReal = returnFalseIfNaN
This is a reference to the returnFalseIfNaN()
function in
the utility script.
Number.prototype.isDecimal = returnFalseIfNaN
This is a reference to the returnFalseIfNaN()
function in
the utility script.
Number.prototype.isRational = returnFalseIfNaN
This is a reference to the returnFalseIfNaN()
function in
the utility script.
Number.prototype.isInteger = function() { if (this * 1 == Math.round(this * 1)) { return true } return false }
Number.prototype.isNegative = function() { if (this * 1 < 0) { return true } return false }
Number.prototype.compareTo = function(that) { if (typeof that.xnsType == "undefined") { return Number.NaN } /* Only XNS numbers have the xnsType property. Thus, anything not having that property is not an XNS number. */ switch (that.xnsType) { case "Number": var response = Number.NaN, leftSide = this * 1, rightSide = that * 1 if (leftSide < rightSide) { response = -1 } if (leftSide == rightSide) { response = 0 } if (leftSide > rightSide) { response = 1 } return response; break; default: var a = new Number["_to" + that.xnsType](this) /* Number is the most primitive XNS number type. Thus, we want to convert this into the higher type which that possesses. We name our conversion functions typeName._fromAltTypeName. For instance, to convert from a Number to a BigDecimal, we would have Number._toBigDecimal(that). */ return a.compareTo(that) break; } return Number.NaN }
Number.prototype.negate = function() { return this * -1 }
Number.prototype.add = function(that) { if (typeof that.xnsType == "undefined") { return Number.NaN } /* Only XNS numbers have the xnsType property. Thus, anything not having that property is not an XNS number. */ switch (that.xnsType) { case "Number": return (this * 1) + (that * 1) // in case this or that is a string break; default: var a = new Number["_to" + that.xnsType](this) /* Number is the most primitive XNS number type. Thus, we want to convert this into the higher type which that possesses. We name our conversion functions typeName._fromAltTypeName. For instance, to convert from a Number to a BigDecimal, we would have Number._toBigDecimal(that). */ return a.add(that) break; } return Number.NaN }
Number.prototype.subtract = function(that) { if (typeof that.xnsType == "undefined") { return Number.NaN } /* Only XNS numbers have the xnsType property. Thus, anything not having that property is not an XNS number. */ switch (that.xnsType) { case "Number": return (this - that) break; default: var a = new Number["_to" + that.xnsType](this) /* Number is the most primitive XNS number type. Thus, we want to convert this into the higher type which that possesses. We name our conversion functions typeName._fromAltTypeName. For instance, to convert from a Number to a BigDecimal, we would have Number._toBigDecimal(that). */ return a.subtract(that) break; } return Number.NaN }
Number.prototype.multiply = function(that) { if (typeof that.xnsType == "undefined") { return Number.NaN } /* Only XNS numbers have the xnsType property. Thus, anything not having that property is not an XNS number. */ switch (that.xnsType) { case "Number": return (this * that) break; default: var a = new Number["_to" + that.xnsType](this) /* Number is the most primitive XNS number type. Thus, we want to convert this into the higher type which that possesses. We name our conversion functions typeName._fromAltTypeName. For instance, to convert from a Number to a BigDecimal, we would have Number._toBigDecimal(that). */ return a.multiply(that) break; } return Number.NaN }
Number.prototype.divide = function(that) { if (typeof that.xnsType == "undefined") { return Number.NaN } /* Only XNS numbers have the xnsType property. Thus, anything not having that property is not an XNS number. */ switch (that.xnsType) { case "Number": return (this / that) break; default: var a = new Number["_to" + that.xnsType](this) /* Number is the most primitive XNS number type. Thus, we want to convert this into the higher type which that possesses. We name our conversion functions typeName._fromAltTypeName. For instance, to convert from a Number to a BigDecimal, we would have Number._toBigDecimal(that). */ return a.divide(that) break; } return Number.NaN }
Number.prototype.toMathML = function() { var response = document.createElementNS(xmlns_math, "cn") response.setAttribute("type", "real") response.appendChild(document.createTextNode(this.toString())) return response }
String.prototype.toLiteralString = function() { var response = this.replace(/\\/g, "\\\\") response = response.replace(/\"/g, '\\"') response = response.replace(/\'/g, "\\'") return "'" + response + "'" }
(as defined in the utility script.)