Skip to content

Commit 1984693

Browse files
committed
Address comments
1 parent 2481dba commit 1984693

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

lib/utils.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,21 @@ function detectUbuntuCodename() {
4141
* The comparison uses the formula:
4242
* abs(a - b) <= max(rtol * max(abs(a), abs(b)), atol)
4343
*
44+
* Implementation checks:
45+
* 1. Absolute tolerance: abs(a - b) <= atol
46+
* 2. Relative tolerance: abs(a - b) / max(abs(a), abs(b)) <= rtol
47+
*
4448
* @param {number} a - The first number to compare
4549
* @param {number} b - The second number to compare
4650
* @param {number} [rtol=1e-9] - The relative tolerance parameter (default: 1e-9)
4751
* @param {number} [atol=0.0] - The absolute tolerance parameter (default: 0.0)
4852
* @returns {boolean} True if the numbers are close within the tolerance
4953
*
5054
* @example
51-
* isClose(1.0, 1.0) // true
52-
* isClose(1.0, 1.1, 0.01) // false - difference 0.1 > 0.01 * max(1.0, 1.1) = 0.011
53-
* isClose(10, 10.00001, 1e-6) // true - difference 0.00001 <= 1e-6 * 10 = 0.00001
55+
* isClose(1.0, 1.0) // true - exact equality
56+
* isClose(1.0, 1.1, 0.01) // false - relative diff: 0.1/1.1 ≈ 0.091 > 0.01
57+
* isClose(10, 10.00001, 1e-6) // true - relative diff: 0.00001/10 = 1e-6 <= 1e-6
58+
* isClose(0, 0.05, 0, 0.1) // true - absolute diff: 0.05 <= 0.1 (atol)
5459
*/
5560
function isClose(a, b, rtol = 1e-9, atol = 0.0) {
5661
// Handle exact equality
@@ -72,6 +77,12 @@ function isClose(a, b, rtol = 1e-9, atol = 0.0) {
7277

7378
// Check relative tolerance
7479
const relativeScaler = Math.max(Math.abs(a), Math.abs(b));
80+
81+
// Handle division by zero when both values are zero or very close to zero
82+
if (relativeScaler === 0) {
83+
return true; // Both are zero, already handled by absolute tolerance
84+
}
85+
7586
const relativeDiff = absDiff / relativeScaler;
7687

7788
return rtol >= relativeDiff;

0 commit comments

Comments
 (0)