mathjs/test/function/complex/arg.test.js

68 lines
2.4 KiB
JavaScript

var assert = require('assert'),
error = require('../../../lib/error/index'),
approx = require('../../../tools/approx'),
math = require('../../../index')(),
arg = math.arg;
describe('arg', function() {
it('should compute the argument of a boolean', function () {
assert.equal(arg(true), 0);
assert.equal(arg(false), 0);
});
it('should compute the argument of a number', function () {
assert.equal(arg(1), 0);
assert.equal(arg(2), 0);
assert.equal(arg(0), 0);
approx.equal(arg(-2), 3.141592653589793);
});
it('should compute the argument of a bignumber (downgrades to number)', function () {
assert.equal(arg(math.bignumber(1)), 0);
});
it('should compute the argument of a complex number correctly', function() {
assert.equal(arg(math.complex('0')) / math.pi, 0);
assert.equal(arg(math.complex('1 + 0i')) / math.pi, 0);
assert.equal(arg(math.complex('1 + i')) / math.pi, 0.25);
assert.equal(arg(math.complex('0 + i')) / math.pi, 0.5);
assert.equal(arg(math.complex('-1 + i')) / math.pi, 0.75);
assert.equal(arg(math.complex('-1 + 0i')) / math.pi, 1);
assert.equal(arg(math.complex('-1 - i')) / math.pi, -0.75);
assert.equal(arg(math.complex('0 - i')) / math.pi, -0.5);
assert.equal(arg(math.complex('1 - i')) / math.pi, -0.25);
assert.equal(arg(math.i) / math.pi, 0.5);
});
it('should calculate the argument for each element in a matrix', function() {
assert.deepEqual(math.divide(arg([
math.i, math.unaryminus(math.i), math.add(1,math.i)
]), math.pi), [
0.5, -0.5, 0.25
]);
assert.deepEqual(math.matrix(math.divide(arg([
math.i, math.unaryminus(math.i), math.add(1,math.i)
]), math.pi)).valueOf(), [
0.5, -0.5, 0.25
]);
});
it('should compute the argument of a real number correctly', function() {
assert.equal(arg(2) / math.pi, 0);
assert.equal(arg(-2) / math.pi, 1);
});
it('should throw an error if used with a string', function() {
assert.throws(function () {arg('string')});
});
it('should throw an error if used with a unit', function() {
assert.throws(function () {arg(math.unit('5cm'))});
});
it('should throw an error in case of invalid number of arguments', function() {
assert.throws(function () {arg()}, error.ArgumentsError);
assert.throws(function () {arg(1, 2)}, error.ArgumentsError);
});
});