圆形和int之间的python差异 [英] python difference between round and int

查看:128
本文介绍了圆形和int之间的python差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在一直在玩python并且我注意到一个让我好奇的奇怪行为: float(int(n)) round(n)

I've been playing around with python for a bit now and i've noticed a strange behavior that makes me curious: what is the difference between float(int(n)) and round(n)?

我应该何时使用一个,另一个或两者都没有?

When should I use one, another or neither of them?

推荐答案

为了完整起见,让我在你的问题中添加两个函数并解释之间的差异float(int(x)) math.floor(x) round(x) math.ceil(x)

For the sake of completeness, let me add two more functions to your question and explain the differences between float(int(x)), math.floor(x), round(x) and math.ceil(x).

让我们从一个问题开始:什么整数代表最好的1.6号?
我们有两个可能的答案(1和2),但有很多不同的原因,为什么一个答案可能比另一个答案更好:

Let's start with a question: "What integer represents best the number 1.6?" We have two possible answers (1 and 2) but many different reasons why one answer may be better than the other one:


  • int(1.6)== 1 :这是切断小数时所得到的。

  • math.floor(1.6)== 1 :小于2.不完整的部分不计算。

  • round(1.6 )== 2 :因为2小于1。

  • math.ceil(1.6)== 2 :超过1.当你开始零件时,你必须支付全价。

  • int(1.6)==1: This is what you get when you cut off the decimals.
  • math.floor(1.6)==1: Its less than 2. Incomplete pieces don't count.
  • round(1.6)==2: Because 2 is closer than 1.
  • math.ceil(1.6)==2: Its more than 1. When you start a part, you have to pay the full price.

让我们问python打印你得到的不同x值的结果很好的表格:

Let's ask python to print a nice table of the results you get with different values of x:

from math import floor, ceil
tab='\t' 

print 'x \tint\tfloor\tround\tceil'
for x in (1.0, 1.1, 1.5, 1.9, -1.1, -1.5, -1.9):
    print x, tab, int(x), tab, floor(x), tab, round(x), tab, ceil(x)

这是输出:

x       int floor   round   ceil
1.0     1   1.0     1.0     1.0
1.1     1   1.0     1.0     2.0
1.5     1   1.0     2.0     2.0
1.9     1   1.0     2.0     2.0
-1.1    -1  -2.0    -1.0    -1.0
-1.5    -1  -2.0    -2.0    -1.0
-1.9    -1  -2.0    -2.0    -1.0

您看到这四个函数中没有一个是相等的。

You see that no of these four functions are equal.


  • 地板向负无穷大舍入:它总是选择最低的答案: floor(1.99)== 1 floor( -1.01)== - 2

  • ceil 向无穷大舍入:它总是选择最高的回答: ceil(1.01)== 2 ceil(-1.99)= - 1

  • int 向零舍入:对于正 x ,它就像 floor ,对于负 x ,它就像 ceil

  • round 舍入到最接近的解决方案: round(1.49)= 1 a nd round(1.51)== 2 。当 x 恰好在两个数字之间时, round(x)从零开始舍入: round (1.5)== 2 round(-1.5)== - 2 。这与 int(x)在这种情况下会做的相反。

  • floor rounds towards minus infinity: It chooses always the lowest possible answer: floor(1.99)==1 and floor(-1.01)==-2.
  • ceil rounds towards infinity: It chooses always the highest possible answer: ceil(1.01)==2 and ceil(-1.99)=-1.
  • int rounds towards zero: For positive x it is like floor, for negative x it is like ceil.
  • round rounds to the closest possible solution: round(1.49)=1 and round(1.51)==2. When x is precisely between two numbers, round(x) rounds away from zero: round(1.5)==2 and round(-1.5)==-2. This is the opposite of what int(x) would do in this case.

注意 int(x)总是返回一个整数---其他函数返回浮点数。

Note that int(x) always returns an integer --- the other functions return floating point numbers.

这篇关于圆形和int之间的python差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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