Skip to content

Instantly share code, notes, and snippets.

@dyoo
Last active December 29, 2015 03:29
Show Gist options
  • Save dyoo/7608538 to your computer and use it in GitHub Desktop.
Save dyoo/7608538 to your computer and use it in GitHub Desktop.
// The curse of a programmer; if you read articles with data,
// you continuously fight the temptation to write a program to
// play with that data. In this case, I failed.
package main
import (
"fmt"
"math"
)
// Information from:
//
// http://www.forbes.com/sites/moneybuilder/2013/01/05/
// updated-2013-federal-income-tax-brackets-and-marginal-rates/
//
// Percent margins
// --------- -----------------------------
// 10% [0, 8925]
// 15% [8925, 36250]
// 25% [36250, 87850]
// 28% [87850, 183250]
// 33% [183250, 398350]
// 35% [398350, 400000]
// 39.6% [400000, inf.0]
//
// I want a program that takes my income and presents the marginal
// taxes on that income.
// Let's first represent the table above as a data structure. Let's
// call it a tax bracket.
type TaxBracket struct {
tax_percent float64
margin_start float64
margin_end float64
}
// Given an income, we can compute the tax for that bracket.
func (t TaxBracket) GetTax(income float64) float64 {
if t.margin_start <= income {
return t.tax_percent * (math.Min(income, t.margin_end) - t.margin_start) / 100.0
}
return 0
}
// With the data definition out of the way, we can encode the table
// as a slice of these brackets:
var TAX_BRACKETS_2013 = []TaxBracket{
{10, 0, 8925},
{15, 8925, 36250},
{25, 36250, 87850},
{28, 87850, 183250},
{33, 183250, 398350},
{35, 398350, 400000},
{39.6, 400000, math.Inf(1)}}
// Now we can write a function to compute, using that table. It's just a matter
// of asking each tax bracket to get the appropriate taxes, and accumulate the
// total:
func ComputeMarginalTaxes(income float64) float64 {
total := 0.0
for _, tax_bracket := range TAX_BRACKETS_2013 {
total += tax_bracket.GetTax(income)
}
return total
}
func main() {
// Let's use this to check the tax rate at $100000. According to
// the article, we'd owe $21293. Let's see that we do.
fmt.Println(ComputeMarginalTaxes(100000))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment