python十进制转九进制_9.4. decimal — 十进制定点和浮点运算 — Python 2.7.18 文档

Python的decimal模块提供高精度浮点数运算,支持十进制到九进制转换。本文介绍了如何使用Decimal构造对象,包括从字符串、元组和浮点数转换,以及其与浮点数的区别。还讨论了Decimal对象的比较、算术运算、特殊方法如exp、ln、log10等,并展示了与浮点数的不兼容性。
摘要由CSDN通过智能技术生成

根据 value 构造一个新的 Decimal 对象。

value can be an integer, string, tuple, float, or another Decimal

object. If no value is given, returns Decimal('0'). If value is a

string, it should conform to the decimal numeric string syntax after leading

and trailing whitespace characters are removed:

sign ::= '+' | '-'

digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

indicator ::= 'e' | 'E'

digits ::= digit [digit]...

decimal-part ::= digits '.' [digits] | ['.'] digits

exponent-part ::= indicator [sign] digits

infinity ::= 'Infinity' | 'Inf'

nan ::= 'NaN' [digits] | 'sNaN' [digits]

numeric-value ::= decimal-part [exponent-part] | infinity

numeric-string ::= [sign] numeric-value | [sign] nan

If value is a unicode string then other Unicode decimal digits

are also permitted where digit appears above. These include

decimal digits from various other alphabets (for example,

Arabic-Indic and Devanāgarī digits) along with the fullwidth digits

u'\uff10' through u'\uff19'.

如果 value 是一个 tuple ,它应该有三个组件,一个符号( 0 表示正数或 1 表示负数),一个数字的 tuple 和整数指数。 例如, Decimal((0, (1, 4, 1, 4), -3)) 返回 Decimal('1.414')。

如果 value 是 float ,则二进制浮点值无损地转换为其精确的十进制等效值。 此转换通常需要53位或更多位数的精度。 例如, Decimal(float('1.1')) 转换为``Decimal(‘1.100000000000000088817841970012523233890533447265625’)``。

context 精度不会影响存储的位数。 这完全由 value 中的位数决定。 例如,Decimal('3.00000') 记录所有五个零,即使上下文精度只有三。

context 参数的目的是确定 value 是格式错误的字符串时该怎么做。 如果上下文陷阱 InvalidOperation,则引发异常;否则,构造函数返回一个新的 Decimal,其值为 NaN。

构造完成后, Decimal 对象是不可变的。

在 2.6 版更改:leading and trailing whitespace characters are permitted when

creating a Decimal instance from a string.

在 2.7 版更改:现在允许构造函数的参数为 float 实例。

Decimal floating point objects share many properties with the other built-in

numeric types such as float and int. All of the usual math

operations and special methods apply. Likewise, decimal objects can be

copied, pickled, printed, used as dictionary keys, used as set elements,

compared, sorted, and coerced to another type (such as float or

long).

算术对十进制对象和算术对整数和浮点数有一些小的差别。 当余数运算符 % 应用于Decimal对象时,结果的符号是 被除数 的符号,而不是除数的符号:

>>>(-7) % 4

1

>>>Decimal(-7) % Decimal(4)

Decimal('-3')

整数除法运算符 // 的行为类似,返回真商的整数部分(截断为零)而不是它的向下取整,以便保留通常的标识 x == (x // y) * y + x % y:

>>>-7 // 4

-2

>>>Decimal(-7) // Decimal(4)

Decimal('-1')

% 和 // 运算符实现了 remainder 和 divide-integer 操作(分别),如规范中所述。

Decimal objects cannot generally be combined with floats in

arithmetic operations: an attempt to add a Decimal to a

float, for example, will raise a TypeError.

There’s one exception to this rule: it’s possible to use Python’s

comparison operators to compare a float instance x

with a Decimal instance y. Without this exception,

comparisons between Decimal and float instances

would follow the general rules for comparing objects of different

types described in the 表达式 section of the reference

manual, leading to confusing results.

在 2.7 版更改:A comparison between a float instance x and a

Decimal instance y now returns a result based on

the values of x and y. In earlier versions x < y

returned the same (arbitrary) result for any Decimal

instance x and any float instance y.

除了标准的数字属性,十进制浮点对象还有许多专门的方法:

adjusted()¶

在移出系数最右边的数字之后返回调整后的指数,直到只剩下前导数字:Decimal('321e+5').adjusted() 返回 7 。 用于确定最高有效位相对于小数点的位置。

as_tuple()¶

返回一个 named tuple 表示的数字: DecimalTuple(sign, digits, exponent)。

在 2.6 版更改:Use a named tuple.

canonical()¶

返回参数的规范编码。 目前,一个 Decimal 实例的编码始终是规范的,因此该操作返回其参数不变。

2.6 新版功能.

compare(other[, context])¶

Compare the values of two Decimal instances. This operation behaves in

the same way as the usual comparison method __cmp__(), except that

compare() returns a Decimal instance rather than an integer, and if

either operand is a NaN then the result is a NaN:

a or b is a NaN ==> Decimal('NaN')

a < b ==> Decimal('-1')

a == b ==> Decimal('0')

a > b ==> Decimal('1')

compare_signal(other[, context])¶

除了所有 NaN 信号之外,此操作与 compare() 方法相同。 也就是说,如果两个操作数都不是信令NaN,那么任何静默的 NaN 操作数都被视为信令NaN。

2.6 新版功能.

compare_total(other)¶

使用它们的抽象表示而不是它们的数值来比较两个操作数。 类似于 compare() 方法,但结果给出了一个总排序 Decimal 实例。 两个 Decimal 实例具有相同的数值但不同的表示形式在此排序中比较不相等:

>>>Decimal('12.0').compare_total(Decimal('12'))

Decimal('-1')

静默和发出信号的 NaN 也包括在总排序中。 这个函数的结果是 Decimal('0') 如果两个操作数具有相同的表示,或是 Decimal('-1') 如果第一个操作数的总顺序低于第二个操作数,或是 Decimal('1') 如果第一个操作数在总顺序中高于第二个操作数。 有关总排序的详细信息,请参阅规范。

2.6 新版功能.

compare_total_mag(other)¶

比较两个操作数使用它们的抽象表示而不是它们的值,如 compare_total(),但忽略每个操作数的符号。 x.compare_total_mag(y) 相当于 x.copy_abs().compare_total(y.copy_abs())。

2.6 新版功能.

conjugate()¶

只返回self,这种方法只符合 Decimal 规范。

2.6 新版功能.

copy_abs()¶

返回参数的绝对值。 此操作不受上下文影响并且是静默的:没有更改标志且不执行舍入。

2.6 新版功能.

copy_negate()¶

回到参数的否定。 此操作不受上下文影响并且是静默的:没有标志更改且不执行舍入。

2.6 新版功能.

copy_sign(other)¶

返回第一个操作数的副本,其符号设置为与第二个操作数的符号相同。 例如:

>>>Decimal('2.3').copy_sign(Decimal('-1.5'))

Decimal('-2.3')

This operation is unaffected by the context and is quiet: no flags are

changed and no rounding is performed.

2.6 新版功能.

exp([context])¶

返回给定数字的(自然)指数函数``e**x``的值。结果使用 ROUND_HALF_EVEN 舍入模式正确舍入。

>>>Decimal(1).exp()

Decimal('2.718281828459045235360287471')

>>>Decimal(321).exp()

Decimal('2.561702493119680037517373933E+139')

2.6 新版功能.

from_float(f)¶

将浮点数转换为十进制数的类方法。

注意, Decimal.from_float(0.1) 与 Decimal(‘0.1’) 不同。 由于 0.1 在二进制浮点中不能精确表示,因此该值存储为最接近的可表示值,即 0x1.999999999999ap-4 。 十进制的等效值是`0.1000000000000000055511151231257827021181583404541015625`。

注解

From Python 2.7 onwards, a Decimal instance

can also be constructed directly from a float.

>>>Decimal.from_float(0.1)

Decimal('0.1000000000000000055511151231257827021181583404541015625')

>>>Decimal.from_float(float('nan'))

Decimal('NaN')

>>>Decimal.from_float(float('inf'))

Decimal('Infinity')

>>>Decimal.from_float(float('-inf'))

Decimal('-Infinity')

2.7 新版功能.

fma(other, third[, context])¶

混合乘法加法。 返回 self*other+third ,中间乘积 self*other 没有四舍五入。

>>>Decimal(2).fma(3, 5)

Decimal('11')

2.6 新版功能.

is_canonical()¶

如果参数是规范的,则为返回 True,否则为 False 。 目前,Decimal 实例总是规范的,所以这个操作总是返回 True 。

2.6 新版功能.

is_finite()¶

如果参数是一个有限的数,则返回为 True ;如果参数为无穷大或 NaN ,则返回为 False。

2.6 新版功能.

is_infinite()¶

如果参数为正负无穷大,则返回为 True ,否则为 False 。

2.6 新版功能.

is_nan()¶

如果参数为 NaN (无论是否静默),则返回为 True ,否则为 False 。

2.6 新版功能.

is_normal()¶

Return True if the argument is a normal finite non-zero

number with an adjusted exponent greater than or equal to Emin.

Return False if the argument is zero, subnormal, infinite or a

NaN. Note, the term normal is used here in a different sense with

the normalize() method which is used to create canonical values.

2.6 新版功能.

is_qnan()¶

如果参数为静默 NaN,返回 True,否则返回 False。

2.6 新版功能.

is_signed()¶

如果参数带有负号,则返回为 True,否则返回 False。注意,0 和 NaN 都可带有符号。

2.6 新版功能.

is_snan()¶

如果参数为显式 NaN,则返回 True,否则返回 False。

2.6 新版功能.

is_subnormal()¶

Return True if the argument is subnormal, and False

otherwise. A number is subnormal is if it is nonzero, finite, and has an

adjusted exponent less than Emin.

2.6 新版功能.

is_zero()¶

如果参数是0(正负皆可),则返回 True,否则返回 False。

2.6 新版功能.

ln([context])¶

返回操作数的自然对数(以 e 为底)。结果是使用 ROUND_HALF_EVEN 舍入模式正确四舍五入的。

2.6 新版功能.

log10([context])¶

返回操作数的以十为底的对数。结果是使用 ROUND_HALF_EVEN 舍入模式正确四舍五入的。

2.6 新版功能.

logb([context])¶

对于一个非零数,返回其运算数的调整后指数作为一个 Decimal 实例。 如果运算数为零将返回 Decimal('-Infinity') 并且产生 the DivisionByZero 标志。如果运算数是无限大则返回 Decimal('Infinity') 。

2.6 新版功能.

logical_and(other[, context])¶

logical_and() 是需要两个 逻辑运算数 的逻辑运算(参考 逻辑操作数 )。结果是按位输出的两运算数的 “和”。

2.6 新版功能.

logical_invert([context])¶

logical_invert() 是一个逻辑运算。 结果是按位的倒转的运算数。

2.6 新版功能.

logical_or(other[, context])¶

logical_or() 是需要两个 logical operands 的逻辑运算(请参阅 逻辑操作数 )。结果是两个运算数的按位的 or 。

2.6 新版功能.

logical_xor(other[, context])¶

logical_xor() 是需要两个 逻辑运算数 的逻辑运算(参考 逻辑操作数 )。结果是按位输出的两运算数的异或运算。

2.6 新版功能.

max(other[, context])¶

像 max(self, other) 一样,除了在返回之前应用上下文舍入规则并且用信号通知或忽略 NaN 值(取决于上下文以及它们是发信号还是安静)。

max_mag(other[, context])¶

与 max() 方法相似,但是操作数使用绝对值进行比较。

2.6 新版功能.

min(other[, context])¶

像 min(self, other) 一样,除了在返回之前应用上下文舍入规则并且用信号通知或忽略 NaN 值(取决于上下文以及它们是发信号还是安静)。

min_mag(other[, context])¶

与 min() 方法相似,但是操作数使用绝对值进行比较。

2.6 新版功能.

next_minus([context])¶

返回小于给定操作数的上下文中可表示的最大数字(或者当前线程的上下文中的可表示的最大数字如果没有给定上下文)。

2.6 新版功能.

next_plus([context])¶

返回大于给定操作数的上下文中可表示的最小数字(或者当前线程的上下文中的可表示的最小数字如果没有给定上下文)。

2.6 新版功能.

next_toward(other[, context])¶

如果两运算数不相等,返回在第二个操作数的方向上最接近第一个操作数的数。如果两操作数数值上相等,返回将符号设置为与第二个运算数相同的第一个运算数的拷贝。

2.6 新版功能.

normalize([context])¶

通过去除尾随的零并将所有结果等于 Decimal('0') 的转化为 Decimal('0e0') 来标准化数字。用于为等效类的属性生成规范值。比如, Decimal('32.100') 和 Decimal('0.321000e+2') 都被标准化为相同的值 Decimal('32.1')。

number_class([context])¶

返回一个字符串描述运算数的 class 。返回值是以下十个字符串中的一个。

"-Infinity" ,指示操作数为负无穷大。

"-Normal" ,指示该操作数是负正常数字。

"-Subnormal" ,指示该操作数是负的次正规数。

"-Zero" ,指示该操作数是负零。

"-Zero" ,指示该操作数是正零。

"+Subnormal" ,指示该操作数是正的次正规数。

"+Normal" ,指示该操作数是正的正规数。

"+Infinity" ,指示该运算数是正无穷。

"NaN" ,指示该运算数是沉寂的 NaN (非数字)。

"sNaN" ,指示该运算数是信号 NaN 。

2.6 新版功能.

quantize(exp[, rounding[, context[, watchexp]]])¶

返回的值等于四舍五入的第一个运算数并且具有第二个操作数的指数。

>>>Decimal('1.41421356').quantize(Decimal('1.000'))

Decimal('1.414')

与其他运算不同,如果量化运算后的系数长度大于精度,那么会发出一个 InvalidOperation 信号。这保证了除非有一个错误情况,量化指数恒等于右手运算数的指数。

与其他运算不同,量化永不信号下溢,即使结果不正常且不精确。

如果第二个运算数的指数大于第一个运算数的指数那或许需要四舍五入。在这种情况下,舍入模式由给定 rounding 参数决定,其余的由给定 context 参数决定;如果参数都未给定,使用当前线程上下文的舍入模式。

If watchexp is set (default), then an error is returned whenever the

resulting exponent is greater than Emax or less than

Etiny.

radix()¶

返回 Decimal(10),即 Decimal 类进行所有算术运算所用的数制(基数)。 这是为保持与规范描述的兼容性而加入的。

2.6 新版功能.

remainder_near(other[, context])¶

返回 self 除以 other 的余数。 这与 self % other 的区别在于所选择的余数要使其绝对值最小化。 更准确地说,返回值为 self - n * other 其中 n 是最接近 self / other 的实际值的整数,并且如果两个整数与实际值的差相等则会选择其中的偶数。

如果结果为零则其符号将为 self 的符号。

>>>Decimal(18).remainder_near(Decimal(10))

Decimal('-2')

>>>Decimal(25).remainder_near(Decimal(10))

Decimal('5')

>>>Decimal(35).remainder_near(Decimal(10))

Decimal('-5')

rotate(other[, context])¶

返回对第一个操作数的数码按第二个操作数所指定的数量进行轮转的结果。 第二个操作数必须为 -precision 至 precision 精度范围内的整数。 第二个操作数的绝对值给出要轮转的位数。 如果第二个操作数为正值则向左轮转;否则向右轮转。 如有必要第一个操作数的系数会在左侧填充零以达到 precision 所指定的长度。 第一个操作数的符号和指数保持不变。

2.6 新版功能.

same_quantum(other[, context])¶

检测自身与 other 是否具有相同的指数或是否均为 NaN。

scaleb(other[, context])¶

返回第一个操作数使用第二个操作数对指数进行调整的结果。 等价于返回第一个操作数乘以 10**other 的结果。 第二个操作数必须为整数。

2.6 新版功能.

shift(other[, context])¶

返回第一个操作数的数码按第二个操作数所指定的数量进行移位的结果。 第二个操作数必须为 -precision 至 precision 范围内的整数。 第二个操作数的绝对值给出要移动的位数。 如果第二个操作数为正值则向左移位;否则向右移位。 移入系数的数码为零。 第一个操作数的符号和指数保持不变。

2.6 新版功能.

sqrt([context])¶

返回参数的平方根精确到完整精度。

to_eng_string([context])¶

转换为字符串,如果需要指数则会使用工程标注法。

工程标注法的指数是 3 的倍数。 这会在十进制位的左边保留至多 3 个数码,并可能要求添加一至两个末尾零。

例如,此方法会将 Decimal('123E+1') 转换为 Decimal('1.23E+3')。

to_integral([rounding[, context]])¶

与 to_integral_value() 方法相同。 保留 to_integral 名称是为了与旧版本兼容。

to_integral_exact([rounding[, context]])¶

舍入到最接近的整数,发出信号 Inexact 或者如果发生舍入则相应地发出信号 Rounded。 如果给出 rounding 形参则由其确定舍入模式,否则由给定的 context 来确定。 如果没有给定任何形参则会使用当前上下文的舍入模式。

2.6 新版功能.

to_integral_value([rounding[, context]])¶

舍入到最接近的整数而不发出 Inexact 或 Rounded 信号。 如果给出 rounding 则会应用其所指定的舍入模式;否则使用所提供的 context 或当前上下文的舍入方法。

在 2.6 版更改:renamed from to_integral to to_integral_value. The old name

remains valid for compatibility.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值