检查了一些在Python是理性的理性、Python

由网友(感情早已陌生)分享简介:我想知道检查的好方法,如果一个数字x是一个理性的蟒蛇(两个整数N,M存在使X = N / M)。I would like to know a good way of checking if a number x is a rational (two integers n,m exist so that x=n/m)...

我想知道检查的好方法,如果一个数字x是一个理性的蟒蛇(两个整数N,M存在使X = N / M)。

I would like to know a good way of checking if a number x is a rational (two integers n,m exist so that x=n/m) in python.

在数学,这是由函数完成

In mathematica, this is done by the function

Rationalize[6.75]
27/4

我认为这个问题对于一个给定精度的答案。 有没有获得这两个整数的常见算法?

I assume this question has an answer for a given accuracy. Is there a common algorithm of obtaining these two integers?

推荐答案

在蟒蛇> = 2.6存在的 as_integer_ratio 上彩车方式:

In python >= 2.6 there is a as_integer_ratio method on floats:

>>> a = 6.75
>>> a.as_integer_ratio()
(27, 4)
>>> import math
>>> math.pi.as_integer_ratio()
(884279719003555, 281474976710656)

然而,由于路上浮标在编程语言中定义的没有无理数的

阅读全文

相关推荐

最新文章