Precise number calculation function that solves JavaScript floating-point precision issues, supports chaining.
| Argument |
Description |
Type |
ComputeNumberResult |
Calculation result object |
{ result: number, next: Function } |
| Parameter |
Description |
Type |
Default |
a |
First number |
number |
Required |
type |
Operation type (+, -, *, /) |
string |
Required |
b |
Second number |
number |
Required |
| Property |
Description |
Type |
result |
Calculation result |
number |
next |
Function to continue calculation |
Function |
import { mathjs } from 'ranuts';
const result = mathjs(0.1, '+', 0.2);
console.log(result.result); // 0.3 (precise result, not 0.30000000000000004)
import { mathjs } from 'ranuts';
const result = mathjs(1.3, '-', 1.2).next('+', 1.5).next('*', 2.3).next('/', 0.2);
console.log(result.result); // Precise calculation result
import { mathjs } from 'ranuts';
// Native JavaScript calculation has precision issues
console.log(0.1 + 0.2); // 0.30000000000000004
// Using mathjs gets precise result
const result = mathjs(0.1, '+', 0.2);
console.log(result.result); // 0.3
import { mathjs } from 'ranuts';
const total = mathjs(100, '*', 0.1).next('+', 50).next('-', 20).next('/', 2);
console.log(total.result); // Precise calculation result
- Precision handling: Automatically handles floating-point precision issues, avoiding
0.1 + 0.2 !== 0.3 problems.
- Chaining: Supports chained calculations through the
next method.
- Operation types: Supports four operations:
+ (add), - (subtract), * (multiply), / (divide).
- Performance: Slightly slower than native operations, but guarantees precision.