Merge branch 'develop' into develop

This commit is contained in:
Jos de Jong 2019-03-10 16:33:12 +01:00 committed by GitHub
commit f9201affba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 126 additions and 15 deletions

View File

@ -1,8 +1,12 @@
'use strict'
const deepMap = require('../../utils/collection/deepMap')
const nearlyEqual = require('../../utils/number').nearlyEqual
const bigNearlyEqual = require('../../utils/bignumber/nearlyEqual')
function factory (type, config, load, typed) {
const round = load(require('../../function/arithmetic/round'))
/**
* Round a value towards plus infinity
* If `x` is complex, both real and imaginary part are rounded towards plus infinity.
@ -32,14 +36,24 @@ function factory (type, config, load, typed) {
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
*/
const ceil = typed('ceil', {
'number': Math.ceil,
'number': function (x) {
if (nearlyEqual(x, round(x), config.epsilon)) {
return round(x)
} else {
return Math.ceil(x)
}
},
'Complex': function (x) {
return x.ceil()
},
'BigNumber': function (x) {
return x.ceil()
if (bigNearlyEqual(x, round(x), config.epsilon)) {
return round(x)
} else {
return x.ceil()
}
},
'Fraction': function (x) {

View File

@ -3,6 +3,9 @@
const deepMap = require('../../utils/collection/deepMap')
function factory (type, config, load, typed) {
const ceil = load(require('../../function/arithmetic/ceil'))
const floor = load(require('../../function/arithmetic/floor'))
/**
* Round a value towards zero.
* For matrices, the function is evaluated element wise.
@ -32,7 +35,7 @@ function factory (type, config, load, typed) {
*/
const fix = typed('fix', {
'number': function (x) {
return (x > 0) ? Math.floor(x) : Math.ceil(x)
return (x > 0) ? floor(x) : ceil(x)
},
'Complex': function (x) {
@ -43,7 +46,7 @@ function factory (type, config, load, typed) {
},
'BigNumber': function (x) {
return x.isNegative() ? x.ceil() : x.floor()
return x.isNegative() ? ceil(x) : floor(x)
},
'Fraction': function (x) {

View File

@ -1,8 +1,12 @@
'use strict'
const deepMap = require('../../utils/collection/deepMap')
const nearlyEqual = require('../../utils/number').nearlyEqual
const bigNearlyEqual = require('../../utils/bignumber/nearlyEqual')
function factory (type, config, load, typed) {
const round = load(require('../../function/arithmetic/round'))
/**
* Round a value towards minus infinity.
* For matrices, the function is evaluated element wise.
@ -31,14 +35,24 @@ function factory (type, config, load, typed) {
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
*/
const floor = typed('floor', {
'number': Math.floor,
'number': function (x) {
if (nearlyEqual(x, round(x), config.epsilon)) {
return round(x)
} else {
return Math.floor(x)
}
},
'Complex': function (x) {
return x.floor()
},
'BigNumber': function (x) {
return x.floor()
if (bigNearlyEqual(x, round(x), config.epsilon)) {
return round(x)
} else {
return x.floor()
}
},
'Fraction': function (x) {

View File

@ -2,6 +2,10 @@
function factory (type, config, load, typed) {
const matrix = load(require('../../type/matrix/function/matrix'))
const smaller = load(require('../relational/smaller'))
const larger = load(require('../relational/larger'))
const smallerEq = load(require('../relational/smallerEq'))
const largerEq = load(require('../relational/largerEq'))
const ZERO = new type.BigNumber(0)
const ONE = new type.BigNumber(1)
@ -131,12 +135,12 @@ function factory (type, config, load, typed) {
const array = []
let x = start
if (step > 0) {
while (x < end) {
while (smaller(x, end)) {
array.push(x)
x += step
}
} else if (step < 0) {
while (x > end) {
while (larger(x, end)) {
array.push(x)
x += step
}
@ -157,12 +161,12 @@ function factory (type, config, load, typed) {
const array = []
let x = start
if (step > 0) {
while (x <= end) {
while (smallerEq(x, end)) {
array.push(x)
x += step
}
} else if (step < 0) {
while (x >= end) {
while (largerEq(x, end)) {
array.push(x)
x += step
}
@ -183,12 +187,12 @@ function factory (type, config, load, typed) {
const array = []
let x = start
if (step.gt(ZERO)) {
while (x.lt(end)) {
while (smaller(x, end)) {
array.push(x)
x = x.plus(step)
}
} else if (step.lt(ZERO)) {
while (x.gt(end)) {
while (larger(x, end)) {
array.push(x)
x = x.plus(step)
}
@ -209,12 +213,12 @@ function factory (type, config, load, typed) {
const array = []
let x = start
if (step.gt(ZERO)) {
while (x.lte(end)) {
while (smallerEq(x, end)) {
array.push(x)
x = x.plus(step)
}
} else if (step.lt(ZERO)) {
while (x.gte(end)) {
while (largerEq(x, end)) {
array.push(x)
x = x.plus(step)
}

View File

@ -49,7 +49,7 @@ describe('ceil', function () {
approx.deepEqual(ceil(complex(-1.3, -1.8)), complex(-1, -1))
})
it('should return the ceil of a number', function () {
it('should return the ceil of a fraction', function () {
const a = fraction('2/3')
assert(ceil(a) instanceof math.type.Fraction)
assert.strictEqual(a.toString(), '0.(6)')
@ -66,6 +66,28 @@ describe('ceil', function () {
assert.strictEqual(ceil(fraction(-2.1)).toString(), '-2')
})
it('should gracefully handle round-off errors', function () {
assert.strictEqual(ceil(3.0000000000000004), 3)
assert.strictEqual(ceil(7.999999999999999), 8)
assert.strictEqual(ceil(-3.0000000000000004), -3)
assert.strictEqual(ceil(-7.999999999999999), -8)
assert.strictEqual(ceil(30000.000000000004), 30000)
assert.strictEqual(ceil(799999.9999999999), 800000)
assert.strictEqual(ceil(-30000.000000000004), -30000)
assert.strictEqual(ceil(-799999.9999999999), -800000)
})
it('should gracefully handle round-off errors with bignumbers', function () {
assert.deepStrictEqual(ceil(bignumber(3.0000000000000004)), bignumber(3))
assert.deepStrictEqual(ceil(bignumber(7.999999999999999)), bignumber(8))
assert.deepStrictEqual(ceil(bignumber(-3.0000000000000004)), bignumber(-3))
assert.deepStrictEqual(ceil(bignumber(-7.999999999999999)), bignumber(-8))
assert.deepStrictEqual(ceil(bignumber(30000.000000000004)), bignumber(30000))
assert.deepStrictEqual(ceil(bignumber(799999.9999999999)), bignumber(800000))
assert.deepStrictEqual(ceil(bignumber(-30000.000000000004)), bignumber(-30000))
assert.deepStrictEqual(ceil(bignumber(-799999.9999999999)), bignumber(-800000))
})
it('should throw an error for units', function () {
assert.throws(function () { ceil(unit('5cm')) }, TypeError, 'Function ceil(unit) not supported')
})

View File

@ -67,6 +67,28 @@ describe('fix', function () {
assert.strictEqual(fix(fraction(-2.1)).toString(), '-2')
})
it('should gracefully handle round-off errors', function () {
assert.strictEqual(fix(3.0000000000000004), 3)
assert.strictEqual(fix(7.999999999999999), 8)
assert.strictEqual(fix(-3.0000000000000004), -3)
assert.strictEqual(fix(-7.999999999999999), -8)
assert.strictEqual(fix(30000.000000000004), 30000)
assert.strictEqual(fix(799999.9999999999), 800000)
assert.strictEqual(fix(-30000.000000000004), -30000)
assert.strictEqual(fix(-799999.9999999999), -800000)
})
it('should gracefully handle round-off errors with bignumbers', function () {
assert.deepStrictEqual(fix(bignumber(3.0000000000000004)), bignumber(3))
assert.deepStrictEqual(fix(bignumber(7.999999999999999)), bignumber(8))
assert.deepStrictEqual(fix(bignumber(-3.0000000000000004)), bignumber(-3))
assert.deepStrictEqual(fix(bignumber(-7.999999999999999)), bignumber(-8))
assert.deepStrictEqual(fix(bignumber(30000.000000000004)), bignumber(30000))
assert.deepStrictEqual(fix(bignumber(799999.9999999999)), bignumber(800000))
assert.deepStrictEqual(fix(bignumber(-30000.000000000004)), bignumber(-30000))
assert.deepStrictEqual(fix(bignumber(-799999.9999999999)), bignumber(-800000))
})
it('should throw an error on unit as parameter', function () {
// unit
assert.throws(function () { fix(unit('5cm')) }, TypeError, 'Function fix(unit) not supported')

View File

@ -66,6 +66,26 @@ describe('floor', function () {
assert.strictEqual(floor(fraction(-2.1)).toString(), '-3')
})
it('should gracefully handle round-off errors', function () {
assert.strictEqual(floor(3.0000000000000004), 3)
assert.strictEqual(floor(7.999999999999999), 8)
assert.strictEqual(floor(-3.0000000000000004), -3)
assert.strictEqual(floor(-7.999999999999999), -8)
assert.strictEqual(floor(30000.000000000004), 30000)
assert.strictEqual(floor(799999.9999999999), 800000)
assert.strictEqual(floor(-30000.000000000004), -30000)
})
it('should gracefully handle round-off errors with bignumbers', function () {
assert.deepStrictEqual(floor(bignumber(3.0000000000000004)), bignumber(3))
assert.deepStrictEqual(floor(bignumber(7.999999999999999)), bignumber(8))
assert.deepStrictEqual(floor(bignumber(-3.0000000000000004)), bignumber(-3))
assert.deepStrictEqual(floor(bignumber(-7.999999999999999)), bignumber(-8))
assert.deepStrictEqual(floor(bignumber(30000.000000000004)), bignumber(30000))
assert.deepStrictEqual(floor(bignumber(799999.9999999999)), bignumber(800000))
assert.deepStrictEqual(floor(bignumber(-30000.000000000004)), bignumber(-30000))
})
it('should throw an error with a unit', function () {
assert.throws(function () { floor(unit('5cm')) }, TypeError, 'Function floor(unit) not supported')
})

View File

@ -84,6 +84,18 @@ describe('range', function () {
assert.throws(function () { bigmath.range('1:a') }, /is no valid range/)
})
it('should gracefully handle round-off errors', function () {
assert.deepStrictEqual(range(1, 2, 0.1, true)._size, [11])
assert.deepStrictEqual(range(0.1, 0.2, 0.01, true)._size, [11])
assert.deepStrictEqual(range(1, 5, 0.1)._size, [40])
assert.deepStrictEqual(range(2, 1, -0.1, true)._size, [11])
assert.deepStrictEqual(range(5, 1, -0.1)._size, [40])
assert.deepStrictEqual(range(-3.2909135802469143, 3.2909135802469143, (3.2909135802469143 + 3.2909135802469143) / 10, true)._size, [11])
assert.deepStrictEqual(range(-3.2909135802469143, 3.2909135802469143, (3.2909135802469143 + 3.2909135802469143) / 9, true)._size, [10])
assert.deepStrictEqual(range(-3.2909135802469143, 3.2909135802469143, (3.2909135802469143 + 3.2909135802469143) / 10)._size, [10])
assert.deepStrictEqual(range(-3.2909135802469143, 3.2909135802469143, (3.2909135802469143 + 3.2909135802469143) / 9)._size, [9])
})
describe('option includeEnd', function () {
it('should parse a string and include end', function () {
assert.deepStrictEqual(range('1:6', false), matrix([1, 2, 3, 4, 5]))