tutorial on the use of fractions in Python _python

Source: Internet
Author: User
Tags gcd

You may not need to deal with points often, but Python's fraction class can help you when you need them. In this guide, I'll provide some interesting examples of how to handle fractions and highlight some cool features.
1 Basic

The fraction class is in the lib/fractions.py file, so you can import it like this:

From fractions import fraction

There are many ways to instantiate the fraction class.

First, you can pass in the numerator and the denominator:

>>> fraction (1, 2)
fraction (1, 2)

Or use another fraction to instantiate:

>>> f = fraction (1, 2)
>>> fraction (f)
fraction (1, 2)

To instantiate using a floating-point number:

>>> fraction (2.5)
fraction (5, 2)

Or use a decimal:

>>> from decimal import decimal
>>> fraction (decimal (' 1.1 '))
fraction (11, 10)

The last method, perhaps the most interesting one, is that you can instantiate the fraction class with a string:

>>> fraction (' 9/16 ')
fraction (9, 16)

Essentially, the fraction class is designed so that you don't have to do a lot of processing before instantiating the class. The fraction class knows how to handle many different data types.
2 automatic numerator

Numerator is not very difficult, but for some complex points, numerator still need to do something. The fraction class is particularly useful in this regard because it automatically numerator fractions.

>>> Fraction (153, 272)
fraction (9, 16)

Simply think, you may not be able to numerator 153/172, but the fraction class can quickly complete numerator.
32-dollar Operation

You can perform a two-dollar operation on a fraction object like an integer and a floating-point number.

Two fractions to add operations:

>>> fraction (1, 2) + fraction (3, 4)
fraction (5, 4)

This is convenient, but you can also mix integers or floating-point numbers. As you would expect, the fraction object is added to an integer to return a fraction object, but adds a floating-point number to the sum of the floating-point numbers.

>>> fraction (5) + 3
fraction (+)
>>> fraction (5) + 3.0
3.3125

Here are some other examples of the two-dollar operation:

>>> fraction (5)-fraction (1, 4)
fraction (1)
>>> fraction (1,) * fraction (3
) Fraction (3, 256)
>>> fraction (3)/fraction (1, 8)
fraction (3, 2)

Now let's try the exponentiation operation:

>>> fraction (1, 8) * * FRACTION (1, 2)
0.3535533905932738

It returns a floating-point number, possibly because the score cannot be calculated properly. In fact, we can use the Limit_denominator method to get an approximate fraction value.

>>> f = fraction (1, 8) * * FRACTION (1, 2)
>>> fraction (f). Limit_denominator ()
fraction (235416 , 665857)

Remember, you can mix the string and the data types mentioned in the other top instantiation section.

>>> fraction ("1/2") + fraction (2.0)
fraction (5, 2)
>>> fraction (2) * fraction ("  1/2  ")
Fraction (1, 1)

4 getting the properties of the fraction object

You've got a fraction object, and you've done some calculations, so how do we access its properties now?

If you don't read the document, you might try Fraction.numerator and fraction.denominator, and it turns out you're right.

>>> f = fraction (221, 234) + fraction (1, 2)
>>> f.numerator
>>> F.denominator
9

Or as a string, print the entire score:

>>> print F
13/9
>>> a = str (f)
>>> a
' 13/9 '
5 GCD

This is not part of the fraction class, it is in the fractions library. With it you can quickly find a two-digit gcd.

First import:

From fractions import GCD

Some examples:

>>> gcd
>>> gcd (221, 234)
13


6 Summary

Hopefully you've learned something about handling fractions in Python. If you want to read more, you can view the document. If you feel very motivated to learn, you can look at the source code.

If you have more interesting points to use, tell me and I will add them to the guide.

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.