Skip to content

my-LinkedIn/reverse-payroll

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Reverse Payroll Solver (Newton + Brent)

A numerical engine that computes gross salary from a target net salary using a hybrid root-finding approach.


Overview

This project solves:


net_salary(gross) = target_net

Rewritten as:


f(gross) = net_salary(gross) - target_net = 0

It combines:

  • Newton–Raphson (fast convergence)
  • Brent’s Method (guaranteed convergence)

Features

  • Hybrid solver (Newton + Brent)
  • Adaptive bracketing
  • Progressive tax model support
  • Contribution caps
  • High numerical precision
  • Robust handling of non-linear, piecewise functions

Payroll Model

Income Tax

Progressive brackets:


(0.0,    0.0)
(1000.0, 0.10)
(2000.0, 0.20)
(4000.0, 0.30)

Tax is computed cumulatively per bracket.


Social Contribution


min(gross * SOCIAL_RATE, SOCIAL_CAP)

Constants:


SOCIAL_RATE = 0.08
SOCIAL_CAP  = 500.0


Net Salary


net = gross - income_tax(gross) - social_contribution(gross)


Solver Architecture

1. Adaptive Bracketing

Finds an interval [low, high] such that:


f(low) * f(high) < 0

Expands high exponentially if needed.


2. Newton Pre-pass

Iteration:


g_next = g - f(g) / f'(g)

Derivative approximated via central difference:


f'(x) ≈ (f(x + h) - f(x - h)) / (2h)

Used as a fast initial convergence attempt.


3. Brent Solver

Fallback solver combining:

  • Bisection
  • Secant method
  • Inverse quadratic interpolation

Guarantees convergence when a root is bracketed.


Solver Flow


Target Net
↓
Bracketing
↓
Newton (fast path)
↓ success
Return result

↓ failure
Brent solver
↓
Return result


Convergence Criteria

  • Newton tolerance: 1e-6
  • Brent tolerance: 1e-6
  • Maximum iterations: configurable (default 100)

Output Types


SolveResult:

* Converged(f64)
* NoBracket
* MaxIterations


Example


Target Net: 2500
Estimated Gross: 3055.56
Verified Net: 2499.999999999954


Complexity

  • Newton: O(log n) near solution
  • Brent: O(log n) guaranteed convergence
  • Hybrid: typically < 20 iterations total

Use Cases

  • Payroll systems
  • Tax simulation
  • Financial modeling
  • Inverse pricing problems
  • Embedded numerical computation

Notes

  • Works with non-linear and piecewise functions
  • Handles discontinuities in derivatives
  • Designed for numerical stability
  • No analytical inversion required

Releases

No releases published

Packages

 
 
 

Contributors

Languages