units.js (3584B)
1 // units 2 3 // load math.js (using node.js) 4 const math = require('..') 5 6 // units can be created by providing a value and unit name, or by providing 7 // a string with a valued unit. 8 console.log('create units') 9 const a = math.unit(45, 'cm') 10 const b = math.unit('0.1m') 11 print(a) // 45 cm 12 print(b) // 0.1 m 13 console.log() 14 15 // units can be added, subtracted, and multiplied or divided by numbers and by other units 16 console.log('perform operations') 17 print(math.add(a, b)) // 55 cm 18 print(math.multiply(b, 2)) // 0.2 m 19 print(math.divide(math.unit('1 m'), math.unit('1 s'))) // 1 m / s 20 print(math.pow(math.unit('12 in'), 3)) // 1728 in^3 21 console.log() 22 23 // units can be converted to a specific type, or to a number 24 console.log('convert to another type or to a number') 25 print(b.to('cm')) // 10 cm Alternatively: math.to(b, 'cm') 26 print(math.to(b, 'inch')) // 3.9370078740157 inch 27 print(b.toNumber('cm')) // 10 28 print(math.number(b, 'cm')) // 10 29 console.log() 30 31 // the expression parser supports units too 32 console.log('parse expressions') 33 print(math.evaluate('2 inch to cm')) // 5.08 cm 34 print(math.evaluate('cos(45 deg)')) // 0.70710678118655 35 print(math.evaluate('90 km/h to m/s')) // 25 m / s 36 console.log() 37 38 // convert a unit to a number 39 // A second parameter with the unit for the exported number must be provided 40 print(math.evaluate('number(5 cm, mm)')) // number, 50 41 console.log() 42 43 // simplify units 44 console.log('simplify units') 45 print(math.evaluate('100000 N / m^2')) // 100 kPa 46 print(math.evaluate('9.81 m/s^2 * 100 kg * 40 m')) // 39.24 kJ 47 console.log() 48 49 // example engineering calculations 50 console.log('compute molar volume of ideal gas at 65 Fahrenheit, 14.7 psi in L/mol') 51 const Rg = math.unit('8.314 N m / (mol K)') 52 const T = math.unit('65 degF') 53 const P = math.unit('14.7 psi') 54 const v = math.divide(math.multiply(Rg, T), P) 55 console.log('gas constant (Rg) = ', format(Rg)) 56 console.log('P = ' + format(P)) 57 console.log('T = ' + format(T)) 58 console.log('v = Rg * T / P = ' + format(math.to(v, 'L/mol'))) 59 // 23.910432393453 L / mol 60 console.log() 61 62 console.log('compute speed of fluid flowing out of hole in a container') 63 const g = math.unit('9.81 m / s^2') 64 const h = math.unit('1 m') 65 const v2 = math.pow(math.multiply(2, math.multiply(g, h)), 0.5) // Can also use math.sqrt 66 console.log('g = ' + format(g)) 67 console.log('h = ' + format(h)) 68 console.log('v = (2 g h) ^ 0.5 = ' + format(v2)) 69 // 4.42944691807 m / s 70 console.log() 71 72 console.log('electrical power consumption:') 73 const expr1 = '460 V * 20 A * 30 days to kWh' 74 console.log(expr1 + ' = ' + math.evaluate(expr1)) // 6624 kWh 75 console.log() 76 77 console.log('circuit design:') 78 const expr2 = '24 V / (6 mA)' 79 console.log(expr2 + ' = ' + math.evaluate(expr2)) // 4 kohm 80 console.log() 81 82 console.log('operations on arrays:') 83 const B = math.evaluate('[1, 0, 0] T') 84 const v3 = math.evaluate('[0, 1, 0] m/s') 85 const q = math.evaluate('1 C') 86 const F = math.multiply(q, math.cross(v3, B)) 87 console.log('B (magnetic field strength) = ' + format(B)) // [1 T, 0 T, 0 T] 88 console.log('v (particle velocity) = ' + format(v3)) // [0 m / s, 1 m / s, 0 m / s] 89 console.log('q (particle charge) = ' + format(q)) // 1 C 90 console.log('F (force) = q (v cross B) = ' + format(F)) // [0 N, 0 N, -1 N] 91 92 /** 93 * Helper function to output a value in the console. Value will be formatted. 94 * @param {*} value 95 */ 96 function print (value) { 97 console.log(format(value)) 98 } 99 100 /** 101 * Helper function to format an output a value. 102 * @param {*} value 103 * @return {string} Returns the formatted value 104 */ 105 function format (value) { 106 const precision = 14 107 return math.format(value, precision) 108 }