mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
Implemented construction function number
This commit is contained in:
parent
0b2e28403b
commit
755d0769e6
@ -10,6 +10,8 @@ https://github.com/josdejong/mathjs
|
||||
`scope`, similar to `parse`. Example: `math.eval('x^a', {x:3, a:2});`.
|
||||
- Implemented function `subset`, to get or set a subset from a matrix, string,
|
||||
or other data types.
|
||||
- Implemented construction functions number and string (mainly useful inside
|
||||
the parser).
|
||||
- Improved function `det`. Thanks Bryan Cuccioli (bcuccioli).
|
||||
- Moved the parse code from prototype math.expr.Parser to function math.parse,
|
||||
simplified Parser a little bit.
|
||||
|
||||
11
README.md
11
README.md
@ -538,6 +538,17 @@ types (Number, Complex, Unit, String, and Array) where applicable.
|
||||
- math.arg(x)
|
||||
- math.conj(x)
|
||||
|
||||
### Construction
|
||||
|
||||
- math.complex(re, im)
|
||||
- math.matrix(x)
|
||||
- math.number(x)
|
||||
- math.parser()
|
||||
- math.range(start [, step] , end)
|
||||
- math.string(x)
|
||||
- math.unit(x)
|
||||
- math.workspace()
|
||||
|
||||
### Matrix
|
||||
|
||||
- math.concat(a, b, c, ... [, dim])
|
||||
|
||||
25
math.js
25
math.js
@ -737,7 +737,7 @@ function Complex(re, im) {
|
||||
|
||||
if ((re != null && !isNumber(re)) || (im != null && !isNumber(im))) {
|
||||
throw new TypeError(
|
||||
'Two numbers or a single string expected in Complex constructor');
|
||||
'Two numbers expected in Complex constructor');
|
||||
}
|
||||
|
||||
this.re = re || 0;
|
||||
@ -6480,6 +6480,29 @@ math.matrix = function matrix(data) {
|
||||
return new Matrix(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a number or convert a string to a number
|
||||
* @param {String | Number | Boolean} [value]
|
||||
* @return {Number} num
|
||||
*/
|
||||
math.number = function (value) {
|
||||
switch (arguments.length) {
|
||||
case 0:
|
||||
return 0;
|
||||
case 1:
|
||||
var num = Number(value);
|
||||
if (isNaN(num)) {
|
||||
num = Number(value.valueOf());
|
||||
}
|
||||
if (isNaN(num)) {
|
||||
throw new SyntaxError(value.toString() + ' is no valid number');
|
||||
}
|
||||
return num;
|
||||
default:
|
||||
throw newArgumentsError('number', arguments.length, 0, 1);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a parser. The function creates a new math.expr.Parser object.
|
||||
*
|
||||
|
||||
6
math.min.js
vendored
6
math.min.js
vendored
File diff suppressed because one or more lines are too long
22
src/function/construction/number.js
Normal file
22
src/function/construction/number.js
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Create a number or convert a string to a number
|
||||
* @param {String | Number | Boolean} [value]
|
||||
* @return {Number} num
|
||||
*/
|
||||
math.number = function (value) {
|
||||
switch (arguments.length) {
|
||||
case 0:
|
||||
return 0;
|
||||
case 1:
|
||||
var num = Number(value);
|
||||
if (isNaN(num)) {
|
||||
num = Number(value.valueOf());
|
||||
}
|
||||
if (isNaN(num)) {
|
||||
throw new SyntaxError(value.toString() + ' is no valid number');
|
||||
}
|
||||
return num;
|
||||
default:
|
||||
throw newArgumentsError('number', arguments.length, 0, 1);
|
||||
}
|
||||
};
|
||||
@ -25,7 +25,7 @@ function Complex(re, im) {
|
||||
|
||||
if ((re != null && !isNumber(re)) || (im != null && !isNumber(im))) {
|
||||
throw new TypeError(
|
||||
'Two numbers or a single string expected in Complex constructor');
|
||||
'Two numbers expected in Complex constructor');
|
||||
}
|
||||
|
||||
this.re = re || 0;
|
||||
|
||||
39
test/function/construction/number.js
Normal file
39
test/function/construction/number.js
Normal file
@ -0,0 +1,39 @@
|
||||
// test number construction
|
||||
var assert = require('assert'),
|
||||
math = require('../../../math.js'),
|
||||
approx = require('../../../tools/approx.js'),
|
||||
number = math.number;
|
||||
|
||||
// 0 arguments
|
||||
approx.equal(number(), 0);
|
||||
|
||||
// 1 argument
|
||||
|
||||
// boolean input
|
||||
approx.equal(number(true), 1);
|
||||
approx.equal(number(false), 0);
|
||||
|
||||
// number input
|
||||
approx.equal(number(3), 3);
|
||||
approx.equal(number(-3), -3);
|
||||
|
||||
// string input
|
||||
approx.equal(number('2.1e3'), 2100);
|
||||
approx.equal(number(' 2.1e-3 '), 0.0021);
|
||||
approx.equal(number(''), 0);
|
||||
approx.equal(number(' '), 0);
|
||||
assert.throws(function () {number('2.3.4')}, SyntaxError);
|
||||
assert.throws(function () {number('23a')}, SyntaxError);
|
||||
|
||||
// wrong number of arguments
|
||||
assert.throws(function () {number(1,2)}, SyntaxError);
|
||||
assert.throws(function () {number(1,2,3)}, SyntaxError);
|
||||
|
||||
// wrong type of arguments
|
||||
assert.throws(function () {number(math.complex(2,3))}, SyntaxError);
|
||||
assert.throws(function () {number(math.unit('5cm'))}, SyntaxError);
|
||||
assert.throws(function () {number(math.range(1,3))}, SyntaxError);
|
||||
assert.throws(function () {number(math.matrix([1,3]))}, SyntaxError);
|
||||
assert.throws(function () {number([1,3])}, SyntaxError);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user