Discuss / Python / 还是会受到计算精度的影响。。。

还是会受到计算精度的影响。。。

Topic source
from functools import reduceDIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': '.'}def str2float(string):    tmp_result = reduce(lambda x, y: x*10+y if y != '.' else x, map(lambda x: DIGITS[x], string))    split_list = string.split('.')    return tmp_result / (10 ** len(split_list[1]))f = input('输入一个小数:')print(str2float(f))

重新排版。。。

from functools import reduce

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': '.'}

def str2float(string):

    tmp_result = reduce(lambda x, y: x*10+y if y != '.' else x, map(lambda x: DIGITS[x], string))

    split_list = string.split('.')

    return tmp_result / (10 ** len(split_list[1]))

f = input('输入一个小数:')print(str2float(f))


  • 1

Reply