Jul-02-2019, 09:57 PM
(This post was last modified: Jul-02-2019, 09:58 PM by Gribouillis.)
This approximate is also given by the Fraction module
Note that for most floating numbers, you cannot expect a significant reduction of the size of the integers by doing this because the global number of bits needed to distinguish these numbers is fixed.
>>> from fractions import Fraction as F >>> y = F(884279719003555,281474976710656) >>> y.limit_denominator(100000000) Fraction(245850922, 78256779)The purpose of
float.as_integer_ratio()
is to give an integer ratio that is exactly equal to the real number stored in memory (which is not pi but an approximation of pi). It is a different problem from approximating pi with shorter fractions. The IEEE754 format indeed implies that the actual denominator is a power of two.Note that for most floating numbers, you cannot expect a significant reduction of the size of the integers by doing this because the global number of bits needed to distinguish these numbers is fixed.