如何使用 'exotic' 函数提高小数精度?
How to increase decimal precision with 'exotic' functions?
我不知道如何为日志指定我的小数精度,导入小数和设置上下文不会影响日志功能。
from decimal import *
getcontext().prec = 54
print(Decimal(197)/ Decimal(83))
2.37349397590361445783132530120481927710843373493975904
print(math.log(Decimal(197)))
5.2832037287379885
我想为分数以外的函数设置高精度。 Python 3 顺便说一句。
可能的解决方案:
字符串格式:
- f-strings 的好处是不需要导入另一个包
- f-Strings: A New and Improved Way to Format Strings in Python
- 将显示的小数位扩展到与
decimal
模块相同的范围
x = 4567.09710599898797936589076897
y = 2445.89790870380808990080797897
加长:
print(f'{(x/y):.054f}')
>>> 1.867247643389702504990168563381303101778030395507812500
calculation = math.log(197)
print(f'{calculation:.050f}')
>>> 5.28320372873798849155946300015784800052642822265625
缩短:
print(f'{(x/y):.02f}')
>>> 1.87
numpy
:
缩短:
print(np.round(x/y, 2))
>>> 1.87
加长:
numpy
不会扩展显示的精度,超出 python 显示的精度。
print(np.around(x/y, 54))
>>> 1.8672476433897025
print(x/y)
>>> 1.8672476433897025
decimal
模块:
问题示例:
print(math.log(197))
>>> 5.2832037287379885
print(math.log(Decimal(197.0)))
>>> 5.2832037287379885
print(Decimal(math.log(197)))
>>> 5.28320372873798849155946300015784800052642822265625
print(Decimal(197).ln())
>>> 5.283203728737988506779797329
print(f'{math.log(197):.050f}')
>>> 5.28320372873798849155946300015784800052642822265625
备注:
- 在写入日志之前,可以使用任何一种方法将数字格式化为所需的小数位。
- 警告:由于数字在计算机中的表示方式,我怀疑增加显示的小数位数是否会提高精度。
- 使用
f-strings
提供与使用 decimal
模块相同的最终输出精度。
我不知道如何为日志指定我的小数精度,导入小数和设置上下文不会影响日志功能。
from decimal import *
getcontext().prec = 54
print(Decimal(197)/ Decimal(83))
2.37349397590361445783132530120481927710843373493975904
print(math.log(Decimal(197)))
5.2832037287379885
我想为分数以外的函数设置高精度。 Python 3 顺便说一句。
可能的解决方案:
字符串格式:
- f-strings 的好处是不需要导入另一个包
- f-Strings: A New and Improved Way to Format Strings in Python
- 将显示的小数位扩展到与
decimal
模块相同的范围
x = 4567.09710599898797936589076897
y = 2445.89790870380808990080797897
加长:
print(f'{(x/y):.054f}')
>>> 1.867247643389702504990168563381303101778030395507812500
calculation = math.log(197)
print(f'{calculation:.050f}')
>>> 5.28320372873798849155946300015784800052642822265625
缩短:
print(f'{(x/y):.02f}')
>>> 1.87
numpy
:
缩短:
print(np.round(x/y, 2))
>>> 1.87
加长:
numpy
不会扩展显示的精度,超出 python 显示的精度。
print(np.around(x/y, 54))
>>> 1.8672476433897025
print(x/y)
>>> 1.8672476433897025
decimal
模块:
问题示例:
print(math.log(197))
>>> 5.2832037287379885
print(math.log(Decimal(197.0)))
>>> 5.2832037287379885
print(Decimal(math.log(197)))
>>> 5.28320372873798849155946300015784800052642822265625
print(Decimal(197).ln())
>>> 5.283203728737988506779797329
print(f'{math.log(197):.050f}')
>>> 5.28320372873798849155946300015784800052642822265625
备注:
- 在写入日志之前,可以使用任何一种方法将数字格式化为所需的小数位。
- 警告:由于数字在计算机中的表示方式,我怀疑增加显示的小数位数是否会提高精度。
- 使用
f-strings
提供与使用decimal
模块相同的最终输出精度。