Implementation limitations of float.as_integer_ratio()

后端 未结 3 641
情歌与酒
情歌与酒 2020-12-06 11:03

Recently, a correspondent mentioned float.as_integer_ratio(), new in Python 2.6, noting that typical floating point implementations are essentially rational approximations o

3条回答
  •  渐次进展
    2020-12-06 11:49

    You get better approximations using

    fractions.Fraction.from_float(math.pi).limit_denominator()
    

    Fractions are included since maybe version 3.0. However, math.pi doesn't have enough accuracy to return a 30 digit approximation.

提交回复
热议问题