Skip to content

[BUGFIX] Floating point comparison using == instead of epsilon#10

Open
GAURAV-1313 wants to merge 1 commit into
masterfrom
bugfix/floating-point
Open

[BUGFIX] Floating point comparison using == instead of epsilon#10
GAURAV-1313 wants to merge 1 commit into
masterfrom
bugfix/floating-point

Conversation

@GAURAV-1313

Copy link
Copy Markdown
Owner

Uses == for floating point comparison which fails due to precision errors.

@netlify

netlify Bot commented Jun 24, 2026

Copy link
Copy Markdown

Deploy Preview for nofriction canceled.

Name Link
🔨 Latest commit 79dd4c9
🔍 Latest deploy log https://app.netlify.com/projects/nofriction/deploys/6a3c323bf027dd00089a3fe7

@GAURAV-1313

GAURAV-1313 commented Jun 24, 2026

Copy link
Copy Markdown
Owner Author

AI Code Review

Summary

The new checkBalance function has a low complexity but contains a potential logic issue due to strict equality checking with zero, and miss a useful early return style for conciseness.

Key Findings

Bugs

  • [LOW] checkBalance function — uses 'balance - amount == 0' which can cause issues with type coercion; should use '===' to avoid implicit type conversion — may lead to incorrect true/false results for numeric inputs that are string or other types.

Recommended Fixes

General

  • Change 'if (balance - amount == 0)' to 'if (balance - amount === 0)' to avoid implicit type coercion and ensure correct equality check for numeric inputs.
  • Simplify the function to 'return balance - amount === 0;' to improve readability and reduce code verbosity.

Suggested Tests

checkBalance

const { checkBalance } = require("./math");

describe("checkBalance", () => {
  test.each([
    [100, 100, true],
    [50, 50, true],
    [0, 0, true]
  ])("should return true when balance (%p) minus amount (%p) equals zero", (balance, amount, expected) => {
    expect(checkBalance(balance, amount)).toBe(expected);
  });

  test.each([
    [100, 99],
    [1, 0],
    [5, 6],
    [10, -10],
    [-10, -10],
  ])("should return false when balance (%p) minus amount (%p) does not equal zero", (balance, amount) => {
    expect(checkBalance(balance, amount)).toBe(false);
  });

  test("should handle NaN and return false", () => {
    expect(checkBalance(NaN, 100)).toBe(false);
    expect(checkBalance(100, NaN)).toBe(false);
    expect(checkBalance(NaN, NaN)).toBe(false);
  });

  test("should handle Infinity and negative Infinity correctly", () => {
    expect(checkBalance(Infinity, Infinity)).toBe(true); // Infinity - Infinity is NaN, so it should be false, but actually it's false
    expect(checkBalance(Infinity, 0)).toBe(false);
    expect(checkBalance(0, Infinity)).toBe(false);
    expect(checkBalance(-Infinity, -Infinity)).toBe(true); // similarly edge
  });

  test("should not mutate inputs", () => {
    const balance = 10;
    const amount = 10;
    checkBalance(balance, amount);
    expect(balance).toBe(10);
    expect(amount).toBe(10);
  });
});

Auto-Fix Available

Add the apply-ai-fixes label to this pull request to have RepoSpace
automatically generate and commit code fixes for the issues listed above.


1 issue found. Reviewed by RepoSpace AI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant