对python负十进制的模运算。十进制和正整数 [英] Modulo operation on a python negative decimal.Decimal and a positive int

查看:100
本文介绍了对python负十进制的模运算。十进制和正整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用简单的 int s:

>>> -45 % 360
315

其中,使用十进制。十进制

>>> from decimal import Decimal
>>> Decimal('-45') % 360
Decimal('-45')

I将会获得 Decimal('315')

有什么理由吗?有没有办法获得一致的行为(不修补 decimal.Decimal )? (我没有更改上下文,也找不到如何解决这种情况的方法。)

Is there any reason for this? Is there a way to get a consistent behaviour (without patching decimal.Decimal)? (I did not change the context, and cannot find how it could be changed to solve this situation).

推荐答案

Python的行为与到IBM的常规十进制算术规范

Python behaves according to IBM's General Decimal Arithmetic Specification.

剩余 定义为:


余数采用两个操作数;它返回整数除法的余数。 […]

remainder takes two operands; it returns the remainder from integer division. […]

结果是按照 除整数 ,如有必要,四舍五入为精确数字。结果的符号(如果非零)与原始股息的符号相同。

the result is the residue of the dividend after the operation of calculating integer division as described for divide-integer, rounded to precision digits if necessary. The sign of the result, if non-zero, is the same as that of the original dividend.

所以因为 Decimal('-45')// D('360') Decimal('-0'),其余的只能是十进制('-45')

So because Decimal('-45') // D('360') is Decimal('-0'), the remainder can only be Decimal('-45').

为什么商是0而不是-1? 规范说:

Though why is the quotient 0 and not -1? The specification says:


divide-integer 接受两个操作数;它将两个数字相除并返回结果的整数部分。 […]

divide-integer takes two operands; it divides two numbers and returns the integer part of the result. […]

返回的结果定义为在股息大于或等于除数的情况下,反复从股息中减去除数的结果。在此减法过程中,将使用除数和除数的绝对值:最终结果的符号与使用正态除法时将得到的符号相同。 […]

the result returned is defined to be that which would result from repeatedly subtracting the divisor from the dividend while the dividend is larger than or equal to the divisor. During this subtraction, the absolute values of both the dividend and the divisor are used: the sign of the final result is the same as that which would result if normal division were used. […]

注意:[…]


  1. 定义了除整数和余数运算,以便可以将它们计算为标准除法操作(如上所述)。一旦得到整数结果,除法过程便结束。

  1. The divide-integer and remainder operations are defined so that they may be calculated as a by-product of the standard division operation (described above). The division process is ended as soon as the integer result is available; the residue of the dividend is the remainder.


您可以从45中减去360次吗? 0次整数结果可用吗?它是。则商是带减号的0,因为 操作表明

How many times can you subtract 360 from 45? 0 times. Is an integer result available? It is. Then the quotient is 0 with a minus sign because the divide operation says that


结果的符号独占或

为什么小数规范会沿这条路线运行,而不是像在数学中那样进行总是积极的,我推测这可能是为了简化减法算法。无需检查操作数的符号即可计算商的绝对值。无论如何,现代的实现可能都使用更复杂的算法,但是在标准形成和硬件更简单(晶体管更少)的时代,简化可能是一个重要因素。有趣的事实:随着 Penryn

As for why the Decimal Specification goes on this route, instead of doing it like in math where the remainder is always positive, I'm speculating that it could be for the simplicity of the subtraction algorithm. No need to check the sign of the operands in order to compute the absolute value of the quotient. Modern implementations probably use more complicated algorithms anyway, but simplicity could be have an important factor back in the days when the standard was taking form and hardware was simpler (way fewer transistors). Fun fact: Intel switched from radix-2 integer division to radix-16 only in 2007 with the release of Penryn.

这篇关于对python负十进制的模运算。十进制和正整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆