Skip to content

fix(maths): correct coordinate subtraction in DistanceFormula.euclideanDistance - #7747

Open
spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/euclidean-distance-formula
Open

spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/euclidean-distance-formula

Conversation

@spidervirus

Copy link
Copy Markdown
  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Description

Fixes #7746

In DistanceFormula.euclideanDistance, the variable dY was calculated using Math.pow(y2 - x1, 2) where x1 was incorrectly used instead of y1.

This PR:

  1. Fixes dY to compute Math.pow(y2 - y1, 2).
  2. Updates DistanceFormulaTest.java with the mathematically exact Euclidean distances.
  3. Adds test cases covering zero distance, a 3-4-5 Pythagorean triangle, and negative coordinates.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.34%. Comparing base (b2af1db) to head (1c59f12).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7747   +/-   ##
=========================================
  Coverage     82.33%   82.34%           
- Complexity     8444     8446    +2     
=========================================
  Files           843      843           
  Lines         26154    26154           
  Branches       5109     5109           
=========================================
+ Hits          21535    21537    +2     
  Misses         3830     3830           
+ Partials        789      787    -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@spidervirus
spidervirus marked this pull request as ready for review November 11, 2026 14:41
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.

[BUG] Incorrect coordinate subtraction in DistanceFormula.euclideanDistance

2 participants