Skip to content

Commit

Permalink
Cleaned up tests and fixed issue #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
whatgoodisaroad committed Feb 12, 2013
1 parent c3d42c6 commit ec972b0
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 49 deletions.
27 changes: 21 additions & 6 deletions build/Big.js
@@ -1,11 +1,11 @@
///////////////////////////////////////////////////////////////
// Big.js
// v0.9.0 (beta)
// v0.10.0 (beta)
///////////////////////////////////////////////////////////////
// http://github.com/whatgoodisaroad/Big-js
// By Wyatt Allen
// MIT Licensed
// 2011-09-19 (Monday, 19 September 2011)
// 2013-02-11 (Monday, 11 February 2013)
///////////////////////////////////////////////////////////////
var Big;

Expand Down Expand Up @@ -164,8 +164,22 @@ function compare(bl, br) {
bl = bl.clone();
br = br.clone();

if (bl.sign != br.sign) {
if (bl.sign == POSITIVE) { return GT; }
if (bl.isZero() || br.isZero()) {
if (bl.isZero() && br.isZero()) { return EQ; }

else if (bl.isZero()) {
if (br.sign == POSITIVE) { return LT; }
else /* (br.sign == POSITIVE) */ { return GT; }
}

else /* (br.isZero()) */ {
if (bl.sign == POSITIVE) { return GT; }
else /* (bl.sign == POSITIVE) */ { return LT; }
}
}

else if (bl.sign != br.sign) {
if (bl.sign == POSITIVE) { return GT; }
else /* (bl.sign == NEGATIVE) */ { return LT; }
}

Expand Down Expand Up @@ -535,7 +549,7 @@ function subtract(l, r) {
r = normalize(r);

if (l.isZero()) {
return negate(l);
return negate(r);
}

if (r.isZero()) {
Expand Down Expand Up @@ -610,7 +624,8 @@ function borrowFromMantissa(m) {



//// Basic, O(n^2) multiplication algorithm:
//
// Basic, O(n^2) multiplication algorithm:
function multiply(l, r) {
var
same = preProcess(l, r),
Expand Down
28 changes: 14 additions & 14 deletions build/Big.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/calc/css/app.css
Expand Up @@ -51,7 +51,7 @@ body {

pre#history, #input {
font-family:monospace;
font-size:12pt;
font-size:10pt;
padding:4px 0;
}

Expand Down
18 changes: 16 additions & 2 deletions lib/big.core.js
Expand Up @@ -35,8 +35,22 @@ function compare(bl, br) {
bl = bl.clone();
br = br.clone();

if (bl.sign != br.sign) {
if (bl.sign == POSITIVE) { return GT; }
if (bl.isZero() || br.isZero()) {
if (bl.isZero() && br.isZero()) { return EQ; }

else if (bl.isZero()) {
if (br.sign == POSITIVE) { return LT; }
else /* (br.sign == POSITIVE) */ { return GT; }
}

else /* (br.isZero()) */ {
if (bl.sign == POSITIVE) { return GT; }
else /* (bl.sign == POSITIVE) */ { return LT; }
}
}

else if (bl.sign != br.sign) {
if (bl.sign == POSITIVE) { return GT; }
else /* (bl.sign == NEGATIVE) */ { return LT; }
}

Expand Down
14 changes: 12 additions & 2 deletions plugins/Big.expression.js
Expand Up @@ -128,7 +128,7 @@
// then push the token to the operator stack.
if (!opStack.length ||
topOperator == "(" ||
currentPrecidence > Big.expression.getPrecidence(topOperator])) {
currentPrecidence > Big.expression.getPrecidence(topOperator)) {
opStack.push(token);
}

Expand All @@ -152,24 +152,34 @@
}

// If a close paren has been found,
// then pop
// then pop the entire operator stack into the result stack,
// while the top of the stack is not an open paren marker.
else if (token == ")") {
while (opStack[opStack.length - 1] != "(")
{
result.push(opStack.pop());

if (!opStack.length) {
throw "Big.expression.fromInfix: No matching open paren.";
}
}

opStack.pop();
}

// The token didn't match any pattern.
else {
throw "Big.expression.fromInfix: unrecognized token \"" + token + "\"";
}
}

// After all of the tokens have been processed,
// pop the operator stack to the result stack.
while (opStack.length) {
result.push(opStack.pop());
}

//
return result.join(" ");
};

Expand Down
34 changes: 31 additions & 3 deletions tests/qunit/big.add.tests.js
@@ -1,49 +1,77 @@
module("add");

test("add(l, r)", function() {
test("add(3, 4) = 7", function() {
var l, r, expected, result;

l = new Big("3");
r = new Big("4");
expected = new Big("7");
result = normalize(add(l, r));
deepEqual(result, expected, "add correctly adds two small positive integers");

});

test("add(3.4, 4.5) = 7.9", function() {
var l, r, expected, result;

l = new Big("3.4");
r = new Big("4.5");
expected = new Big("7.9");
result = normalize(add(l, r));
deepEqual(result, expected, "add correctly adds two small positive reals");

});

test("add(4, -3) = 1", function() {
var l, r, expected, result;

l = new Big("4");
r = new Big("-3");
expected = new Big("1");
result = normalize(add(l, r));
deepEqual(result, expected, "add correctly adds small negative integer to small positive integer");
});

test("add(-4, -5) = -9", function() {
var l, r, expected, result;

l = new Big("-4");
r = new Big("-5");
expected = new Big("-9");
result = normalize(add(l, r));
deepEqual(result, expected, "add correctly adds two negative integers");
});

test("add(4, -36) = 32", function() {
var l, r, expected, result;

l = new Big("4");
r = new Big("-36");
expected = new Big("-32");
result = normalize(add(l, r));
deepEqual(result, expected, "add correctly adds negative integer to positive integer");
});

test("add(3.456789, 789.1) = 792.556789", function() {
var l, r, expected, result;

l = new Big("3.456789");
r = new Big("789.1");
expected = new Big("792.556789");
result = normalize(add(l, r));
deepEqual(result, expected, "add correctly adds positive reals with different whole and fractional lengths");
});

test("add(8716138.12, 55257081.72085) = 63973219.84085", function() {
var l, r, expected, result;

l = new Big("8716138.12");
r = new Big("55257081.72085");
expected = new Big("63973219.84085");
result = normalize(add(l, r));
deepEqual(result, expected, "add correctly adds large positive reals");
});

test("add(-1.1, 1.8) = 0.7", function() {
var l, r, expected, result;

l = new Big("-1.1");
r = new Big("1.8");
Expand Down

0 comments on commit ec972b0

Please sign in to comment.