python有多少库数量,为什么python十进制库不为某些输入返回指定数量的有效数字...

NB: this question is about significant figures. It is not a question about "digits after the decimal point" or anything like that.

EDIT: This question is not a duplicate of Significant figures in the decimal module. The two questions are asking about entirely different problems. I want to know why the function about does not return the desired value for a specific input. None of the answers to Significant figures in the decimal module address this question.

The following function is supposed to return a string representation of a float with the specified number of significant figures:

import decimal

def to_sigfigs(value, sigfigs):

return str(decimal.Context(prec=sigfigs).create_decimal(value))

At first glance, it seems to work:

print to_sigfigs(0.000003141592653589793, 5)

# 0.0000031416

print to_sigfigs(0.000001, 5)

# 0.0000010000

print to_sigfigs(3.141592653589793, 5)

# 3.1416

...but

print to_sigfigs(1.0, 5)

# 1

The desired output for the last expression (IOW, the 5-significant figure representation of 1.0) is the string '1.0000'. The actual output is the string '1'.

Am I misunderstanding something or is this a bug in decimal?

解决方案

Is it a bug? I don't know, I don't think the documentation is tight enough to make that determination. It certainly is a surprising result.

It is possible to fix your own function with a little more logic.

def to_sigfigs(value, sigfigs):

sign, digits, exponent = decimal.Context(prec=sigfigs).create_decimal(value).as_tuple()

if len(digits) < sigfigs:

missing = sigfigs - len(digits)

digits = digits + (0,) * missing

exponent -= missing

return str(decimal.Decimal((sign, digits, exponent)))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值