The gain is sale price minus basis minus costs
The gain on a sale is the sale price less your cost basis less the selling costs - commissions, transfer taxes and closing costs. The cost basis is what you paid plus capital improvements, minus any depreciation you claimed while renting part of the home.
A sale that loses money produces no gain and nothing to tax: the page floors the result at zero, prints the loss amount and switches the note to a warning that names the shortfall. That branch also says plainly that the Section 121 exclusion is not used and that a capital loss offsetting other income is outside the model.
A sale price of $0 returns a single warning instead of a breakdown, because there is no transaction to price. Every other money input is clamped at zero and at the page ceiling, so the arithmetic always has a finite answer even for an absurd entry.