[BUGFIX] Floating point comparison using == instead of epsilon#10
Open
GAURAV-1313 wants to merge 1 commit into
Open
[BUGFIX] Floating point comparison using == instead of epsilon#10GAURAV-1313 wants to merge 1 commit into
GAURAV-1313 wants to merge 1 commit into
Conversation
✅ Deploy Preview for nofriction canceled.
|
Owner
Author
AI Code ReviewSummaryThe 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 FindingsBugs
Recommended FixesGeneral
Suggested Tests
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 AvailableAdd the 1 issue found. Reviewed by RepoSpace AI. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uses == for floating point comparison which fails due to precision errors.