mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
Conflicts: HISTORY.md bower.json component.json dist/math.js dist/math.map dist/math.min.js docs/reference/units.md lib/version.js package.json test/function/construction/chain.test.js test/function/matrix/det.test.js test/function/matrix/transpose.test.js test/function/utils/import.test.js
60 lines
2.0 KiB
JavaScript
60 lines
2.0 KiB
JavaScript
// test sign
|
|
var assert = require('assert');
|
|
var approx = require('../../../tools/approx');
|
|
var math = require('../../../index');
|
|
var bignumber = math.bignumber;
|
|
|
|
describe('sign', function() {
|
|
it('should calculate the sign of a boolean', function () {
|
|
assert.equal(math.sign(true), 1);
|
|
assert.equal(math.sign(false), 0);
|
|
});
|
|
|
|
it('should calculate the sign of null', function () {
|
|
assert.equal(math.sign(null), 0);
|
|
});
|
|
|
|
it('should calculate the sign of a number', function() {
|
|
assert.equal(math.sign(3), 1);
|
|
assert.equal(math.sign(-3), -1);
|
|
assert.equal(math.sign(0), 0);
|
|
});
|
|
|
|
it('should calculate the sign of a big number', function() {
|
|
assert.deepEqual(math.sign(bignumber(3)), bignumber(1));
|
|
assert.deepEqual(math.sign(bignumber(-3)), bignumber(-1));
|
|
assert.deepEqual(math.sign(bignumber(0)), bignumber(0));
|
|
});
|
|
|
|
it('should calculate the sign of a complex value', function() {
|
|
approx.deepEqual(math.sign(math.complex(2,-3)), math.complex(0.554700196225229, -0.832050294337844));
|
|
});
|
|
|
|
it('should throw an error when used with a unit', function() {
|
|
assert.throws(function () { math.sign(math.unit('5cm')); });
|
|
});
|
|
|
|
it('should throw an error when used with a string', function() {
|
|
assert.throws(function () { math.sign("hello world"); });
|
|
});
|
|
|
|
it('should return a matrix of the signs of each elements in the given array', function() {
|
|
assert.deepEqual(math.sign([-2,-1,0,1,2]), [-1,-1,0,1,1]);
|
|
});
|
|
|
|
it('should return a matrix of the signs of each elements in the given matrix', function() {
|
|
assert.deepEqual(math.sign(math.matrix([-2,-1,0,1,2])), math.matrix([-1,-1,0,1,1]));
|
|
});
|
|
|
|
it('should throw an error in case of invalid number of arguments', function() {
|
|
assert.throws(function () {math.sign()}, /TypeError: Too few arguments/);
|
|
assert.throws(function () {math.sign(1, 2)}, /TypeError: Too many arguments/);
|
|
});
|
|
|
|
it('should LaTeX sign', function () {
|
|
var expression = math.parse('sign(-4)');
|
|
assert.equal(expression.toTex(), '\\mathrm{sign}\\left({-{4}}\\right)');
|
|
});
|
|
|
|
});
|