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

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

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:

import math

def aPythonOperator(a, b):
    quotient, remainder = math.modf(a / b)
    return quotient, remainder

result = aPythonOperator(8, 3)
print(f'aPythonOperator(8, 3) = {result}')

This will give you the desired output:

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:

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:

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

which gives:

Division:2.6666666666666665
Floored division: 2.0
Modulo: 2.0
Remainder: -1.0

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

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

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

def hackDivmod(a,b):  
    #HACK
    Div = (a/b)
    floredDiv = (a//b) 
    return floredDiv,  Div-floredDiv 

This works as desired:

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

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

答案2

得分: 1

以下是翻译好的部分:

def divfract(a, b):
    return a // b, (a % b) / b

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

英文:

You could do

def divfract(a, b):
    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() 函数来实现如下功能:

import math
def myDivMod(myParam, myDiv):
    fraction, wholeNum = math.modf( myParam / myDiv)
    return fraction, wholeNum

a,b = myDivMod(8,3)
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:

import math
def myDivMod(myParam, myDiv):
    fraction, wholeNum = math.modf( myParam / myDiv)
    return fraction, wholeNum

a,b = myDivMod(8,3)
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:

确定