mirror of
https://github.com/Xahau/xahaud.git
synced 2025-11-20 10:35:50 +00:00
Add support for mixed operands to divide().
This commit is contained in:
@@ -610,9 +610,6 @@ Amount.prototype.compareTo = function (v) {
|
|||||||
Amount.prototype.copyTo = function (d, negate) {
|
Amount.prototype.copyTo = function (d, negate) {
|
||||||
if ('object' === typeof this._value)
|
if ('object' === typeof this._value)
|
||||||
{
|
{
|
||||||
if (this._is_native && negate)
|
|
||||||
this._value.negate().copyTo(d._value);
|
|
||||||
else
|
|
||||||
this._value.copyTo(d._value);
|
this._value.copyTo(d._value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -622,9 +619,7 @@ Amount.prototype.copyTo = function (d, negate) {
|
|||||||
|
|
||||||
d._offset = this._offset;
|
d._offset = this._offset;
|
||||||
d._is_native = this._is_native;
|
d._is_native = this._is_native;
|
||||||
d._is_negative = this._is_native
|
d._is_negative = negate
|
||||||
? undefined // Native sign in BigInteger.
|
|
||||||
: negate
|
|
||||||
? !this._is_negative // Negating.
|
? !this._is_negative // Negating.
|
||||||
: this._is_negative; // Just copying.
|
: this._is_negative; // Just copying.
|
||||||
|
|
||||||
@@ -657,62 +652,57 @@ Amount.prototype.equals = function (d) {
|
|||||||
|
|
||||||
// Result in terms of this' currency and issuer.
|
// Result in terms of this' currency and issuer.
|
||||||
Amount.prototype.divide = function (d) {
|
Amount.prototype.divide = function (d) {
|
||||||
var result;
|
var result, vn, on, vd, od;
|
||||||
|
|
||||||
if (!this.is_comparable(d)) {
|
if (d.is_zero()) {
|
||||||
// XXX We might want to allow mixed division. In that case convert d to same terms as this.
|
|
||||||
result = Amount.NaN();
|
|
||||||
}
|
|
||||||
else if (d.is_zero()) {
|
|
||||||
throw "divide by zero";
|
throw "divide by zero";
|
||||||
}
|
}
|
||||||
else if (this.is_zero()) {
|
else if (this.is_zero()) {
|
||||||
result = this;
|
result = this.clone();
|
||||||
}
|
}
|
||||||
else if (this._is_native) {
|
else if (!this.is_valid()) {
|
||||||
var vn = this._value;
|
throw new Error("Invalid dividend");
|
||||||
var on = this._offset;
|
}
|
||||||
var vd = d._value;
|
else if (!d.is_valid()) {
|
||||||
var od = d._offset;
|
throw new Error("Invalid divisor");
|
||||||
|
|
||||||
while (vn.compareTo(consts.bi_man_min_value) < 0) {
|
|
||||||
vn = vn.multiply(consts.bi_10);
|
|
||||||
on -= 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (dn.compareTo(consts.bi_man_min_value) < 0) {
|
if (this._is_native) {
|
||||||
vd = vn.multiply(consts.bi_10);
|
vn = this._value.multiply(consts.bi_1e16);
|
||||||
od -= 1;
|
on = 0 - 16;
|
||||||
|
} else {
|
||||||
|
vn = this._value.multiply(consts.bi_1e16);
|
||||||
|
on = this._offset - 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d._is_native) {
|
||||||
|
vd = d._value;
|
||||||
|
od = 0;
|
||||||
|
} else {
|
||||||
|
vd = d._value;
|
||||||
|
od = d._offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = new Amount();
|
result = new Amount();
|
||||||
result._value = vn.multiply(consts.bi_1e16).divide(vd);
|
result._offset = on-od;
|
||||||
|
result._value = vn.divide(vd).abs();
|
||||||
if (this._is_negative != d._is_negative)
|
result._is_native = this._is_native;
|
||||||
result._value = result._value.negate();
|
result._is_negative = !!this._is_negative !== !!d._is_negative;
|
||||||
|
|
||||||
// XXX Check range?
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var vn = this._value.multiply(consts.bi_10).add(3);
|
|
||||||
var on = this._offset - 1;
|
|
||||||
var vd = d._value.multiply(consts.bi_10).add(3);
|
|
||||||
var od = d._offset - 1;
|
|
||||||
|
|
||||||
result = new Amount();
|
|
||||||
result._offset = on+od-16;
|
|
||||||
result._value = vn.multiply(consts.bi_1e16).divide(vd);
|
|
||||||
result._is_negative = this._is_negative != d._is_negative;
|
|
||||||
result._currency = this._currency.clone();
|
result._currency = this._currency.clone();
|
||||||
result._issuer = this._issuer.clone();
|
result._issuer = this._issuer.clone();
|
||||||
|
|
||||||
if (result._offset > cMaxOffset || result._offset < cMaxOffset)
|
if (result._is_native) {
|
||||||
throw "division result out of range.";
|
// For native results, we need to manually normalize
|
||||||
|
result._value = result._value.divide(consts.bi_10.pow(result._offset+4));
|
||||||
|
result._offset = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
//if (result._offset > consts.cMaxOffset || result._offset < consts.cMaxOffset)
|
||||||
|
// throw new Error("division result out of range.");
|
||||||
|
|
||||||
// XXX Check value is in legal range here or have canonicalize do it?
|
// XXX Check value is in legal range here or have canonicalize do it?
|
||||||
|
|
||||||
result.canonicalize();
|
result.canonicalize();
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
@@ -919,12 +909,9 @@ Amount.prototype.parse_native = function (j) {
|
|||||||
this._value = int_part.add(fraction_part);
|
this._value = int_part.add(fraction_part);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m[1])
|
|
||||||
this._value = this._value.negate();
|
|
||||||
|
|
||||||
this._is_native = true;
|
this._is_native = true;
|
||||||
this._offset = undefined;
|
this._offset = undefined;
|
||||||
this._is_negative = undefined;
|
this._is_negative = !!m[1] && this._value.compareTo(BigInteger.ZERO) !== 0;
|
||||||
|
|
||||||
if (this._value.compareTo(consts.bi_xns_max) > 0 || this._value.compareTo(consts.bi_xns_min) < 0)
|
if (this._value.compareTo(consts.bi_xns_max) > 0 || this._value.compareTo(consts.bi_xns_min) < 0)
|
||||||
{
|
{
|
||||||
@@ -1049,7 +1036,7 @@ Amount.prototype.to_text = function (allow_nan) {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return this._value.toString();
|
return (this._is_negative ? "-" : "") + this._value.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (this.is_zero())
|
else if (this.is_zero())
|
||||||
|
|||||||
@@ -104,6 +104,48 @@ buster.testCase("Amount", {
|
|||||||
"Clone non-native -123" : function () {
|
"Clone non-native -123" : function () {
|
||||||
buster.assert.equals("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").clone().to_text_full());
|
buster.assert.equals("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").clone().to_text_full());
|
||||||
},
|
},
|
||||||
|
"Divide XRP by USD" : function () {
|
||||||
|
buster.assert.equals("20/XRP", Amount.from_json("200").divide(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide XRP by USD" : function () {
|
||||||
|
buster.assert.equals("2000/XRP", Amount.from_json("20000").divide(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide XRP by USD" : function () {
|
||||||
|
buster.assert.equals("200000/XRP", Amount.from_json("2000000").divide(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide XRP by USD, neg" : function () {
|
||||||
|
buster.assert.equals("-20/XRP", Amount.from_json("200").divide(Amount.from_json("-10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide XRP by USD, neg, frac" : function () {
|
||||||
|
buster.assert.equals("-162/XRP", Amount.from_json("-6000").divide(Amount.from_json("37/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide USD by USD" : function () {
|
||||||
|
buster.assert.equals("200/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide USD by USD, fractional" : function () {
|
||||||
|
buster.assert.equals("57142.85714285714/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("35/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide USD by USD" : function () {
|
||||||
|
buster.assert.equals("20/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("100000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide EUR by USD, result < 1" : function () {
|
||||||
|
buster.assert.equals("0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide EUR by USD, neg" : function () {
|
||||||
|
buster.assert.equals("-12/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-24000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide EUR by USD, neg, <1" : function () {
|
||||||
|
buster.assert.equals("-0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("-1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide EUR by XRP, result < 1" : function () {
|
||||||
|
buster.assert.equals("0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("2000")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide EUR by XRP, neg" : function () {
|
||||||
|
buster.assert.equals("-20/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("5")).to_text_full());
|
||||||
|
},
|
||||||
|
"Divide EUR by XRP, neg, <1" : function () {
|
||||||
|
buster.assert.equals("-0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("2000")).to_text_full());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user