Generic Numeric division

前端 未结 2 1974
臣服心动
臣服心动 2020-12-18 04:26

As a general rule, we can take any value of any number type, and divide it by any non-zero value of any number type, and get a reasonable result.

212.7 / 6           


        
相关标签:
2条回答
  • 2020-12-18 04:44

    The reason is because integer division and float division are two very different operations, so all Numerics do not share a common division operation, although humans might think of them both as "division."

    The workaround would be to create 4 division operations: Integral/Integral, Integral/Fractional, Fractional/Integral, Fractional/Fractional. Do the calculation in whatever application-specific way you feel is appropriate. When I did this for a calculator I wrote, I kept it in Integral if possible, and cast to Double otherwise.

    0 讨论(0)
  • 2020-12-18 05:02

    My understanding is that these traits describe sets closed under defined operations:

    Numeric is closed under plus, minus, times, negate,

    Fractional adds div (i.e. plus, minus, times, negate, div),

    Integral adds quot and rem (i.e. plus, minus, times, negate, quot, rem).

    Why do you want to sidestep it?

    0 讨论(0)
提交回复
热议问题