科学记数法舍入行为

Scientific notation rounding behaviour

无论是数字 '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. 整数除法的正确使用(感谢@GreenCloakGuy 和@MarkDickinson)
  2. 使用decimal.Decimal。可能是 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 使用浮点除法,小数也可以工作,但我认为无论如何我都会进行修复,因为它是有道理的。