|
1 | 1 | # HyperMath |
2 | 2 |
|
3 | | -HyperMath is a simple JavaScript/TypeScript library designed to address common pitfalls in JavaScript math operations. It provides a set of static methods for basic arithmetic operations with improved precision and input handling. |
| 3 | +HyperMath is a simple JavaScript/TypeScript library designed to address common pitfalls in JavaScript math operations. It provides a set of static methods for basic arithmetic operations with improved precision and robust error handling. |
4 | 4 |
|
5 | 5 |  |
6 | 6 | [](https://npmjs.com/package/hypermath) |
@@ -29,37 +29,130 @@ console.log(HyperMath.divide(0.3, 0.1)); // 3 (instead of 2.9999999999999996) |
29 | 29 |
|
30 | 30 | // Subtraction |
31 | 31 | console.log(HyperMath.subtract(0.3, 0.1)); // 0.2 (instead of 0.19999999999999998) |
| 32 | + |
| 33 | +// Format a number |
| 34 | +console.log(HyperMath.formatNumber(1.12345)); // 1.12 |
| 35 | + |
| 36 | +// String inputs are supported |
| 37 | +console.log(HyperMath.multiply('10.5', '2.5')); // 26.25 |
| 38 | +``` |
| 39 | + |
| 40 | +## Error Handling |
| 41 | + |
| 42 | +The library now throws `HyperMathError` for invalid inputs: |
| 43 | + |
| 44 | +```typescript |
| 45 | +import { HyperMath, HyperMathError } from 'hypermath'; |
| 46 | + |
| 47 | +try { |
| 48 | + HyperMath.add(null, 5); // Throws HyperMathError |
| 49 | +} catch (error) { |
| 50 | + if (error instanceof HyperMathError) { |
| 51 | + console.error(error.message); // "Invalid input provided: null" |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Invalid string inputs also throw errors |
| 56 | +HyperMath.multiply('abc', '123'); // Throws HyperMathError: "Invalid input provided: abc" |
| 57 | + |
| 58 | +// Division by zero throws an error |
| 59 | +HyperMath.divide(10, 0); // Throws HyperMathError: "Division by zero is not allowed" |
32 | 60 | ``` |
33 | 61 |
|
34 | 62 | ## Features |
35 | 63 |
|
36 | | -- Handles both number and string inputs |
37 | | -- Attempts to maintain precision up to 2 significant digits |
38 | | -- Provides static methods for multiplication, addition, and division |
39 | | -- Throws an error for invalid string inputs |
| 64 | +- **Precision handling**: Maintains precision up to 2 decimal places using `.toFixed(2)` |
| 65 | +- **Flexible inputs**: Handles both number and string inputs seamlessly |
| 66 | +- **Robust error handling**: Throws descriptive `HyperMathError` exceptions for invalid inputs |
| 67 | +- **Edge case protection**: Proper handling of division by zero and invalid operations |
| 68 | +- **TypeScript support**: Full TypeScript definitions included |
| 69 | +- **Zero dependencies**: Lightweight with no external dependencies |
| 70 | +- **Comprehensive tests**: 52+ unit tests ensuring reliability |
40 | 71 |
|
41 | 72 | ## API |
42 | 73 |
|
43 | 74 | ### `HyperMath.multiply(firstValue: number | string, secondValue: number | string): number` |
44 | 75 |
|
45 | 76 | Multiplies two numbers with improved precision. |
46 | 77 |
|
| 78 | +**Parameters:** |
| 79 | +- `firstValue`: Number or string to multiply |
| 80 | +- `secondValue`: Number or string to multiply |
| 81 | + |
| 82 | +**Returns:** The product rounded to 2 decimal places |
| 83 | + |
| 84 | +**Throws:** `HyperMathError` if inputs are invalid (null, undefined, or non-numeric strings) |
| 85 | + |
| 86 | +--- |
| 87 | + |
47 | 88 | ### `HyperMath.add(firstValue: number | string, secondValue: number | string): number` |
48 | 89 |
|
49 | 90 | Adds two numbers with improved precision. |
50 | 91 |
|
| 92 | +**Parameters:** |
| 93 | +- `firstValue`: Number or string to add |
| 94 | +- `secondValue`: Number or string to add |
| 95 | + |
| 96 | +**Returns:** The sum rounded to 2 decimal places |
| 97 | + |
| 98 | +**Throws:** `HyperMathError` if inputs are invalid |
| 99 | + |
| 100 | +--- |
| 101 | + |
51 | 102 | ### `HyperMath.divide(firstValue: number | string, secondValue: number | string): number` |
52 | 103 |
|
53 | 104 | Divides two numbers with improved precision. |
54 | 105 |
|
| 106 | +**Parameters:** |
| 107 | +- `firstValue`: Dividend (number or string) |
| 108 | +- `secondValue`: Divisor (number or string) |
| 109 | + |
| 110 | +**Returns:** The quotient rounded to 2 decimal places |
| 111 | + |
| 112 | +**Throws:** |
| 113 | +- `HyperMathError` if inputs are invalid |
| 114 | +- `HyperMathError` with message "Division by zero is not allowed" if divisor is 0 |
| 115 | + |
| 116 | +--- |
| 117 | + |
55 | 118 | ### `HyperMath.subtract(firstValue: number | string, secondValue: number | string): number` |
56 | 119 |
|
57 | 120 | Subtracts two numbers with improved precision. |
58 | 121 |
|
| 122 | +**Parameters:** |
| 123 | +- `firstValue`: Number or string (minuend) |
| 124 | +- `secondValue`: Number or string to subtract (subtrahend) |
| 125 | + |
| 126 | +**Returns:** The difference rounded to 2 decimal places |
| 127 | + |
| 128 | +**Throws:** `HyperMathError` if inputs are invalid |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +### `HyperMath.formatNumber(number: number | string): number` |
| 133 | + |
| 134 | +Formats a number to maintain precision up to 2 decimal places. |
| 135 | + |
| 136 | +**Parameters:** |
| 137 | +- `number`: Number or string to format |
| 138 | + |
| 139 | +**Returns:** The formatted number rounded to 2 decimal places |
| 140 | + |
| 141 | +**Throws:** `HyperMathError` if input is invalid |
| 142 | + |
59 | 143 | ## Notes |
60 | 144 |
|
61 | | -- This library uses `toPrecision(2)` internally, which may lead to rounding in some cases. Be aware of this limitation when using the library for calculations requiring high precision. |
62 | | -- Input strings are parsed to numbers. Invalid string inputs will throw an error. |
| 145 | +- **Precision**: This library uses `.toFixed(2)` internally to maintain precision up to 2 decimal places. This may lead to rounding in some cases. Be aware of this limitation when using the library for calculations requiring higher precision. |
| 146 | +- **Error handling**: The library now throws `HyperMathError` exceptions instead of returning 0 for invalid inputs. Make sure to handle these errors appropriately in your code. |
| 147 | +- **Edge cases**: |
| 148 | + - **Division by zero**: Throws `HyperMathError` instead of returning 0 |
| 149 | + - **Invalid inputs**: Any null, undefined, or non-numeric string values will throw `HyperMathError` |
| 150 | + - **String parsing**: String inputs are parsed using `parseFloat()`, so partial numeric strings like "123abc" will parse as 123 |
| 151 | +- **Type safety**: TypeScript users benefit from full type definitions and the custom `HyperMathError` class for better error handling |
| 152 | + |
| 153 | +## Support |
| 154 | + |
| 155 | +If you encounter any issues or have questions, please open an issue on the [GitHub repository](https://github.com/hyperteksolutions/hypermath/issues). |
63 | 156 |
|
64 | 157 | ## Contributing |
65 | 158 |
|
|
0 commit comments