返回商和余数的除法

huangapple go评论117阅读模式
英文:

Division with returning quotient and remainder

问题

我尝试从Python迁移到Golang。我目前正在研究一些数学运算,并想知道如何在除法的结果中同时获取商和余数。我将在下面分享一个等价的Python代码。

hours, remainder = divmod(5566, 3600)
minutes, seconds = divmod(remainder, 60)
fmt.Printf("%d:%d\n", minutes, seconds)
// 32:46

以上是我的目标。谢谢。

英文:

I try to migrate from Python to Golang. I currently do research some math operations and wonder about how can I get both quotient and remainder value with result of a division. I'm going to share below an equivalent of Python code.

hours, remainder = divmod(5566, 3600)
minutes, seconds = divmod(remainder, 60)
print('%s:%s' % (minutes, seconds))
# 32:46

Above will be my aim. Thanks.

答案1

得分: 38

整数除法加上取模可以实现这一点。

func divmod(numerator, denominator int64) (quotient, remainder int64) {
    quotient = numerator / denominator // 整数除法,小数部分被截断
    remainder = numerator % denominator
    return
}

https://play.golang.org/p/rimqraYE2B

编辑:定义

(Quotient),在整数除法的上下文中,是被除数整除以除数的_整数_倍数的次数。换句话说,它与十进制语句FLOOR(n/d)相同。

模数(Modulo)给出这种除法的_余数_。分子和分母的模数始终在0和d-1之间(其中d是除数)。

英文:

Integer division plus modulus accomplishes this.

func divmod(numerator, denominator int64) (quotient, remainder int64) {
    quotient = numerator / denominator // integer division, decimals are truncated
    remainder = numerator % denominator
    return
}

https://play.golang.org/p/rimqraYE2B

Edit: Definitions

Quotient, in the context of integer division, is the number of whole times the numerator goes into the denominator. In other words, it is identical to the decimal statement: FLOOR(n/d)

Modulo gives you the remainder of such a division. The modulus of a numerator and denominator will always be between 0 and d-1 (where d is the denominator)

答案2

得分: 31

如果你想要一行代码的话,

商,余数 := 被除数/除数, 被除数%除数
英文:

if you want a one-liner,

quotient, remainder := numerator/denominator, numerator%denominator

答案3

得分: 1

如果你有一个32位的数字,你可以使用以下其中之一:

package main
import "math/bits"

func main() {
   { // 示例 1
      var n uint = 4294967295
      q, r := bits.Div(0, n, 2)
      println(q == n / 2, r == 1)
   }
   { // 示例 2
      var n uint32 = 4294967295
      q, r := bits.Div32(0, n, 2)
      println(q == n / 2, r == 1)
   }
}

如果你有一个64位的数字,你可以这样做:

package main
import "math/bits"

func main() {
   var n uint64 = 18446744073709551615
   q, r := bits.Div64(0, n, 2)
   println(q == n / 2, r == 1)
}

如果你有一个大于64位的数字,你可以这样做:

package main
import "math/bits"

func main() {
   q, r := bits.Div64(1, 0, 2)
   println(q == 9223372036854775808, r == 0)
}
英文:

If you have a 32 bit number, you can use one of these:

package main
import "math/bits"

func main() {
   { // example 1
      var n uint = 4294967295
      q, r := bits.Div(0, n, 2)
      println(q == n / 2, r == 1)
   }
   { // example 2
      var n uint32 = 4294967295
      q, r := bits.Div32(0, n, 2)
      println(q == n / 2, r == 1)
   }
}

If you have a 64 bit number, you can do it like this:

package main
import "math/bits"

func main() {
   var n uint64 = 18446744073709551615
   q, r := bits.Div64(0, n, 2)
   println(q == n / 2, r == 1)
}

If you have something larger than 64 bit, you can do it like this:

package main
import "math/bits"

func main() {
   q, r := bits.Div64(1, 0, 2)
   println(q == 9223372036854775808, r == 0)
}

答案4

得分: 0

我做了这个函数,也许这就是你要找的。

// QuotientAndRemainderF32计算输入的整数商和余数。该函数将floor(x/y)四舍五入为最接近负无穷大的整数。
func QuotientAndRemainderF32(x, y float32) (Remainder, Quotient float32) {
    Quotient = float32(math.Floor(float64(x / y)))
    Remainder = x - y*Quotient
    return Remainder, Quotient
}
英文:

I did this function maybe it's what you are looking for.

//QuotientAndRemainderF32 Computes the integer quotient and the remainder of the inputs. This function rounds floor(x/y) to the nearest integer towards -inf.
func QuotientAndRemainderF32(x, y float32) (Remainder, Quotient float32) {
	Quotient = float32(math.Floor(float64(x / y)))
	Remainder = x - y*Quotient
	return Remainder, Quotient
}

答案5

得分: 0

我认为这个问题不够清楚:你是在寻找与Python的divmod等效的函数,还是只是想要一个返回商和余数的函数?

这些解决方案与Python的divmod不等效,因为它们返回的是余数而不是模数。因此,它们在处理负数时的方式不同。

在Python中,divmod(-7, 4)返回-2, 1
这里的答案返回-1, 3

我的建议是:

func DivMod(n, d int64) (q, r int64) {
	q = n / d
	r = n % d

	// 分子或分母为负(但不是同时)
	if r != 0 && n*d < 0 {
		q--
		r += d
	}
	return
}
英文:

I think that this question is not clear: are you looking for something equivalent to the Python divmod or just a function that returns the quotient and the reminder?

These solutions are not equivalent to the Python divmod since they return the reminder instead of the modulus. So, they do not work the same way with negative numbers.

In Python divmod(-7, 4) returns -2, 1.
The answers here return -1, 3.

My suggestion is

func DivMod(n, d int64) (q, r int64) {
	q = n / d
	r = n % d

	// the numerator or the denominator is negarive (but not both)
	if r != 0 &amp;&amp; n*d &lt; 0 {
		q--
		r += d
	}
	return
}

huangapple
  • 本文由 发表于 2017年5月13日 04:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/43945675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定