1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import * as math from './math.js'
|
8 |
|
9 | export const yotta = 1e24
|
10 | export const zetta = 1e21
|
11 | export const exa = 1e18
|
12 | export const peta = 1e15
|
13 | export const tera = 1e12
|
14 | export const giga = 1e9
|
15 | export const mega = 1e6
|
16 | export const kilo = 1e3
|
17 | export const hecto = 1e2
|
18 | export const deca = 10
|
19 | export const deci = 0.1
|
20 | export const centi = 0.01
|
21 | export const milli = 1e-3
|
22 | export const micro = 1e-6
|
23 | export const nano = 1e-9
|
24 | export const pico = 1e-12
|
25 | export const femto = 1e-15
|
26 | export const atto = 1e-18
|
27 | export const zepto = 1e-21
|
28 | export const yocto = 1e-24
|
29 |
|
30 | const prefixUp = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
|
31 | const prefixDown = ['', 'm', 'μ', 'n', 'p', 'f', 'a', 'z', 'y']
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export const prefix = (n, baseMultiplier = 0) => {
|
41 | const nPow = n === 0 ? 0 : math.log10(n)
|
42 | let mult = 0
|
43 | while (nPow < mult * 3 && baseMultiplier > -8) {
|
44 | baseMultiplier--
|
45 | mult--
|
46 | }
|
47 | while (nPow >= 3 + mult * 3 && baseMultiplier < 8) {
|
48 | baseMultiplier++
|
49 | mult++
|
50 | }
|
51 | const prefix = baseMultiplier < 0 ? prefixDown[-baseMultiplier] : prefixUp[baseMultiplier]
|
52 | return {
|
53 | n: math.round((mult > 0 ? n / math.exp10(mult * 3) : n * math.exp10(mult * -3)) * 1e12) / 1e12,
|
54 | prefix
|
55 | }
|
56 | }
|