任意精度整数算术:天花板? [英] Arbitrary precision integer arithmetic: ceiling?

查看:88
本文介绍了任意精度整数算术:天花板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将天花板功能应用于任意大小(长整数)。

但是,除法会自动返回其操作数的类型,例如,

例如:math.ceil(7/4)返回1.我可以使用float,如:

math.ceil(7 / float(4)),除非对于非常大的整数float导致

不可接受的精度损失。


找到任意大小的商数上限的最佳方法是什么?
整数? br />

谢谢,

Alasdair

I need to apply the ceiling function to arbitrary sized (long) integers.
However, division automatically returns the type of its operands, so that,
for example: math.ceil(7/4) returns 1. I can use float, as in:
math.ceil(7/float(4)), except that for very large integers float causes an
unacceptable loss of precision.

What is the best way of finding a ceiling of a quotient of arbitrary sized
integers?

Thanks,
Alasdair

推荐答案

Alasdair< am *** *@gmail.comwrites:
Alasdair <am****@gmail.comwrites:

找到任意大小的商数上限的最佳方法是什么?
整数?
What is the best way of finding a ceiling of a quotient of arbitrary sized
integers?



ceiling(a / b)=(a + b-1)// b

ceiling(a/b) = (a+b-1)//b


Alasdair写道:
Alasdair wrote:

我需要将天花板函数应用于任意大小(长整数)。

但是,除法会自动返回其操作数的类型,所以,

例如:math.ceil(7/4)返回1.我可以使用float,如:

math.ceil(7 / float( 4)),除非对于非常大的整数浮动导致

不可接受的精度损失。


找到商的上限的最佳方法是什么任意大小的

整数?
I need to apply the ceiling function to arbitrary sized (long) integers.
However, division automatically returns the type of its operands, so that,
for example: math.ceil(7/4) returns 1. I can use float, as in:
math.ceil(7/float(4)), except that for very large integers float causes an
unacceptable loss of precision.

What is the best way of finding a ceiling of a quotient of arbitrary sized
integers?



使用divmod()同时获取商和余数。如果只有剩余部分大于0,则将1加到



在[11]中:def qceil(x,y):

....:"""找到商x / y的上限。

....:

....:这对于非常大的Python长整数特别有用。

....:"""

....:q,r = divmod(x,y)

... 。:如果r 0:

....:q + = 1

....:返回q

.... :


在[13]中:qceil(7,4)

Out [13]:2


在[14]中:qceil(8,4)

Out [14]:2


在[15]中:qceil(9,4)

Out [15]:3

在[16]中:qceil(100000000000000000000000003,10)

Out [16]:10000000000000000000000001L


-

Robert Kern


我开始相信整个世界都是个谜,一个无害的谜团

由于我们疯狂地试图解释它而使它变得可怕,好像它有一个潜在的真相。

- - Umberto Eco

Use divmod() to get the quotient and the remainder at the same time. Add 1 to
the quotient if and only the remainder is greater than 0.
In [11]: def qceil(x, y):
....: """ Find the ceiling of a quotient x/y.
....:
....: This is especially useful for very large Python long integers.
....: """
....: q, r = divmod(x, y)
....: if r 0:
....: q += 1
....: return q
....:

In [13]: qceil(7, 4)
Out[13]: 2

In [14]: qceil(8, 4)
Out[14]: 2

In [15]: qceil(9, 4)
Out[15]: 3

In [16]: qceil(100000000000000000000000003, 10)
Out[16]: 10000000000000000000000001L

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


3月8日,6:26 * pm,Paul Rubin< http ://phr...@NOSPAM.invalidwrote:
On Mar 8, 6:26*pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:

Alasdair< amc ... @ gmail.comwrites:
Alasdair <amc...@gmail.comwrites:

找到任意大小的商数上限的最佳方法是什么?
整数?
What is the best way of finding a ceiling of a quotient of arbitrary sized
integers?



上限(a / b)=(a + b-1)// b


ceiling(a/b) = (a+b-1)//b



I更喜欢:


上限(a / b)= - ( - a)// b


如果a和b是某种东西也可以其他

比整数(例如有理数)。


Mark

I prefer:

ceiling(a/b) = -(-a)//b

which also works if a and b are something other
than integers (e.g. rational numbers).

Mark


这篇关于任意精度整数算术:天花板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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