Skip to content

Commit

Permalink
Support new metric prefixes: Q, R, r, and q (#3113)
Browse files Browse the repository at this point in the history
* added Q, R, r, q metrix prefixes

* tests added for new prefixes

* removed duplicate tests

* maybe square and cubic tests will bump code cov into the positive

* Check numeric value

---------

Co-authored-by: Jos de Jong <[email protected]>
  • Loading branch information
AlexEdgcomb and josdejong authored Jan 10, 2024
1 parent a1192de commit af55b12
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
8 changes: 6 additions & 2 deletions docs/datatypes/units.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ 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
```

If you cannot express the new unit in terms of any existing unit, then the second argument can be omitted. In this case, a new *base unit* is created:

```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
```

Expand Down Expand Up @@ -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
------ | ------------- | -----
Expand All @@ -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`).
Expand Down
24 changes: 20 additions & 4 deletions src/type/unit/Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand Down
34 changes: 32 additions & 2 deletions test/unit-tests/type/unit/Unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit af55b12

Please sign in to comment.