Jump to content

Tough spec question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
wheel

wheel

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

I'm doing some calculator specs for our developer and having a tough time documenting this sanely.

Basically I need to solve for a variable, where one side of the equation contains both the variable and a function dependent on that variable. And the function doesn't resolve into something nice that I can plunk into the equation.

Here's the basic idea. Net income is gross income minus taxes. I have net income, want to solve for gross. Taxes are the function based on gross (and are a funky calc).

So basic equation to be solved for gross income is:
Gross - Taxes(Gross) = Net

The taxes function look something like this:
First $10,000 of gross, taxes are 0.
From $10,000 to $90,000 of gross, taxes are 20% of that amount above 10K.
From 90,000 and above of gross, taxes are 40% of the amount above 90K.

Which leaves me with a bunch of IF statements dependent on gross to calculate the taxes. And IF statements don't leave me with some nice equation I can reverse engineer and plunk into the equation to solve for Gross. At least, I don't think so.

My head tells me just to do some sort of iterative calculation, targetting and testing numbers until I get the right gross number. But I have this nagging feeling like I've done a problem like this in years past and had a somewhat elegant solution.

Thanks in advance for any kicks in the general direction.

#2
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts
In PHP there is something known as a switch that eliminates what I like to call "if complexes". I used to use big blocks of if statements until John taught me how to use the switch.

An example is
$bool = true;
switch($bool){
case true:
//do something here
break;
case false:
//do false here
break;
}

I'm sure other languages have similar functions.

I have no other suggestions on how to cut runtime down. Hopefully someone else does.

#3
kalam88

kalam88

    Newbie

  • Members
  • Pip
  • 3 posts
Which leaves me with a bunch of IF statements dependent on gross to calculate the taxes. And IF statements don't leave me with some nice equation I can reverse engineer and plunk into the equation to solve for Gross. At least, I don't think so.
seriöse Singlebörse best wrinkle creams

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
First issue:
Determine the boundary values for resulting Net, that will change what you are looking for to an if-based solver:

First $10,000 of gross, taxes are 0.
From $10,000 to $90,000 of gross, taxes are 20% of that amount above 10K.
From 90,000 and above of gross, taxes are 40% of the amount above 90K.

if G<=10000 -> N = G
if 10000<G<=90000 -> N=10000 + (G-10000)*(.8)
if 90000<G -> N = 10000 + 64000 + (G-90000)*(.6)

So:
if N<= 10000 -> G = N
if 10000 < N <= 74000 -> G = 10000 + (N-10000)/(.8)
if 74000 < N -> G = 90000 + (N-74000)/(.6)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
butidonot

butidonot

    Newbie

  • Members
  • PipPip
  • 14 posts
Which leaves me with a bunch of IF statements dependent on gross to calculate the taxes. And IF statements don't leave me with some nice equation I can reverse engineer and plunk into the equation to solve for Gross. At least, I don't think so.

#6
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts

butidonot said:

Which leaves me with a bunch of IF statements dependent on gross to calculate the taxes. And IF statements don't leave me with some nice equation I can reverse engineer and plunk into the equation to solve for Gross. At least, I don't think so.

I don't think there is any other elegant ways of doing this. What you want is really defendant on an if clause or a switch. Personally, I think the switch is more efficient.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If a function is defined piece-wise (with if statements), the inverse usually is too. Sorry.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog