mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
52 lines
1016 B
JavaScript
52 lines
1016 B
JavaScript
/**
|
|
* Compute the argument of a complex value.
|
|
* If x = a+bi, the argument is computed as atan2(b, a).
|
|
* @param {Number | Complex} x
|
|
* @return {Number} res
|
|
*/
|
|
function arg(x) {
|
|
if (arguments.length != 1) {
|
|
throw newArgumentsError('arg', arguments.length, 1);
|
|
}
|
|
|
|
if (isNumber(x)) {
|
|
return Math.atan2(0, x);
|
|
}
|
|
|
|
if (x instanceof Complex) {
|
|
return Math.atan2(x.im, x.re);
|
|
}
|
|
|
|
// TODO: implement array support
|
|
// TODO: implement matrix support
|
|
|
|
throw newUnsupportedTypeError('arg', x);
|
|
}
|
|
|
|
math.arg = arg;
|
|
|
|
/**
|
|
* Function documentation
|
|
*/
|
|
arg.doc = {
|
|
'name': 'arg',
|
|
'category': 'Complex',
|
|
'syntax': [
|
|
'arg(x)'
|
|
],
|
|
'description':
|
|
'Compute the argument of a complex value. ' +
|
|
'If x = a+bi, the argument is computed as atan2(b, a).',
|
|
'examples': [
|
|
'arg(2 + 2i)',
|
|
'atan2(3, 2)',
|
|
'arg(2 - 3i)'
|
|
],
|
|
'seealso': [
|
|
're',
|
|
'im',
|
|
'conj',
|
|
'abs'
|
|
]
|
|
};
|