Skip to content

Add bisection method for root finding - #1074

Open
susbakun wants to merge 3 commits into
TheAlgorithms:masterfrom
susbakun:add-bisection
Open

susbakun wants to merge 3 commits into
TheAlgorithms:masterfrom
susbakun:add-bisection

Conversation

@susbakun

@susbakun susbakun commented Oct 14, 2026 •

Copy link
Copy Markdown

Description

Adds the bisection method for finding a root of a continuous function
inside a bracketing interval [lower, upper] (where f(lower) and
f(upper) have opposite signs).

  • Returns Option<f64>: None for invalid input (non-finite bounds,
    non-positive or non-finite tolerance, NaN function values, no sign
    change) or if the interval doesn't shrink below the tolerance within
    max_iterations
  • Accepts the bounds in either order
  • Accepts closures (F: Fn(f64) -> f64)
  • Computes the midpoint without overflowing for very large bounds
  • Includes unit tests

Reference: https://en.wikipedia.org/wiki/Bisection_method

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

Please make sure that if there is a test that takes too long to run ( > 300ms), you #[ignore] that or
try to optimize your code or make the test easier to run. We have this rule because we have hundreds of
tests to run; If each one of them took 300ms, we would have to wait for a long time.

@susbakun
susbakun requested a review from imp2002 as a code owner October 14, 2026 14:05
@codecov-commenter

codecov-commenter commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.91%. Comparing base (fed66cb) to head (8c4aff8).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1074      +/-   ##
==========================================
+ Coverage   95.89%   95.91%   +0.01%     
==========================================
  Files         396      397       +1     
  Lines       30441    30562     +121     
==========================================
+ Hits        29192    29313     +121     
  Misses       1249     1249              

☔ 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.

@susbakun

susbakun commented Oct 14, 2026 •

Copy link
Copy Markdown
Author

Added a test for the case where the midpoint is an exact root, covering the previously uncovered branch.

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.

2 participants