Bill Ackman, seen in 2013, is chief executive of Pershing Square and one of Wall Street’s best-known hedge fund managers. (Brendan McDermid/Reuters)

Wall Street math can be a wonderful thing. If you’re on the right side of it, that is.

Consider, if you will, the case of Bill Ackman, one of Wall Street’s best-known hedge fund managers, who did one fabulously profitable deal involving the now-disgraced and distressed Valeant drug company and one horribly unprofitable Valeant-related deal.

The profitable deal produced a $350 million payday for Ackman, whose management company gets 16 percent of the profits — in this case, $2.2 billion — that investors in his Pershing Square funds make. The unprofitable deal has generated a $4.2 billion loss, all of which is being borne by Pershing’s investors.

How can this be? Wall Street math. Because the Valeant-related profit was earned in 2014 and the Valeant-related losses did not start until 2015, Ackman got to keep the $350 million fee generated by the 2014 profit.

Now, let’s back up a bit, and I’ll take you through this.

In 2014, a year in which Ackman’s private hedge fund — he also runs a fund with publicly traded shares — earned a stunning 37 percent (after fees) for his investors, Ackman made a controversial but wildly successful deal with Valeant. That was before Valeant’s price-gouging strategy made it a national pariah and its finances turned into a shambles.

Valeant wanted to buy another drug company, Allergan, but knew it would be difficult.

So it made a deal with Ackman for his funds to buy a large Allergan stake before Valeant announced its takeover intentions.

It was like shooting fish in a barrel for Ackman because he knew that Allergan’s price would run up after Valeant made its offer. Which it did. A battle ensued, and Allergan sold out to a Valeant rival. But Ackman’s Pershing Square funds emerged with that $2.2 billion profit.

Then, in 2015, Ackman decided that Valeant itself was a great investment. Which it wasn’t. Valeant’s price gouging and fragile finances drove the stock down and down. So Ackman, who had done everything he could to boost Valeant’s share price — including joining Valeant’s board and picking fights with the likes of Charlie Munger, Warren Buffett’s partner and one of Wall Street’s most revered figures — recently sold Pershing’s final Valeant stake.

Pershing ended up $4.2 billion behind. Pershing’s investors, that is.

Overall, Pershing’s investors lost a combined $2 billion on Valeant deals. But because the $2.2 billion gain came in 2014 and the $4.2 billion loss came in 2015, 2016 and 2017, Pershing’s two Valeant-related deals are treated separately for purposes of calculating “carried interest” fees for Ackman’s management company.

Even though Ackman, who has become one of Wall Street’s most mocked managers, wouldn’t talk to me, it’s only fair to point out that a good part of his piece of the $350 million fee was probably reinvested in Pershing Square. Ackman probably has a good part of his net worth tied up in Pershing’s funds and feels some of the fund investors’ pain.

Even with his disastrous 2015 and 2016 performances — his private fund lost 16 percent and 9.6 percent, respectively, according to people who have seen the figures — his investors have still earned 14.8 percent annually, compounded, from the fund’s launch in 2004 through the end of last year. (The fund is about even this year.) But that’s down from the 24 percent annual return the fund had posted through 2014.

Ackman is now struggling with a different piece of Wall Street math — what’s called the high-water mark.

The big, big money that hedgies make comes from their piece of their investors’ profits. But in a case like Ackman’s, profits don’t begin to get counted until Pershing Square tops its previous high, which was in 2014.

Ackman’s private fund is about 24 percent below its high-water mark. That means it has to rise about 32 percent from its current level just to get back to where it was at year-end 2014 before it can resume generating “carried interest” fees for Ackman. (The math: 100 divided by 76 is about 1.32.)

That’s a daunting obstacle. Another problem is a civil suit, currently pending, that charges that the $2.2 billion of profit Pershing made on Allergan was illicit because it stemmed from material nonpublic information that Ackman got from Valeant.

Valeant has agreed to bear 60 percent of any loss that Pershing might incur as a result of the suit. That leaves Pershing Square investors at risk for 40 percent.

If Pershing holders have to shell out money as the result of the suit, will Ackman’s management company kick in 16 percent of any outlays, given that it took 16 percent of the profits?

Some day, we may find out. And we’ll all get another example of how Wall Street math works.