Is there a division operation that produces both quotient and reminder?

前端 未结 4 1891
无人共我
无人共我 2021-01-12 03:00

Currently I write some ugly code like

    def div(dividend: Int, divisor: Int) = {
        val q = dividend / divisor
        val mod = dividend % divisor
          


        
4条回答
  •  佛祖请我去吃肉
    2021-01-12 03:03

    No (except for BigInt, as mentioned in other answers), but you can add it:

    implicit class QuotRem[T: Integral](x: T) {
      def /%(y: T) = (x / y, x % y)
    }
    

    will work for all integral types. You can improve performance by making separate classes for each type such as

    implicit class QuotRemInt(x: Int) extends AnyVal {
      def /%(y: Int) = (x / y, x % y)
    }
    

提交回复
热议问题