Make Python integer evaluates to floating-point

Source: Internet
Author: User

This is an introductory knowledge, I recorded.

In Python, by default, if the expression is full of integers, the result will be just an integral type. Sometimes you don't notice unexpected results, such as:

>>> a = 7>>> b = 3>>> c = a/b>>> Print C2

Sometimes it can cause a certain error, so when you need to get the result of floating-point precision, you must have a floating-point type in the expression.

If it is a constant, it can be solved by adding.

>>> Print 7.0/32.33333333333

If all variables are variable, you can use the Float function to convert, or *1.0, such as

>>> a = 7>>> B = 3>>> print float (a)/b2.33333333333>>> print a*1.0/b2.33333333333

But remember to start the conversion at the first place, or else there will be problems, such as

>>> a=7>>> b=3>>> c=2>>> print a/b*float (c) 4.0>>> print float (a)/b* c4.66666666667

Make Python integer evaluates to floating-point

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.