Skip to content

Commit 12fb177

Browse files
author
Samori Gorse
committed
- Added division tests
- Started to implement a basic version of division handling. It would basically set both operands to the same exponent and thus divide an integer by another integer. I had to hack the _neg_exp function in order to provide a correct output. This needs some cleanup.
1 parent a611ebe commit 12fb177

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

decimaljs.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,39 +63,54 @@ Decimal.prototype.sub = function(target) {
6363

6464
Decimal.prototype.mul = function(target) {
6565
target = Decimal(target);
66+
67+
var calc = String(this.repr.value * target.repr.value)
68+
69+
return Decimal._format(calc, this.repr.exp + target.repr.exp);
70+
}
71+
72+
Decimal.prototype.div = function(target) {
73+
target = Decimal(target);
6674

6775
var ops = [this, target];
68-
var fst = ops[0].repr.value;
69-
var snd = ops[1].repr.value;
70-
var calc = String(fst * snd)
76+
ops.sort(function(x, y) { return x.repr.exp - y.repr.exp });
77+
78+
var smallest = ops[0].repr.exp;
79+
var biggest = ops[1].repr.exp;
7180

72-
return Decimal._format(calc, ops[0].repr.exp + ops[1].repr.exp);
81+
var fst = Decimal._format(ops[1].repr.value, biggest - smallest) * 1;
82+
var snd = ops[0].repr.value * 1;
83+
84+
var calc = String(fst / snd);
85+
86+
return Decimal._format(calc, smallest);
7387
}
7488

75-
7689
Decimal.prototype.toString = function() {
7790
return this.internal;
7891
}
7992

80-
8193
Decimal._neg_exp = function(str, position) {
8294
position = Math.abs(position);
95+
8396
var offset = position - str.length;
84-
var sep = '.'
97+
var sep = Decimal.SEPARATOR;
8598

8699
if(offset >= 0) {
87100
str = Decimal.__zero(offset) + str;
88101
sep = '0.';
89102
}
90103

91104
var length = str.length;
105+
var head = str.substr(0, length - position);
106+
var tail = str.substring(length - position, length).replace(/\./g, '');
92107

93-
return str.substr(0, length - position) + sep + str.substring(length - position, length);
108+
return head + sep + tail;
94109
}
95110

96111
Decimal._pos_exp = function(str, exp) {
97112
var zeros = Decimal.__zero(exp);
98-
return str + zeros;
113+
return String(str + zeros);
99114
}
100115

101116
Decimal._format = function(num, exp) {
@@ -107,9 +122,11 @@ Decimal.__zero = function(exp) {
107122
return new Array(exp + 1).join('0');
108123
};
109124

125+
Decimal.SEPARATOR = '.';
126+
110127
(function() {
111128
//Generics
112-
var methods = ['add','mul', 'sub'];
129+
var methods = ['add','mul', 'sub', 'div'];
113130

114131
for(var i=0; i < methods.length; i++) {
115132
(function(method) {

tests/division.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
assert.equals(Decimal('10.1').div('0.25'), '40.4');
2+
assert.equals(Decimal('50').div('5'), '10');

0 commit comments

Comments
 (0)