-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
197 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ dist | |
types | ||
docs | ||
*.tgz | ||
*/examples/*.js | ||
*.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { SMath } from 'smath'; | ||
|
||
// Determine the value of 0.1 + 0.2 using vanilla JavaScript and SMath | ||
console.log('0.1 + 0.2 == 0.3 is ' + (0.1 + 0.2 == 0.3)); | ||
console.log('SMath.approx(0.1 + 0.2, 0.3) is ' + SMath.approx(0.1 + 0.2, 0.3)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"type": "module", | ||
"scripts": { | ||
"start": "rm -f *.out && touch success && find *.js -exec bash -c \"node {} > {}.out || rm success\" \\; && rm success", | ||
"clean": "rm -rf node_modules package-lock.json *.out" | ||
}, | ||
"dependencies": { | ||
"smath": "file:smath-1.1.8.tgz" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { SMath } from 'smath'; | ||
|
||
// Water always freezes at the | ||
// same temperature, but the | ||
// units might be different. | ||
// Define some constants to | ||
// create two number ranges. | ||
const C_Freeze = 0, | ||
C_Boil = 100, | ||
F_Freeze = 32, | ||
F_Boil = 212; | ||
|
||
// Use the `SMath` class to | ||
// generate an array of five | ||
// linearly spaced temperature | ||
// values from 0 to 20. | ||
const C = SMath.linspace(0, 20, 5); | ||
|
||
// Use the `SMath` class to linearly | ||
// interpolate the temperature in the | ||
// C number range to a temperature | ||
// in the F number range. | ||
const F = C.map(c => SMath.translate(c, C_Freeze, C_Boil, F_Freeze, F_Boil)); | ||
|
||
// Print out each temperature | ||
// in both units of C and F. | ||
for (let i = 0; i < C.length; i++) { | ||
console.log(C[i].toFixed() + 'C is ' + F[i].toFixed() + 'F') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env node | ||
|
||
import { SMath } from '.'; | ||
|
||
const args: Array<string> = process.argv.slice(2); | ||
|
||
/** | ||
* Try to convert an argument into a numeric value. | ||
*/ | ||
function N(index: number, defaultVal: number = NaN): number { | ||
if (index >= args.length) { | ||
if (Number.isFinite(defaultVal)) { | ||
return defaultVal; | ||
} else { | ||
console.error('Required argument ' + index + ' is missing!'); | ||
process.exit(1); | ||
} | ||
} | ||
const arg: number = Number.parseFloat(args[index]); | ||
if (Number.isFinite(arg)) { | ||
return arg; | ||
} else if (Number.isFinite(defaultVal)) { | ||
return defaultVal; | ||
} else { | ||
console.error('Argument #' + index + ' is ' + arg + ' but a number was expected.'); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
if (args.length < 1 || args[0].includes('help')) { | ||
console.log('Key: <required> [optional]'); | ||
console.log('Arguments:'); | ||
console.log(' help : Show this page'); | ||
console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal'); | ||
console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers'); | ||
console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`'); | ||
console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`'); | ||
console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`'); | ||
console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`'); | ||
console.log(' normalize <n> <min> <max>'); | ||
console.log(' : Normalize `n` between `min` and `max`'); | ||
console.log(' translate <n> <min1> <max1> <min2> <max2>'); | ||
console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`'); | ||
process.exit(1); | ||
} | ||
|
||
switch (args[0]) { | ||
case ('approx'): { | ||
console.log(SMath.approx(N(1), N(2), N(3, 1e-6))); | ||
break; | ||
} | ||
case ('avg'): { | ||
if (args.length < 2) { | ||
console.error('Need at least 1 argument.'); | ||
process.exit(1); | ||
} | ||
const operands: Array<number> = []; | ||
for (let i = 1; i < args.length; i++) { | ||
operands.push(N(i)); | ||
} | ||
console.log(SMath.avg(...operands)); | ||
break; | ||
} | ||
case ('clamp'): { | ||
console.log(SMath.clamp(N(1), N(2), N(3))); | ||
break; | ||
} | ||
case ('expand'): { | ||
console.log(SMath.expand(N(1), N(2), N(3))); | ||
break; | ||
} | ||
case ('linspace'): { | ||
console.log(SMath.linspace(N(1), N(2), N(3))); | ||
break; | ||
} | ||
case ('logspace'): { | ||
console.log(SMath.logspace(N(1), N(2), N(3))); | ||
break; | ||
} | ||
case ('normalize'): { | ||
console.log(SMath.normalize(N(1), N(2), N(3))); | ||
break; | ||
} | ||
case ('translate'): { | ||
console.log(SMath.translate(N(1), N(2), N(3), N(4), N(5))); | ||
break; | ||
} | ||
default: { | ||
console.error('Unknown argument "' + args[0] + '". Use with "help" for a list of commands.'); | ||
process.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
} | ||
}, | ||
"include": [ | ||
"src", | ||
], | ||
} |