A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/as_integer_ratio-python-reduced-fraction-given-rational/ below:

as_integer_ratio() in Python for reduced fraction of a given rational

as_integer_ratio() in Python for reduced fraction of a given rational

Last Updated : 30 Nov, 2021

Given a rational number d, print the reduced fraction which gives d.
Examples: 

Input : d = 2.5 
Output : 5/2
Explanation: 5/2 gives 2.5 which is the reduced form
             of any fraction that gives 2.5 

Input : d = 1.5 
Output : 3/2 


as_integer_ratio() function Python: 
Returns a pair of integers whose ratio is exactly equal to the original float and with a positive denominator. 

Syntax: 
float. as_integer_ratio() 
Return Value: 
Tuple (a pair of integers) 
Errors: 
Raises OverflowError on infinities and a ValueError on NaNs.


In Python we have an inbuilt function as_integer_ratio() which prints the reduced fraction form of any given rational number d. We need to store that in any variable and then print the 0th index and 1st index of the stored fraction.

Python3
# function to print the fraction of 
# a given rational number 
def reducedfraction(d): 

    # function that converts a rational number 
    # to the reduced fraction 
    b = d.as_integer_ratio() 

    # reduced the list that contains the fraction 
    return b 
    
# driver code 
b = reducedfraction(2.5) 
print (b[0], "/", b[1]) 

Output: 

5 / 2 


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4