[Solved] Why isn’t my math right


It is not clear why you expect this code to behave differently. Look at these lines:

        var ExchangeRate = c.GrabBTCAmount();
        var AmountInBTC = (c.USDtoBTC(15000, ExchangeRate));
        var AmountAtMarket = (AmountInBTC * ExchangeRate);

If I inline USDtoBTC and inline everything to calculate AmountAtMarket the formula would be

AmountAtMarket  = (15000 / ExchangeRate) * ExchangeRate

So you should always get 15000 with some rounding error.

The bug seems to be in the line where you calculate AmountInBTC. To calculate AmountInBTC you should divide your investment sum (I assume 15000) by the exchange rate at the moment you did your investment rather than current exchange rate.

7

solved Why isn’t my math right