科学记数法四舍五入行为

时间:2020-10-18 23:31:07

标签: python floating-point python-unittest integer-arithmetic

是数字“ a”,“ b”和“ c”:

a = 77033412951888085
b = a - 5
c = 2840988860479

一个部门产生以下结果

>>> a/c, b/c
(27115.0, 27115.0)

尝试解决a CodeWars' Kata,我需要计算a = 14949283383840498 * 27115 / 5262)

但是,不是得到

77033412951888085

我明白了

7.703341295188808e+16

...这只是...,但有点错误,叹了口气。 但这足以阻止我通过,这是错误的,因为它被解释为 77033412951888080 。还有...

>>> 14949283383840498 * 27115 / 5262 == 77033412951888085
False

编辑:

由于@GreenCloakGuy的注释解决了我的问题(如图所示),所以我决定添加实际的代码(和测试),因为给定的解决方案对我不起作用。我正在使用Python 3.8.6

import functools

def gcd(a:int, b:int) -> int:
    'Recursive function to return gcd of a and b'
    if a == 0:
        return b
    return gcd(b % a, a)

def lcm(a:int, b:int) -> int:
    'Function to return LCM of two numbers'
    return a / gcd(a, b) * b

def convertFracts(fractions:list) -> list:
    if len(fractions) == 0:
        return []
    denominators = [i[1] for i in fractions]
    LCM = functools.reduce(lambda a, b: lcm(a, b), denominators)
    # Here I attempt so use @GreenCloakGuy 's solution, and use floor division.
    return [[num * LCM // den, LCM] for num, den in fractions]

import unittest
class TestSolution(unittest.TestCase):
    #@unittest.skip
    def test_sample(self):
        a = [[1, 2], [1, 3], [1, 4]]
        b = [[6, 12], [4, 12], [3, 12]]
        self.assertEqual(convertFracts(a), b)

    @staticmethod
    def simplifyFractions(result: list):
        for numerator, denominator in result:
            GCD = gcd(numerator, denominator)
            yield [numerator/GCD, denominator/GCD]

    @unittest.expectedFailure
    def test_failed(self):
        #Test result I got problems with
        test_result = [[77033412951888085, 14949283383840498],
                       [117787497858828, 14949283383840498],
                       [2526695441399712, 14949283383840498]
                       ]

        #Infer initial simplified fractions
        starting_fractions = [fraction for fraction in self.simplifyFractions(test_result)]
        print('Infered starting fractions: ', starting_fractions)

        my_result = convertFracts(starting_fractions)
        print('My result: ', my_result)
        my_result_as_int = list(map(lambda x: [int(x[0]), int(x[1])], convertFracts(my_result)))
        print('My result as int: ', my_result_as_int)
        self.assertEqual(test_result, my_result_as_int)

1 个答案:

答案 0 :(得分:0)

使其工作的两种方法:

  1. 正确使用整数除法(感谢@GreenCloakGuy和@MarkDickinson)
  2. 使用十进制。十进制。可能是this question
  3. 的副本

十进制为我工作。这是更新的代码(仅导入和更新的函数):

正确使用//

import functools

#def gcd

def lcm(a:int, b:int) -> int:
    'Function to return LCM of two numbers'
    return a // gcd(a, b) * b

def convertFracts(fractions:list) -> list:
    if len(fractions) == 0:
        return []
    denominators = [i[1] for i in fractions]
    least_common_multiple = functools.reduce(lambda a, b: lcm(a, b), denominators)
    return [[num * least_common_multiple // den, least_common_multiple] for num, den in fractions]

使用十进制:

import functools
from decimal import Decimal

#def gcd

def lcm(a:int, b:int) -> int:
    'Function to return LCM of two numbers'
    return a // gcd(a, b) * b

def convertFracts(fractions:list) -> list:
    if len(fractions) == 0:
        return []
    denominators = [i[1] for i in fractions]
    least_common_multiple = Decimal(functools.reduce(lambda a, b: lcm(a, b), denominators))
    return [[Decimal(num) * least_common_multiple / Decimal(den), least_common_multiple] for num, den in fractions]

如果lcm使用浮点除法,十进制也可以使用,但是我认为我还是可以解决这个问题,