mirror of
https://github.com/josdejong/mathjs.git
synced 2026-02-01 16:07:46 +00:00
Fixed tabs
This commit is contained in:
parent
ce213ca467
commit
55d3842d60
@ -36,9 +36,9 @@ function factory(type, config, load, typed) {
|
||||
},
|
||||
|
||||
'number, Unit': function (x, y) {
|
||||
var res = y.pow(-1);
|
||||
res.value = ((res.value === null) ? res._normalize(1) : res.value) * x;
|
||||
return res;
|
||||
var res = y.pow(-1);
|
||||
res.value = ((res.value === null) ? res._normalize(1) : res.value) * x;
|
||||
return res;
|
||||
},
|
||||
|
||||
'Unit, Unit': function (x, y) {
|
||||
|
||||
@ -775,49 +775,49 @@ function factory (type, config, load, typed) {
|
||||
{
|
||||
this.units = [];
|
||||
}
|
||||
else {
|
||||
var matchingUnit;
|
||||
if(matchingBase) {
|
||||
// Does the unit system have a matching unit?
|
||||
if(currentUnitSystem.hasOwnProperty(matchingBase)) {
|
||||
matchingUnit = currentUnitSystem[matchingBase]
|
||||
}
|
||||
}
|
||||
else {
|
||||
var matchingUnit;
|
||||
if(matchingBase) {
|
||||
// Does the unit system have a matching unit?
|
||||
if(currentUnitSystem.hasOwnProperty(matchingBase)) {
|
||||
matchingUnit = currentUnitSystem[matchingBase]
|
||||
}
|
||||
}
|
||||
|
||||
var value;
|
||||
var str;
|
||||
if(matchingUnit) {
|
||||
this.units = [{
|
||||
unit: matchingUnit.unit,
|
||||
prefix: matchingUnit.prefix,
|
||||
power: 1.0,
|
||||
}];
|
||||
}
|
||||
else {
|
||||
// Multiple units or units with powers are formatted like this:
|
||||
// 5 (kg m^2) / (s^3 mol)
|
||||
// Build an representation from the base units of the current unit system
|
||||
for(var i=0; i<BASE_DIMENSIONS.length; i++) {
|
||||
var baseDim = BASE_DIMENSIONS[i];
|
||||
if(Math.abs(this.dimensions[i]) > 1e-12) {
|
||||
proposedUnitList.push({
|
||||
unit: currentUnitSystem[baseDim].unit,
|
||||
prefix: currentUnitSystem[baseDim].prefix,
|
||||
power: this.dimensions[i]
|
||||
});
|
||||
}
|
||||
}
|
||||
var value;
|
||||
var str;
|
||||
if(matchingUnit) {
|
||||
this.units = [{
|
||||
unit: matchingUnit.unit,
|
||||
prefix: matchingUnit.prefix,
|
||||
power: 1.0,
|
||||
}];
|
||||
}
|
||||
else {
|
||||
// Multiple units or units with powers are formatted like this:
|
||||
// 5 (kg m^2) / (s^3 mol)
|
||||
// Build an representation from the base units of the current unit system
|
||||
for(var i=0; i<BASE_DIMENSIONS.length; i++) {
|
||||
var baseDim = BASE_DIMENSIONS[i];
|
||||
if(Math.abs(this.dimensions[i]) > 1e-12) {
|
||||
proposedUnitList.push({
|
||||
unit: currentUnitSystem[baseDim].unit,
|
||||
prefix: currentUnitSystem[baseDim].prefix,
|
||||
power: this.dimensions[i]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Is the proposed unit list "simpler" than the existing one?
|
||||
if(proposedUnitList.length < this.units.length) {
|
||||
// Replace this unit list with the proposed list
|
||||
this.units = proposedUnitList;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Is the proposed unit list "simpler" than the existing one?
|
||||
if(proposedUnitList.length < this.units.length) {
|
||||
// Replace this unit list with the proposed list
|
||||
this.units = proposedUnitList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.isUnitListSimplified = true;
|
||||
return;
|
||||
this.isUnitListSimplified = true;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -894,30 +894,30 @@ function factory (type, config, load, typed) {
|
||||
*/
|
||||
Unit.prototype.format = function (options) {
|
||||
|
||||
// Simplfy the unit list, if necessary
|
||||
// Simplfy the unit list, if necessary
|
||||
this.simplifyUnitListLazy();
|
||||
|
||||
// Now apply the best prefix
|
||||
// Units must have only one unit and not have the fixPrefix flag set
|
||||
if (this.units.length === 1 && !this.fixPrefix) {
|
||||
// Units must have integer powers, otherwise the prefix will change the
|
||||
// outputted value by not-an-integer-power-of-ten
|
||||
if (Math.abs(this.units[0].power - Math.round(this.units[0].power)) < 1e-14) {
|
||||
// Apply the prefix
|
||||
var bestPrefix = this._bestPrefix();
|
||||
this.units[0].prefix = bestPrefix;
|
||||
}
|
||||
}
|
||||
// Now apply the best prefix
|
||||
// Units must have only one unit and not have the fixPrefix flag set
|
||||
if (this.units.length === 1 && !this.fixPrefix) {
|
||||
// Units must have integer powers, otherwise the prefix will change the
|
||||
// outputted value by not-an-integer-power-of-ten
|
||||
if (Math.abs(this.units[0].power - Math.round(this.units[0].power)) < 1e-14) {
|
||||
// Apply the prefix
|
||||
var bestPrefix = this._bestPrefix();
|
||||
this.units[0].prefix = bestPrefix;
|
||||
}
|
||||
}
|
||||
|
||||
var value = this._denormalize(this.value);
|
||||
var str = (this.value !== null) ? (format(value, options)) : '';
|
||||
var unitStr = this.formatUnits();
|
||||
if(unitStr.length > 0 && str.length > 0) {
|
||||
str += " ";
|
||||
}
|
||||
str += unitStr;
|
||||
var value = this._denormalize(this.value);
|
||||
var str = (this.value !== null) ? (format(value, options)) : '';
|
||||
var unitStr = this.formatUnits();
|
||||
if(unitStr.length > 0 && str.length > 0) {
|
||||
str += " ";
|
||||
}
|
||||
str += unitStr;
|
||||
|
||||
return str;
|
||||
return str;
|
||||
|
||||
/*
|
||||
var value,
|
||||
@ -950,24 +950,24 @@ function factory (type, config, load, typed) {
|
||||
|
||||
|
||||
return str;
|
||||
*/
|
||||
*/
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the fixPrefix flag to true.
|
||||
*/
|
||||
Unit.prototype._fix = function() {
|
||||
this.fixPrefix = true;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Sets the fixPrefix flag to true.
|
||||
*/
|
||||
Unit.prototype._fix = function() {
|
||||
this.fixPrefix = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fixPrefix flag to false.
|
||||
*/
|
||||
Unit.prototype._unfix = function() {
|
||||
this.fixPrefix = false;
|
||||
return this;
|
||||
}
|
||||
Unit.prototype._unfix = function() {
|
||||
this.fixPrefix = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the best prefix using current value.
|
||||
@ -975,10 +975,10 @@ function factory (type, config, load, typed) {
|
||||
* @private
|
||||
*/
|
||||
Unit.prototype._bestPrefix = function () {
|
||||
if (this.units.length !== 1) {
|
||||
if (this.units.length !== 1) {
|
||||
throw new Error("Can only compute the best prefix for single units with integer powers, like kg, s^2, N^-1, and so forth!");
|
||||
}
|
||||
if (Math.abs(this.units[0].power - Math.round(this.units[0].power)) >= 1e-14) {
|
||||
}
|
||||
if (Math.abs(this.units[0].power - Math.round(this.units[0].power)) >= 1e-14) {
|
||||
throw new Error("Can only compute the best prefix for single units with integer powers, like kg, s^2, N^-1, and so forth!");
|
||||
}
|
||||
|
||||
@ -988,11 +988,11 @@ function factory (type, config, load, typed) {
|
||||
// sequence 1mm 100mm 500mm 0.6m 1m 10m 100m 500m 0.6km 1km ...
|
||||
|
||||
var absValue = Math.abs(this.value); // / this.units[0].unit.value);
|
||||
var bestPrefix = this.units[0].prefix;
|
||||
if (absValue === 0) {
|
||||
return bestPrefix;
|
||||
}
|
||||
var power = this.units[0].power;
|
||||
var bestPrefix = this.units[0].prefix;
|
||||
if (absValue === 0) {
|
||||
return bestPrefix;
|
||||
}
|
||||
var power = this.units[0].power;
|
||||
var bestDiff = Math.abs(
|
||||
Math.log(absValue / Math.pow(bestPrefix.value * this.units[0].unit.value, power)) / Math.LN10 - 1.2);
|
||||
|
||||
|
||||
@ -365,21 +365,21 @@ describe('unit', function() {
|
||||
assert.equal(new Unit(600 ,'m').toString(), '0.6 km');
|
||||
assert.equal(new Unit(1000 ,'m').toString(), '1 km');
|
||||
assert.equal(new Unit(1000 ,'ohm').toString(), '1 kohm');
|
||||
});
|
||||
|
||||
it('should render best prefix for a single unit raised to integral power', function() {
|
||||
assert.equal(new Unit(3.2e7, 'm^2').toString(), "32 km^2");
|
||||
assert.equal(new Unit(3.2e-7, 'm^2').toString(), "0.32 mm^2");
|
||||
assert.equal(new Unit(15000, 'm^-1').toString(), "15 mm^-1");
|
||||
assert.equal(new Unit(3e-9, 'm^-2').toString(), "3000 Mm^-2");
|
||||
assert.equal(new Unit(3e-9, 'm^-1.5').toString(), "3e-9 m^-1.5");
|
||||
assert.equal(new Unit(2, 'kg^0').toString(), "2");
|
||||
});
|
||||
|
||||
it('should not render best prefix if "fixPrefix" is set', function() {
|
||||
assert.equal(new Unit(5e-3, 'm')._fix().toString(), "0.005 m");
|
||||
assert.equal(new Unit(5e-3, 'm')._fix()._unfix().toString(), "5 mm");
|
||||
});
|
||||
it('should render best prefix for a single unit raised to integral power', function() {
|
||||
assert.equal(new Unit(3.2e7, 'm^2').toString(), "32 km^2");
|
||||
assert.equal(new Unit(3.2e-7, 'm^2').toString(), "0.32 mm^2");
|
||||
assert.equal(new Unit(15000, 'm^-1').toString(), "15 mm^-1");
|
||||
assert.equal(new Unit(3e-9, 'm^-2').toString(), "3000 Mm^-2");
|
||||
assert.equal(new Unit(3e-9, 'm^-1.5').toString(), "3e-9 m^-1.5");
|
||||
assert.equal(new Unit(2, 'kg^0').toString(), "2");
|
||||
});
|
||||
|
||||
it('should not render best prefix if "fixPrefix" is set', function() {
|
||||
assert.equal(new Unit(5e-3, 'm')._fix().toString(), "0.005 m");
|
||||
assert.equal(new Unit(5e-3, 'm')._fix()._unfix().toString(), "5 mm");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user