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

前端 未结 4 1857
无人共我
无人共我 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:22

    In BigInt, note /% operation which delivers a pair with the division and the reminder (see API). Note for instance

    scala> BigInt(3) /% BigInt(2)
    (scala.math.BigInt, scala.math.BigInt) = (1,1)
    
    scala> BigInt(3) /% 2
    (scala.math.BigInt, scala.math.BigInt) = (1,1)
    

    where the second example involves an implicit conversion from Int to BigInt.

提交回复
热议问题