Q: This year, we are selling the townhome that we bought almost 30 years ago. We lived there first for 17 years as a primary residence, then we rented it. How do I calculate capital gain? Do we use the sales price or do we use the market price when we began renting it out?

A: Your question seems simple, but it’s actually complicated. We don’t have enough information from you to give you a detailed answer, so we’ll make a few assumptions and provide some general advice. But you’ll definitely want to talk with your tax preparer or someone who has experience with calculating the capital gains tax owed on investment property.

Let’s start with the obvious: You’ve owned the property for a long time. Let’s assume you purchased your townhome for $100,000 and are selling it for $1 million. If you subtract the purchase price from the sales price, you might think you have a profit of $900,000. We’ll assume you made improvements to the home over time. In addition, we believe you paid something to buy it and will incur expenses when you sell.

Some of the improvements reduce your profit, as do the costs of purchase and sale. Some of these expenses include commissions paid to real estate agents, fees paid to settlement agents, transfer taxes and recording fees. If you put in a new kitchen for $50,000, you should be able to exclude that amount from the profits.

The first step in calculating your cost basis is getting your paperwork together. Think about any material changes you made to the house (additions, roof replacement, plumbing, electrical upgrades, etc.) and add up those costs. For more detailed information on what expenses are allowed, see IRS Publication 523 on the IRS website.

If you never rented your townhome and were just selling it, you could add up all of these material costs, plus the cost of buying and selling, and subtract it from the sales price. This would give you your profit.

But renting out your townhome will affect your profit. While you rented out the home, you should have reported the rental income. In addition, you would have been able to deduct the interest you paid on your mortgage (if you still had it) and myriad other expenses. In addition, you should have received a tax break — the ability to depreciate the value of the home over time.

Now that you’re selling, the federal government will tax you on the amount of depreciation you took over the years that you rented out the property and owned it as an investment property.

Back to our example. Let’s say you spent $100,000 improving the property while it was rented but took $50,000 in depreciation. Finally, we’ll assume you had $100,000 in expenses from the purchase and sale of the home.

Your numbers would now look as follows: The purchase price was $100,000; the sales price was $1 million, resulting in a $900,000 profit. We would reduce that profit by the $100,000 cost of purchase and sale plus $100,000 from the improvements. That means your true profit is $700,000, which is the amount on which you’ll pay long-term capital gains tax. Unless there are extenuating circumstances, your tax rate would be 20%.

In addition, you’d have to pay a 25% tax on $50,000 in depreciation that you took while you owned the property; this is known as “recapture.” The government also charges an additional tax of 3.8% on capital gains from real estate. This is the net investment income tax.

If you only rented out the property a short time, you might be able to take advantage of another tax break. Owners who have lived in their home for two of the past five years would get to exclude from capital gains the first $250,000 of profits if they are single or up to $500,000 in profits if married. You wouldn’t qualify for this exclusion, however.

We’ve glossed over the numbers to give you a general idea of what you might owe. But please consult with a tax professional to go through the details of your ownership and figure out exactly what you will owe the federal government and your state, if your state has an income tax.

Ilyce Glink is the CEO of Best Money Moves and Samuel J. Tamkin is a real estate attorney. Contact them through the website ThinkGlink.com.