diff --git a/docs/datatypes/units.md b/docs/datatypes/units.md index 33502723ee..6ae2adde8e 100644 --- a/docs/datatypes/units.md +++ b/docs/datatypes/units.md @@ -124,7 +124,7 @@ units on the page [Syntax](../expressions/syntax.md#units). You can add your own units to Math.js using the `math.createUnit` function. The following example defines a new unit `furlong`, then uses the user-defined unit in a calculation: ```js -math.createUnit('furlong', '220 yards') +math.createUnit('furlong', '220 yards') math.evaluate('1 mile to furlong') // 8 furlong ``` @@ -132,7 +132,7 @@ If you cannot express the new unit in terms of any existing unit, then the secon ```js // A 'foo' cannot be expressed in terms of any other unit. -math.createUnit('foo') +math.createUnit('foo') math.evaluate('8 foo * 4 feet') // 32 foo feet ``` @@ -338,6 +338,8 @@ peta | P | 1e15 exa | E | 1e18 zetta | Z | 1e21 yotta | Y | 1e24 +ronna | R | 1e27 +quetta | Q | 1e30 Name | Abbreviation | Value ------ | ------------- | ----- @@ -351,6 +353,8 @@ femto | f | 1e-15 atto | a | 1e-18 zepto | z | 1e-21 yocto | y | 1e-24 +ronto | r | 1e-27 +quecto | q | 1e-30 The following binary prefixes are available. They can be used with units `bits` (`b`) and `bytes` (`B`). diff --git a/src/type/unit/Unit.js b/src/type/unit/Unit.js index 9379460203..0ae5c59818 100644 --- a/src/type/unit/Unit.js +++ b/src/type/unit/Unit.js @@ -1275,6 +1275,8 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ E: { name: 'E', value: 1e18, scientific: true }, Z: { name: 'Z', value: 1e21, scientific: true }, Y: { name: 'Y', value: 1e24, scientific: true }, + R: { name: 'R', value: 1e27, scientific: true }, + Q: { name: 'Q', value: 1e30, scientific: true }, d: { name: 'd', value: 1e-1, scientific: false }, c: { name: 'c', value: 1e-2, scientific: false }, @@ -1285,7 +1287,9 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ f: { name: 'f', value: 1e-15, scientific: true }, a: { name: 'a', value: 1e-18, scientific: true }, z: { name: 'z', value: 1e-21, scientific: true }, - y: { name: 'y', value: 1e-24, scientific: true } + y: { name: 'y', value: 1e-24, scientific: true }, + r: { name: 'r', value: 1e-27, scientific: true }, + q: { name: 'q', value: 1e-30, scientific: true } }, LONG: { '': { name: '', value: 1, scientific: true }, @@ -1300,6 +1304,8 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ exa: { name: 'exa', value: 1e18, scientific: true }, zetta: { name: 'zetta', value: 1e21, scientific: true }, yotta: { name: 'yotta', value: 1e24, scientific: true }, + ronna: { name: 'ronna', value: 1e27, scientific: true }, + quetta: { name: 'quetta', value: 1e30, scientific: true }, deci: { name: 'deci', value: 1e-1, scientific: false }, centi: { name: 'centi', value: 1e-2, scientific: false }, @@ -1310,7 +1316,9 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ femto: { name: 'femto', value: 1e-15, scientific: true }, atto: { name: 'atto', value: 1e-18, scientific: true }, zepto: { name: 'zepto', value: 1e-21, scientific: true }, - yocto: { name: 'yocto', value: 1e-24, scientific: true } + yocto: { name: 'yocto', value: 1e-24, scientific: true }, + ronto: { name: 'ronto', value: 1e-27, scientific: true }, + quecto: { name: 'quecto', value: 1e-30, scientific: true } }, SQUARED: { '': { name: '', value: 1, scientific: true }, @@ -1325,6 +1333,8 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ E: { name: 'E', value: 1e36, scientific: true }, Z: { name: 'Z', value: 1e42, scientific: true }, Y: { name: 'Y', value: 1e48, scientific: true }, + R: { name: 'R', value: 1e54, scientific: true }, + Q: { name: 'Q', value: 1e60, scientific: true }, d: { name: 'd', value: 1e-2, scientific: false }, c: { name: 'c', value: 1e-4, scientific: false }, @@ -1335,7 +1345,9 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ f: { name: 'f', value: 1e-30, scientific: true }, a: { name: 'a', value: 1e-36, scientific: true }, z: { name: 'z', value: 1e-42, scientific: true }, - y: { name: 'y', value: 1e-48, scientific: true } + y: { name: 'y', value: 1e-48, scientific: true }, + r: { name: 'r', value: 1e-54, scientific: true }, + q: { name: 'q', value: 1e-60, scientific: true } }, CUBIC: { '': { name: '', value: 1, scientific: true }, @@ -1350,6 +1362,8 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ E: { name: 'E', value: 1e54, scientific: true }, Z: { name: 'Z', value: 1e63, scientific: true }, Y: { name: 'Y', value: 1e72, scientific: true }, + R: { name: 'R', value: 1e81, scientific: true }, + Q: { name: 'Q', value: 1e90, scientific: true }, d: { name: 'd', value: 1e-3, scientific: false }, c: { name: 'c', value: 1e-6, scientific: false }, @@ -1360,7 +1374,9 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ f: { name: 'f', value: 1e-45, scientific: true }, a: { name: 'a', value: 1e-54, scientific: true }, z: { name: 'z', value: 1e-63, scientific: true }, - y: { name: 'y', value: 1e-72, scientific: true } + y: { name: 'y', value: 1e-72, scientific: true }, + r: { name: 'r', value: 1e-81, scientific: true }, + q: { name: 'q', value: 1e-90, scientific: true } }, BINARY_SHORT_SI: { '': { name: '', value: 1, scientific: true }, diff --git a/test/unit-tests/type/unit/Unit.test.js b/test/unit-tests/type/unit/Unit.test.js index c8f475d1ff..14af3530ea 100644 --- a/test/unit-tests/type/unit/Unit.test.js +++ b/test/unit-tests/type/unit/Unit.test.js @@ -737,8 +737,8 @@ describe('Unit', function () { }) it('should format a unit with a bignumber', function () { - assert.strictEqual(new Unit(math.bignumber(1).plus(1e-24), 'm').format(), '1.000000000000000000000001 m') - assert.strictEqual(new Unit(math.bignumber(1e24).plus(1), 'm').format(), '1.000000000000000000000001 Ym') + assert.strictEqual(new Unit(math.bignumber(1).plus(1e-30), 'm').format(), '1.000000000000000000000000000001 m') + assert.strictEqual(new Unit(math.bignumber(1e30).plus(1), 'm').format(), '1.000000000000000000000000000001 Qm') }) it('should format a unit with a fraction', function () { @@ -994,6 +994,36 @@ describe('Unit', function () { }) }) + describe('metric prefixes adopted by BIPM in 2022: Q(uetta), R(onna), r(onto), and q(uecto)', function () { + it('should accept long prefixes', function () { + assert.strictEqual(new Unit(math.bignumber(1e30), 'meter').format(), '1 quettameter') + assert.strictEqual(new Unit(math.bignumber(1e27), 'meter').format(), '1 ronnameter') + assert.strictEqual(new Unit(math.bignumber(1e-27), 'meter').format(), '1 rontometer') + assert.strictEqual(new Unit(math.bignumber(1e-30), 'meter').format(), '1 quectometer') + }) + + it('should accept short prefixes', function () { + assert.strictEqual(new Unit(math.bignumber(1e30), 'm').format(), '1 Qm') + assert.strictEqual(new Unit(math.bignumber(1e27), 'm').format(), '1 Rm') + assert.strictEqual(new Unit(math.bignumber(1e-27), 'm').format(), '1 rm') + assert.strictEqual(new Unit(math.bignumber(1e-30), 'm').format(), '1 qm') + }) + + it('should create square meter correctly', function () { + assert.strictEqual(new Unit(math.bignumber(1e60), 'm2').format(), '1 Qm2') + assert.strictEqual(new Unit(math.bignumber(1e54), 'm2').format(), '1 Rm2') + assert.strictEqual(new Unit(math.bignumber(1e-54), 'm2').format(), '1 rm2') + assert.strictEqual(new Unit(math.bignumber(1e-60), 'm2').format(), '1 qm2') + }) + + it('should create cubic meter correctly', function () { + assert.strictEqual(new Unit(math.bignumber(1e90), 'm3').format(), '1 Qm3') + assert.strictEqual(new Unit(math.bignumber(1e81), 'm3').format(), '1 Rm3') + assert.strictEqual(new Unit(math.bignumber(1e-81), 'm3').format(), '1 rm3') + assert.strictEqual(new Unit(math.bignumber(1e-90), 'm3').format(), '1 qm3') + }) + }) + describe('_isDerived', function () { it('should return the correct value', function () { assert.strictEqual(Unit.parse('34 kg')._isDerived(), false)