Skip to content

Commit

Permalink
fix(fmod): fmod computes the mod operation with raw arithmetic
Browse files Browse the repository at this point in the history
fixes #15
  • Loading branch information
mauriciopoppe committed Feb 16, 2018
1 parent fe7a842 commit 0133e47
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ Interval.prototype.setWhole = function () {
* @return {Interval} The calling interval
*/
Interval.prototype.open = function (lo, hi) {
console.log(lo, hi)
return this.assign(round.safeNext(lo), round.safePrev(hi))
}

Expand Down
7 changes: 6 additions & 1 deletion lib/operations/algebra.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ var algebra = {}
* Interval(5.3, 5.3),
* Interval(2, 2)
* ) // Interval(1.3, 1.3)
*
* @example
* Interval.fmod(
* Interval(5, 7),
* Interval(2, 3)
* ) // Interval(2, 5)
* // explanation: [5, 7] - [2, 3] * 1 = [2, 5]
*
* @param {Interval} x
* @param {Interval} y
* @return {Interval}
Expand All @@ -37,7 +40,9 @@ algebra.fmod = function (x, y) {
return constants.EMPTY
}
var yb = x.lo < 0 ? y.lo : y.hi
var n = rmath.intLo(rmath.divLo(x.lo, yb))
var n = x.lo / yb
if (n < 0) n = Math.ceil(n)
else n = Math.floor(n)
// x mod y = x - n * y
return arithmetic.sub(x, arithmetic.mul(y, Interval(n)))
}
Expand Down
9 changes: 9 additions & 0 deletions test/operations/algebra.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ describe('algebra', function () {
assert(Interval.isEmpty(n))
})

it('should compute the fmod (issue #15)', function () {
// issue #15
n = algebra.fmod(
new Interval(2, 2),
new Interval(2, 2)
)
Interval.almostEqual(n, [0, 0])
})

it('should compute the multiplicative inverse', function () {
n = algebra.multiplicativeInverse(new Interval(1, 1))
Interval.almostEqual(n, [1, 1])
Expand Down
5 changes: 5 additions & 0 deletions test/operations/arithmetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe('arithmetic', function () {
)
assertEps(a.lo, -2)
assertEps(a.hi, 2)
a = arithmetic.sub(
new Interval(5, 7),
new Interval(2, 3)
)
Interval.almostEqual(a, [2, 5])
a = arithmetic.sub(
new Interval(-1, Infinity),
new Interval(0, 1)
Expand Down

0 comments on commit 0133e47

Please sign in to comment.