Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Why do int.numerator and int.denominator exist?

Tags:

int.numerator and int.denominator are a mystery to me.

help(int.numerator) states:

the numerator of a rational number in lowest terms

But as far as I know, int is not a rational number. So why do these properties exist?

like image 635
Dave Halter Avatar asked Apr 14 '12 19:04

Dave Halter


People also ask

Why is it called a numerator and denominator?

One simple way to understand a fraction is to imagine a pizza. The denominator shows you how many equal pieces the pizza is divided into, and the numerator represents how many of those pieces you have. In Latin, numerator means "counter or numberer."

What does integer denominator mean?

The meaning of the denominator is the divisor of a fraction. The denominator is the number or integer that is below the horizontal line in a fraction. Whereas the numerator lies above the line in a fraction.

What is denominator in Python?

Fractionis used to create a Fraction object. It takes Numerator and Denominator. The default value of the numerator is 0 and denominator is 1. It raises ZeroDivisionError when the denominator is 0. At first we will see how the class can create fractions using Numerator and Denominator.

Which is more important numerator or denominator?

Improper fractions – in this type of fraction, the numerator has a greater value than the denominator. Solving mixed fractions results in improper fractions. For example- 7/2, 11/5, 7¾, and so on. Proper fractions – in proper fractions, the numerator has a lower value than the denominator.


1 Answers

See http://docs.python.org/library/numbers.html - int (numbers.Integral) is a subtype of numbers.Rational.

>>> import numbers >>> isinstance(1337, numbers.Integral) True >>> isinstance(1337, numbers.Rational) True >>> issubclass(numbers.Integral, numbers.Rational) True 

The denominator of an int is always 1 while its numerator is the value itself.

In PEP 3141 you find details about the implementation of the various number types, e.g. proving the previous statement:

@property def numerator(self):     """Integers are their own numerators."""     return +self  @property def denominator(self):     """Integers have a denominator of 1."""     return 1 
like image 116
ThiefMaster Avatar answered Sep 28 '22 09:09

ThiefMaster