smath v1.8.5
Home | Docs | GitHub | npm | Changelog | YouTube | Small math function library
Installation
smath can be installed from the official npm package repository. It is highly recommended to install the latest version, which is installed by default with the following command.
npm i smath@1.8.5
Bugs and Requests
Is there a way we can make smath better? Please report all bugs, issues, and new feature requests to the issues page in the official repository. For critical security issues, please send an email to smath@nicfv.com.
Contribute
Thank you for your interest in contributing to smath! smath is an open source software package maintained by Nicolas Ventura (@nicfv) and built by users like you! You are allowed to fork the repository as permitted by the MIT License terms. Contributions are welcome by submitting a pull request. Please follow the existing code styling if submitting a pull request. Thank you for your consideration!
Getting Started
Small math? Simple math? Or supplemental math? Canonically, "SMath" is pronounced "smath" and stands for "small math (library.)" Similar to JavaScript's builtin Math
object, SMath
exports one global object with several math-related helper functions. There is no need to instantiate the class, just call functions directly. See the examples below to get started using SMath!
Executables
SMath is also packaged with an executabe that can be run directly through npx
in the terminal - even outside of a NodeJS project! In fact, open your terminal now, and type the following to show a list of valid npx smath
commands!
npx smath
Commands are all structured like this.
npx smath [cmd] [args]
This example command returns the value 0.4.
npx smath normalize 4 0 10
Examples
Here are a few quickstart examples written in JavaScript that showcase some out-of-box features of the smath
package.
JavaScript Math Oddities
Sometimes, JavaScript does weird math! Try adding 0.1 + 0.2
in your NodeJS terminal. What did you get?
Hint: It's not 0.3!
The function SMath.approx()
is an attempt to mitigate some of the issues that arise when doing arithmetic with non-whole numbers.
Instructions
- Copy the source code
- Paste into a new file
- Save as
JavaScript-Math-Oddities.mjs
- Run this command in your terminal
node JavaScript-Math-Oddities.mjs
Source
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));
Output
0.1 + 0.2 == 0.3 is false
SMath.approx(0.1 + 0.2, 0.3) is true
Temperature Conversion
This example demonstrates a simple temperature converter from Celsius to Fahrenheit, using SMath.translate()
to linearly interpolate between unit systems. The translation uses freezing and boiling points to fix the bounds of the linear interpolation.
Instructions
- Copy the source code
- Paste into a new file
- Save as
Temperature-Conversion.mjs
- Run this command in your terminal
node Temperature-Conversion.mjs
Source
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')
}
Output
0C is 32F
5C is 41F
10C is 50F
15C is 59F
20C is 68F
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
9 years ago