Skip to content

Commit

Permalink
v1.0.14 - fixed simplification of negative fraction bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dthwaite committed Apr 10, 2016
1 parent eb1856b commit 138fbfd
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
## TPA - Total Precision Arithmetic change log

### v1.0.14 (2016-04-10)
* Fixed simplification of negative fraction bug

### v1.0.13 (2016-04-08)
* Upgraded documentation

Expand Down
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -10,7 +10,7 @@

##### tpa.js performs basic arithmetic operations with total precision.

Available on [GitHub](https://github.com/dthwaite/TPA), details on [JSDocs](http://dthwaite.github.io/docs/TPA/1.0.13). See it working: [Demonstration](http://dthwaite.github.io/tpa/)
Available on [GitHub](https://github.com/dthwaite/TPA), details on [JSDocs](http://dthwaite.github.io/docs/TPA/1.0.14). See it working: [Demonstration](http://dthwaite.github.io/tpa/)

The main features are:

Expand Down Expand Up @@ -41,8 +41,8 @@ console.log(n.toString()); // Outputs '3.[3]'
##### Browser:
To install it:

* Download `lib/tpa.min.js` from my latest version on GitHub or use their CDN: 'https://cdn.rawgit.com/dthwaite/TPA/v1.0.13/lib/tpa.min.js'
* `tpa.min.js` is a UMD bundle with an export name of `Tpa`
* Download [tpa.min.js](https://github.com/dthwaite/TPA/tree/master/lib/tpa.min.js) from GitHub or use their CDN for my latest version: [v1.0.14](https://cdn.rawgit.com/dthwaite/TPA/v1.0.14/lib/tpa.min.js)
* `tpa.min.js` is a UMD (Universal Module Definition) bundle with an export name of `Tpa`

To see how to use it:
<http://dthwaite.github.io/tpa/>
Expand Down Expand Up @@ -70,7 +70,7 @@ Build minified version for browser into lib/tpa.min.js:
`npm run build`

### A note about performance
How fast is this library compared to others? Good question. And tricky to answer. It all depends on the operation, the size of the numbers, whether they are fractional or not (many libraries just do integers), whether you call static or instance methods and your run time environment. I spent some time comparing and contrasting and there's no straight answer. Most of the time this library performs quite well in comparison. Sometimes wildy better than most, sometimes not so good and it's difficult to summarise. If performance is really important then you must to do your own analysis specific to your environment and needs to then choose the fastest in your circumstance. If it's not that important then you could do a lot worse than choosing this one. I've focussed on delivering a healthy mix of the features listed earlier. It's not slow, by any means.
How fast is this library compared to others? Good question. And tricky to answer. It all depends on the operation, the size of the numbers, whether they are fractional or not (many libraries just do integers), whether you call static or instance methods and your run time environment. I spent some time comparing and contrasting and there's no straight answer. Most of the time this library performs quite well in comparison. Sometimes wildy better than most, sometimes not so good and it's difficult to summarise. If performance is really important then you must do your own analysis specific to your environment and needs to then choose the fastest in your circumstance. If it's not that important then you could do a lot worse than choosing this one. I've focussed on delivering a healthy mix of the features listed earlier. It's not slow, by any means.
### Usage

#### Set up
Expand Down Expand Up @@ -203,7 +203,7 @@ console.log(Tpa(-3).sign()); // -1
console.log(Tpa(3.3).hasFraction()); // true
console.log(Tpa('-3 1/3').frac().toFraction()); // '-0 1/3'
console.log(Tpa('-3 1/3').int().toFraction()); // '-3'
console.log(Tpa(22).modulus(3).toString()); // '1'
console.log(Tpa(22).modulus(3).toString()); // '1'
console.log(Tpa(-33.5).abs().value()); // 33.5
```
#### Static methods
Expand All @@ -224,7 +224,7 @@ console.log(Tpa.add(a,b).value()); // 17
console.log(Tpa.subtract(a,b).value()); // -7
console.log(Tpa.multiply(a,b).value()); // 60
console.log(Tpa.divide(b,a).toFraction());// '2 25/50'
console.log(Tpa.modulus(a,b).value()); // 5
console.log(Tpa.modulus(a,b).value()); // 5
console.log(Tpa.frac(b).value()); // 0.5
console.log(Tpa.int(b).value()); // 12
console.log(Tpa.abs(-23).value()); // 23
Expand Down
4 changes: 2 additions & 2 deletions lib/N.js
Expand Up @@ -507,11 +507,11 @@ module.exports=(/** @lends module:N*/function() {
* @return {String} The full decimal representation of this number
*/
N.prototype.toString=function() {
var result=this.isNegative() ? '-' : '';

var result='';
var test=new N(this).abs().normalise().positivise();
while (!test.isZero()) result=test.divide(N.TEN).lsb()+result;
if (result.length==0) result='0';
if (this.isNegative()) result='-'+result;
return result;
};

Expand Down
16 changes: 8 additions & 8 deletions lib/tpa.js
Expand Up @@ -251,7 +251,9 @@ module.exports = (/** @lends module:TPA*/function(globalObj) { //eslint-disable-
throw new Error('Simplify() takes an optional numeric argument specifying the maximum number of millisecondsto process');
if (typeof milliseconds=='undefined') milliseconds=100;
if (this.isInteger() || this.remainder.numerator.isZero()) return true;
var limit= N.abs(this.remainder.numerator)._roughSqrt().value();
var isNegative=this.remainder.numerator.isNegative();
this.remainder.numerator.abs();
var limit= this.remainder.numerator._roughSqrt().value();
var primes=new N.Primes();
var start=new Date().getTime();
var factor=new N().set(1);
Expand All @@ -276,9 +278,9 @@ module.exports = (/** @lends module:TPA*/function(globalObj) { //eslint-disable-
if (remainder.isZero()) {
this.remainder.denominator=denominator;
this.remainder.numerator=factor;
return true;
prime=1;
} else this.remainder.numerator.multiply(factor);

if (isNegative) this.remainder.numerator.negate();
// If prime is zero then we never got to finish
return prime>0;
};
Expand Down Expand Up @@ -340,7 +342,7 @@ module.exports = (/** @lends module:TPA*/function(globalObj) { //eslint-disable-
};

/**
* @returns {boolean} `true` if this number is equal than zero
* @returns {boolean} `true` if this number is equal to zero
*/
Tpa.prototype.isZero=function() {
this._normaliseRemainder();
Expand Down Expand Up @@ -689,8 +691,7 @@ module.exports = (/** @lends module:TPA*/function(globalObj) { //eslint-disable-
this.number.subtract(number.number);
if (!this.integer) {
if (!number.integer && !number.remainder.numerator.isZero()) {
this.remainder.numerator.multiply(number.remainder.denominator);
this.remainder.numerator.subtract(N.temporary(number.remainder.numerator).multiply(this.remainder.denominator));
this.remainder.numerator.multiply(number.remainder.denominator).subtract(N.temporary(number.remainder.numerator).multiply(this.remainder.denominator));
this.remainder.denominator.multiply(number.remainder.denominator);
}
this._normaliseRemainder();
Expand All @@ -714,8 +715,7 @@ module.exports = (/** @lends module:TPA*/function(globalObj) { //eslint-disable-
this.number.add(number.number);
if (!this.integer) {
if (!number.integer && !number.remainder.numerator.isZero()) {
this.remainder.numerator.multiply(number.remainder.denominator);
this.remainder.numerator.add(N.temporary(number.remainder.numerator).multiply(this.remainder.denominator));
this.remainder.numerator.multiply(number.remainder.denominator).add(N.temporary(number.remainder.numerator).multiply(this.remainder.denominator));
this.remainder.denominator.multiply(number.remainder.denominator);
}
this._normaliseRemainder();
Expand Down

0 comments on commit 138fbfd

Please sign in to comment.