MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.88k stars 847 forks source link

Money type operator div overloading #272

Closed ghost closed 7 years ago

ghost commented 7 years ago

Hi,

when using:

var bobInput = Money.Coins(1.1M); var aliceInput = Money.Coins(0.275M); var spend = (bobInput + aliceInput) / 2;

"spend" is falsely calculated due to false boxing/type conversions.

The Money class needs:

    public static Money operator / (Money left, Money right)
    {
        if (left == null)
            throw new ArgumentNullException("left");
        if (right == null)
            throw new ArgumentNullException("right");
        return new Money(checked(left._Satoshis / right._Satoshis));
    }

Regards martin

NicolasDorier commented 7 years ago

Thanks a lot. For this report, will fix it.

NicolasDorier commented 7 years ago

Mh dividing money by money makes no semantic sense though... I see the problem, and if you use Money spend instead of var spend, you don't have issue. But yes, this is tricky and should be fixed.

NicolasDorier commented 7 years ago

fixed by https://github.com/MetacoSA/NBitcoin/commit/79d83ff112d2f08db25aa7cb35b5c123f17d5f13 thanks a lot, version 4.0.0.18 pushed