mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
Renamed Unit.as to Unit.toNumber (more clear, and more consistent with the rest of the library)
This commit is contained in:
parent
e682dc35bd
commit
ec4219c23f
23
CHANGELOG.md
23
CHANGELOG.md
@ -1,9 +1,20 @@
|
||||
# math.js changelog
|
||||
https://github.com/josdejong/mathjs
|
||||
|
||||
|
||||
## not yet relesed, version 0.6.0
|
||||
|
||||
- Implemented methods gcd and lcm.
|
||||
- Implemented method `Unit.in(unit)`, which creates a clone of the unit with a
|
||||
fixed representation. For example `math.unit('5.08 cm').in('inch')` will
|
||||
return a unit which string representation always is in inch, thus `2 inch`.
|
||||
`Unit.in(unit)` is the same as method `math.in(x, unit)`.
|
||||
- Implemented `Unit.toNumber(unit)`, which returns the value of the unit when
|
||||
represented with given unit. For example
|
||||
`math.unit('5.08 cm').toNumber('inch')` returns the number `2`, as the
|
||||
representation of the unit in inches has 2 as value.
|
||||
- Improved: method `math.in(x, unit)` now supports a string as second parameter,
|
||||
for example `math.in(math.unit('5.08 cm'), 'inch')`.
|
||||
|
||||
|
||||
## 2013-04-06, version 0.5.0
|
||||
@ -13,18 +24,18 @@ https://github.com/josdejong/mathjs
|
||||
- Implemented data types Matrix and Range.
|
||||
- Implemented matrix methods clone, concat, det, diag, eye, inv, ones, size,
|
||||
squeeze, transpose, zeros.
|
||||
- Implemented range operator (:), and transpose operator (') in parser.s
|
||||
- Implemented range operator `:`, and transpose operator `'` in parser.
|
||||
- Changed: created construction methods for easy object creation for all data
|
||||
types and for the parser. For example, a complex value is now created
|
||||
with "math.complex(2, 3)" instead of "new math.Complex(2, 3)", and a parser
|
||||
is now created with "math.parser()" instead of "new math.parser.Parser()".
|
||||
with `math.complex(2, 3)` instead of `new math.Complex(2, 3)`, and a parser
|
||||
is now created with `math.parser()` instead of `new math.parser.Parser()`.
|
||||
- Changed: moved all data types under the namespace math.type, and moved the
|
||||
Parser, Workspace, etc. under the namespace math.expr.
|
||||
- Changed: changed operator precedence of the power operator:
|
||||
- it is now right associative instead of left associative like most scripting
|
||||
languages. So 2^3^4 is now calculated as 2^(3^4).
|
||||
- it has now higher precedence than unary minus most languages, thus -3^2 is
|
||||
now calculated as -(3^2).
|
||||
languages. So `2^3^4` is now calculated as `2^(3^4)`.
|
||||
- it has now higher precedence than unary minus most languages, thus `-3^2` is
|
||||
now calculated as `-(3^2)`.
|
||||
- Changed: renamed the parsers method 'put' into 'set'.
|
||||
- Fixed: method 'in' did not check for units to have the same base.
|
||||
|
||||
|
||||
@ -223,6 +223,8 @@ Math.js supports units.
|
||||
var a = math.unit(55, 'cm'); // 550 mm
|
||||
var b = math.unit('0.1m'); // 100 mm
|
||||
math.add(a, b); // 0.65 m
|
||||
b.in('cm'); // 10 cm
|
||||
b.toNumber('cm'); // 10
|
||||
|
||||
var parser = math.parser();
|
||||
parser.eval('2 inch in cm'); // 5.08 cm
|
||||
|
||||
2
math.js
2
math.js
@ -2451,7 +2451,7 @@ Unit.prototype.in = function (plainUnit) {
|
||||
* @param {String | Unit} plainUnit For example 'cm' or 'inch'
|
||||
* @return {Number} value
|
||||
*/
|
||||
Unit.prototype.as = function (plainUnit) {
|
||||
Unit.prototype.toNumber = function (plainUnit) {
|
||||
var other = this.in(plainUnit);
|
||||
var prefix = this.fixPrefix ? other._bestPrefix() : other.prefix;
|
||||
return other._unnormalize(other.value, prefix.value);
|
||||
|
||||
2
math.min.js
vendored
2
math.min.js
vendored
File diff suppressed because one or more lines are too long
@ -361,7 +361,7 @@ Unit.prototype.in = function (plainUnit) {
|
||||
* @param {String | Unit} plainUnit For example 'cm' or 'inch'
|
||||
* @return {Number} value
|
||||
*/
|
||||
Unit.prototype.as = function (plainUnit) {
|
||||
Unit.prototype.toNumber = function (plainUnit) {
|
||||
var other = this.in(plainUnit);
|
||||
var prefix = this.fixPrefix ? other._bestPrefix() : other.prefix;
|
||||
return other._unnormalize(other.value, prefix.value);
|
||||
|
||||
@ -32,11 +32,11 @@ var u = math.unit(5000, 'cm');
|
||||
assert.equal(u.toString(), '50 m');
|
||||
var u2 = u.in('mm');
|
||||
assert.equal(u2.toString(), '50000 mm');
|
||||
assert.strictEqual(u.as('mm'), 50000);
|
||||
assert.strictEqual(u.toNumber('mm'), 50000);
|
||||
assert.throws( function () {u.in('5mm'); });
|
||||
var u3 = math.unit('5.08 cm').in('inch');
|
||||
assert.equal(u3.toString(), '2 inch');
|
||||
assert.strictEqual(math.format(math.unit('5.08 cm').as('inch')), '2');
|
||||
assert.strictEqual(math.format(math.unit('5.08 cm').toNumber('inch')), '2');
|
||||
|
||||
// test calculation of best prefix
|
||||
assert.equal(math.unit('0.001m').toString(), '1 mm');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user