浮点列表的gcd - 分数的输出不正确

时间:2017-05-10 09:42:14

标签: python list floating-point greatest-common-divisor

我需要一个获取浮点数列表的函数,并计算该列表的gcd。

例如,给定输入[1/2.0, 1/3.0]我希望输出为1/6.0

但事实并非如此。它会打印5.55111512313e-17或换句话说,为零。这是我的代码:

def gcd(L):
    return reduce(fractions.gcd, L)

print gcd([1/2.0, 1/3.0])

这里有什么问题?有什么办法可以解决吗?

2 个答案:

答案 0 :(得分:0)

您需要使用Stream<String> infinite = Stream.generate(() -> "chimp"); 个对象,因为Fraction不精确,这就是float类存在的原因:

Fraction

答案 1 :(得分:0)

避免经历浮点麻烦,只需将您的数字保持为分数,保持其准确性:

Follow these steps to create a Linked Server:

       Server Objects -> Linked Servers -> New Linked Server
       Provide Remote Server Name.
       Select Remote Server Type (SQL Server or Other).
       Select Security -> Be made using this security context and provide login and password of remote server.
       Click OK and you are done !!
        [HERE][1]ple tutorial for creating a linked server.

       OR

       You can add linked server using query.

       Syntax:

       sp_addlinkedserver [ @server= ] 'server' [ , [ @srvproduct= ] 'product_name' ] 
            [ , [ @provider= ] 'provider_name' ]
            [ , [ @datasrc= ] 'data_source' ] 
            [ , [ @location= ] 'location' ] 
            [ , [ @provstr= ] 'provider_string' ] 
            [ , [ @catalog= ] 'catalog' ] 
       Read more about sp_addlinkedserver.

       You have to create linked server only once. After creating linked server, we can query it as follows:

       select * from LinkedServerName.DatabaseName.OwnerName.TableName