Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

smath

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smath

Small math function library

  • 1.6.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
168
increased by281.82%
Maintainers
1
Weekly downloads
 
Created
Source

Home | Docs | GitHub | npm | Changelog | YouTube | Small math function library

NPM Downloads NPM Version Relative date GitHub watchers GitHub forks GitHub Repo stars

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.6.1

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
  1. Copy the source code
  2. Paste into a new file
  3. Save as JavaScript-Math-Oddities.mjs
  4. 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
  1. Copy the source code
  2. Paste into a new file
  3. Save as Temperature-Conversion.mjs
  4. 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

Keywords

FAQs

Package last updated on 27 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc