Skip to content

Commit

Permalink
Merge branch 'develop' into unitless-quantity-conversion-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong authored Jan 10, 2024
2 parents cda62c6 + af55b12 commit 6607151
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ Math.js is an extensive math library for JavaScript and Node.js. It features a f
[![Version](https://img.shields.io/npm/v/mathjs.svg)](https://www.npmjs.com/package/mathjs)
[![Downloads](https://img.shields.io/npm/dm/mathjs.svg)](https://www.npmjs.com/package/mathjs)
[![Build Status](https://github.com/josdejong/mathjs/workflows/Node.js%20CI/badge.svg)](https://github.com/josdejong/mathjs/actions)
[![Maintenance](https://img.shields.io/maintenance/yes/2023.svg)](https://github.com/josdejong/mathjs/graphs/commit-activity)
[![Maintenance](https://img.shields.io/maintenance/yes/2024.svg)](https://github.com/josdejong/mathjs/graphs/commit-activity)
[![License](https://img.shields.io/github/license/josdejong/mathjs.svg)](https://github.com/josdejong/mathjs/blob/master/LICENSE)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fjosdejong%2Fmathjs.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fjosdejong%2Fmathjs?ref=badge_shield)
[![Codecov](https://codecov.io/gh/josdejong/mathjs/branch/develop/graph/badge.svg)](https://codecov.io/gh/josdejong/mathjs)
[![Github Sponsor](https://camo.githubusercontent.com/7d9333b097b2f54a8957d126ab82937811489c9b75c3850f609985cf94cd29fe/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2532302d53706f6e736f722532306d652532306f6e2532304769744875622d6f72616e6765)](https://github.com/sponsors/josdejong)
[![Github Sponsor](https://img.shields.io/github/sponsors/josdejong
)](https://github.com/sponsors/josdejong)

## Features

Expand Down
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 @@ -1276,6 +1276,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 @@ -1286,7 +1288,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 @@ -1301,6 +1305,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 @@ -1311,7 +1317,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 @@ -1326,6 +1334,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 @@ -1336,7 +1346,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 @@ -1351,6 +1363,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 @@ -1361,7 +1375,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 @@ -738,8 +738,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 @@ -995,6 +995,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 6607151

Please sign in to comment.