Using the library fractions module to allow Python to support fractional types

Source: Internet
Author: User
Tags greatest common divisor
Recently encountered in the work of the score processing, find the relevant data discovery can be implemented using the fraction class, so the following article is mainly about the use of the standard library fractions module Let Python support the score type of the relevant data, the text through the sample code introduced in very detailed, You need a friend to refer to below.

Objective

You may not need to deal with fractions often, but Python's fraction class will help you a lot when you need them. This article will give you a detailed introduction to the use of the standard library fractions module to enable Python support score type of relevant content, share it for everyone to reference the study, the following words do not say, come together to see the detailed introduction:

Fractions Module

The Fractions module provides support for fractional types.

Fraction class

The class is the core of the fractions module, which inherits the numbers.Rational class and implements all the methods of that class.

Constructors are not complex:


Class fractions. Fraction (numerator=0, denominator=1) class fractions. Fraction (int|float|str| decimal| Fraction)

You can provide both the numerator (numerator) and the denominator (denominator) to the constructor for instantiating the fraction class, but both must be of type int or numbers.Rational type, or the type error will be thrown. When the denominator is 0, initialization causes an exception to be thrown zeropisionerror.

If you provide only one parameter, you can initialize it with these five types. When initialized with a string, the fractions module uses a built-in regular expression to match. When initialized with a floating-point number or decimal, the fractions module is called internally as_integer_ratio() .

The following code example comes from an official document that shows the various ways to instantiate fraction:


>>> from fractions import fraction>>> fraction ( -10) fraction ( -8, 5) >>> fraction (123) Fraction (123, 1) >>> fraction () fraction (0, 1) >>> fraction (' 3/7 ') fraction (3, 7) >>> fraction (' -3/7 ') Fraction ( -3, 7) >>> fraction (' 1.414213 \t\n ') fraction (1414213, 1000000) >>> fraction ('-.125 ') Fraction ( -1, 8) >>> fraction (' 7e-6 ') fraction (7, 1000000) >>> fraction (2.25) fraction (9, 4) >> > fraction (1.1) fraction (2476979795053773, 2251799813685248) >>> from decimal import decimal>>> Fraction (Decimal (' 1.1 ')) fraction (11, 10)

Limit Denominator

fractions.Fraction.limit_denominator(max_denominator=1000000)

Sometimes a floating-point number or decimal as the initialization data for a fraction instance may encounter rounding errors, such as an Fraction(1.1) example that is not returned when called above Fraction(11, 10) . The fraction class then provides an example method limit_denominator() to reduce this error. This method is intended to achieve an approximate value by limiting the denominator, but it makes the result more accurate when rounding errors occur, as in the following example:


>>> from fractions import fraction>>> fraction (1.1) fraction (2476979795053773, 2251799813685248) >>> Fraction (1.1). Limit_denominator () fraction (11, 10)

Use fraction for arithmetic operations, relational operations, and many other operations

As mentioned above, the fraction class inherits the numbers.Rational class and implements all the methods of that class. So the fraction class actually overloads many special functions so that their instances can be used directly for many arithmetic operations.

In addition to arithmetic operations, the fraction class also supports relational operations, pickle modules, copy modules, and hash value calculations.


>>> from fractions import fraction>>> x = fraction (1, 2) >>> y = fraction (1, 3) >>> x + Yfraction (5, 6) >>> x-yfraction (1, 6) >>> x * yfraction (1, 6) >>> x/yfraction (3, 2) >>> ; X * * 2Fraction (1, 4) >>>-xfraction ( -1, 2) >>> abs (x) fraction (1, 2) >>> round (x) 0>>> Import math>>> Math.floor (x) 0>>> Math.ceil (x) 1>>> x = = yfalse>>> x > YTrue

Other functions


Fractions. Fraction.from_float (FLT) fractions. Fraction.from_decimal (DEC)

Prior to Python3.2, the fraction class did not support obtaining an instance by passing in a floating-point number and decimal into a construction method. Instead, it provides the above two class methods to produce an instance by invoking a class method, and the two class methods of the current version (Python 3.6.1) still exist.


FRACTIONS.GCD (A, B)

Used to calculate greatest common divisor. This function was abandoned after Python3.5, and is officially recommended math.gcd() .

Summarize

There's nothing to summarize ... I can only say that Python is too convenient ... By the way, this blog is actually a dig for a long time of the pit, before there is a part of the source code has not read so has not been sent up. Haha, the back of this standard library Learning series will continue to be even more!

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.