mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
39 lines
699 B
JavaScript
39 lines
699 B
JavaScript
/**
|
|
* Calculate the square root of a value
|
|
* @param {Number | Complex} x
|
|
* @return {Number | Complex} res
|
|
*/
|
|
function abs(x) {
|
|
if (isNumber(x)) {
|
|
return Math.abs(x);
|
|
}
|
|
|
|
if (x instanceof Complex) {
|
|
return Math.sqrt(x.re * x.re + x.im * x.im);
|
|
}
|
|
|
|
// TODO: implement array support
|
|
// TODO: implement matrix support
|
|
|
|
throw newUnsupportedTypeError('abs', x);
|
|
}
|
|
|
|
math.abs = abs;
|
|
|
|
/**
|
|
* Function documentation
|
|
*/
|
|
abs.doc = {
|
|
'name': 'abs',
|
|
'category': 'Arithmetic',
|
|
'syntax': [
|
|
'abs(x)'
|
|
],
|
|
'description': 'Compute the absolute value.',
|
|
'examples': [
|
|
'abs(3.5)',
|
|
'abs(-4.2)'
|
|
],
|
|
'seealso': ['sign']
|
|
};
|