从浮点数的除法中获取商和余数。

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

Get quotients and remainder from division of floats

问题

The Pythonic approach to getting quotients and remainders from division of floats can be achieved using the built-in math.modf function. Here's an example:

  1. import math
  2. def aPythonOperator(a, b):
  3. quotient, remainder = math.modf(a / b)
  4. return quotient, remainder
  5. result = aPythonOperator(8, 3)
  6. print(f'aPythonOperator(8, 3) = {result}')

This will give you the desired output:

  1. aPythonOperator(8, 3) = (2.0, 0.6666666666666665)

The math.modf function separates the integer part (floored division) and the fractional part of the division result, providing the desired behavior without rounding the remainder.

英文:

What is the Pythonic approach to getting a quotients and remainder from division of floats? So taking 8 divided by 3 which is 2.66666666666

so I want:

  1. aPythonOperator(8,3) = 2.0, 0.66666666666

but I cannot find a clean way of doing this without the remainder being rounded.
For completeness the Python division operators are:

  1. import math
  2. a = 8.0
  3. b = 3.0
  4. print(f'Division:{a/b}')
  5. print(f'Floored division: {a//b}')
  6. print(f'Modulo: {a%b}')
  7. print(f'Remainder: {math.remainder(a,b)}') # rounds to int and yields -1 (?)
  8. print(f'Divmod: {divmod(a, b)}')

which gives:

  1. Division:2.6666666666666665
  2. Floored division: 2.0
  3. Modulo: 2.0
  4. Remainder: -1.0

These don't help, but I expected the Divmod function to work. However, Divmod rounds the quotient to an int:

  1. print(f'Divmod: {divmod(a, b)}')
  2. Divmod: (2.0, 2.0)

I can get the behaviour I want by creating my own version of the Divmod function:

  1. def hackDivmod(a,b):
  2. #HACK
  3. Div = (a/b)
  4. floredDiv = (a//b)
  5. return floredDiv, Div-floredDiv

This works as desired:

  1. HackDivmod: (2.0, 0.6666666666666665)

But is there a better way do doing this using built in functions or operators?

答案1

得分: 3

I guess you want to use math.modf() combined with native division

>>> import math
>>> math.modf( 8 / 3)
(0.6666666666666665, 2.0)

英文:

I guess you want to use math.modf() combined with native division

  1. >>> import math
  2. >>> math.modf( 8 / 3)
  3. (0.6666666666666665, 2.0)

答案2

得分: 1

以下是翻译好的部分:

  1. def divfract(a, b):
  2. return a // b, (a % b) / b

这段代码返回与您的“hack”函数相同的结果(实际上也完全可以),包括负数(例如 divfract(-8, 3) == (-3, 0.333...)),而 math.modf 则是另一种方式 (-2, -0.666...)。(有关计算余数的不同方法,请参考此链接

英文:

You could do

  1. def divfract(a, b):
  2. return a // b, (a % b) / b

This returns the same result as your 'hack' function (which is actually also perfectly fine), including for negative numbers (e.g divfract(-8, 3) == (-3, 0.333...)), whereas math.modf rounds the other way (-2, -0.666...). (There are different ways to calculate a remainder)

答案3

得分: 0

你可以使用 math.modf() 函数来获取两个分开的部分。它是一个内置方法,用于将数字拆分为整数和小数部分。你可以使用以下自定义的 myDivMod() 函数来实现如下功能:

  1. import math
  2. def myDivMod(myParam, myDiv):
  3. fraction, wholeNum = math.modf( myParam / myDiv)
  4. return fraction, wholeNum
  5. a,b = myDivMod(8,3)
  6. print(a,b) # 输出是 0.6666666666666665, 2.0

希望这解决了你的问题。

英文:

You can use the math.modf() to get two separate portions. It is a built in method to split a number into integer and decimal parts. You can use the following custom myDivMod() function is order to make it work as following:

  1. import math
  2. def myDivMod(myParam, myDiv):
  3. fraction, wholeNum = math.modf( myParam / myDiv)
  4. return fraction, wholeNum
  5. a,b = myDivMod(8,3)
  6. print(a,b) # output is 0.6666666666666665, 2.0

I hope this solves your issue

huangapple
  • 本文由 发表于 2023年5月8日 00:24:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195047.html
匿名

发表评论

匿名网友

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

确定