Skip to content

Commit

Permalink
Add rounding function (#15)
Browse files Browse the repository at this point in the history
* Minor doc updates

* Add contribution guidelines in readme

* Update typedoc

* Release v1.0.1

* Add rounding function

* n cannot be infinity

* Normalize bug

* Changelog for v1.0.2

* Release v1.0.2
  • Loading branch information
nicfv authored Mar 5, 2024
1 parent 55fcc59 commit dbab2ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
6 changes: 6 additions & 0 deletions smath/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.0.2

- Add rounding function
- `isNumber` cannot be +/- infinity
- Fix normalization bug when `min = max`

## 1.0.1

- Fix minor documentation in `SMath`
Expand Down
2 changes: 1 addition & 1 deletion smath/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smath",
"version": "1.0.1",
"version": "1.0.2",
"description": "Small math function library",
"main": "dist/index.js",
"types": "types/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion smath/src/Polate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export abstract class Polate {
*/
public static normalize(n: number, min: number, max: number): number {
if (min === max) {
return min;
return 0;
}
return (n - min) / (max - min);
}
Expand Down
15 changes: 14 additions & 1 deletion smath/src/SMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export abstract class SMath {
* ```
*/
public static isNumber(n: any): boolean {
return typeof n === 'number';
return typeof n === 'number' && n !== Infinity && n !== -Infinity;
}
/**
* Clamp a number within a range.
Expand Down Expand Up @@ -51,4 +51,17 @@ export abstract class SMath {
public static approx(a: number, b: number, epsilon: number = 1e-6): boolean {
return a - b < epsilon && b - a < epsilon;
}
/**
* Round a number to any number of decimal places.
* @param n The number to round
* @param d The number of places on the right side of the decimal
* @returns The rounded number
* @example
* ```js
* const pi = SMath.round(3.1416, 2); // 3.14
* ```
*/
public static round(n: number, d: number): number {
return Math.round(n * 10 ** d) / (10 ** d);
}
}

0 comments on commit dbab2ec

Please sign in to comment.