是否存在会同时产生商和提醒的除法运算? [英] Is there a division operation that produces both quotient and reminder?
本文介绍了是否存在会同时产生商和提醒的除法运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我编写了一些丑陋的代码,例如
Currently I write some ugly code like
def div(dividend: Int, divisor: Int) = {
val q = dividend / divisor
val mod = dividend % divisor
(q, mod)
}
是在标准库中指定的吗?
Is it specified in standard library?
推荐答案
否(如其他答案中所述,BigInt
除外),但是您可以添加它:
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)
}
这篇关于是否存在会同时产生商和提醒的除法运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文