Python Learning Note (iv) number (II)

Source: Internet
Author: User

Python

The problem of division in Python2

  1  >>> 3/6  2  0   3  >>> 3.0/6 4  0.5 5  >>> 3.0/6.0 6  0.5< Span style= "COLOR: #008080" > 7  >>> 5/2  8  2 9  >>> 6.0/310  2.011  >>> 10.0/312  3.3333333333333335  >>> 0.2 +0.914  1.115  >>> 0.2 +0.1 16  0.30000000000000004 
Because the computer to convert our input decimal into binary, floating-point number conversion binary, there will be a problem 0.1 to bits 0.000110011 conversion will not be exactly equal to the decimal 0.1, generally we will be rounding the results reserved two bits, for calculation.

Python2 is the division takes the integer part, thus obtains 3/6 =0,5/2=2

Can be referenced from the __FUTURE__ Import Division module to resolve

1  from __future__ Import Division 2 >>> 3/63 0.54 >>>

Remainder in Python

Two ways of the remainder

1,%

2, Divmod (), get quotient and remainder

1 >>> 6 52 13 >>> 6% 44 25 >>> divmod (6,4) /c5>6 (1, 2)    quotient and remainder 7 >>> divmod (6,5)8 (1, 1)  

  

Python Learning Note (iv) number (II)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.