Python中的定点数据类型。
项目描述
FixedPoint
FixedPoint许可协议为ZPL 2.1
版权2007-2008由gocept gmbh & co. kg所有
FixedPoint使用方法
>>> from gocept.fixedpoint import FixedPoint
FixedPoint对象支持小数点后具有固定位数(称为对象的精度)的十进制算术。小数点前的位数是可变的,且没有限制。
当创建FixedPoint时,可以在每个对象的基础上设置精度,并且FixedPoint对象的精度可能不同。也可以通过FixedPoint.set_precision(p)在构造后更改精度。请注意,如果通过set_precision减少FixedPoint的精度,可能会因为四舍五入而丢失信息。
>>> x = FixedPoint("5.55") # precision defaults to 2 >>> print x 5.55 >>> x.set_precision(1) # round to one fraction digit >>> print x 5.6 >>> print FixedPoint("5.55", 1) # same thing setting to 1 in constructor 5.6 >>> repr(x) # returns constructor string that reproduces object exactly "FixedPoint('5.6', 1)"
当通过+ - * /将不同精度的FixedPoint对象组合时,结果将计算为输入精度较大者,该精度也将成为结果FixedPoint对象的精度。
>>> print FixedPoint("3.42") + FixedPoint("100.005", 3) 103.425 >>> print FixedPoint("2.1") * FixedPoint("10.995", 3) 23.090
当FixedPoint与其他数值类型(整数、浮点数、表示数字的字符串)通过+ - * /组合时,计算将以FixedPoint的精度进行,并且结果继承FixedPoint的精度。
>>> print FixedPoint(1) / 7 0.14 >>> print FixedPoint(1, 30) / 7 0.142857142857142857142857142857 >>>
由str(x)(通过“print”隐式调用)产生的字符串始终包含至少一个小数点前的数字,然后是小数点,然后是x.get_precision()个确切数字。如果x是负数,则str(x)[0] == “-”。
>>> print FixedPoint("1.0", 5) 1.00000 >>> print FixedPoint("1.234567", 2) 1.23 >>> str(FixedPoint("-1.45"))[0] == "-" True
FixedPoint 构造函数可以接受 int、long、string、float、FixedPoint 或任何可以通过 float() 或 long() 转换为 float 或 long 的对象。传递精度是可选的;如果指定,精度必须是非负整数。精度的尺寸没有固有的限制,但如果非常大,可能会耗尽内存。
>>> FixedPoint("1.0", -3) # negative precision values are not allowed Traceback (most recent call last): ... ValueError: precision must be >= 0: -3
请注意,float 转换为 FixedPoint 可能会令人惊讶,因此在可能的情况下应避免。从字符串转换是精确的(直到最终四舍五入到请求的精度),因此更受欢迎。
>>> print FixedPoint(1.1e30) 1099999999999999993725589651456.00 >>> print FixedPoint("1.1e30") 1100000000000000000000000000000.00 >>>
以下 Python 操作符和函数以预期的方式接受 FixedPoint
二元 + - * / % divmod 与自动将其他类型转换为 FixedPoint。FixedPoint 的 + - % divmod 总是精确的。* / 可能会因舍入而丢失信息,在这种情况下,结果是无限精确的答案舍入到结果的精度。
divmod(x, y) 返回 (q, r),其中 q 是一个 long,等于 floor(x/y)(就像 x/y 以无限精度计算一样),r 是一个等于 x - q * y 的 FixedPoint;没有信息丢失。请注意,q 有 y 的符号,且 abs(r) < abs(y)。
一元 -
== != < > <= >= cmp
min, max
float, int, long(int 和 long 截断)
abs
str, repr
hash
作为字典键使用
作为布尔值使用(例如,“if some_FixedPoint:” – 如果不为零则为真)
- FixedPoint 独有的方法
copy(): 返回具有相同值的新的 FixedPoint
frac(): long(x) + x.frac() == x
get_precision(): 返回此 FixedPoint 对象的精度(p)
set_precision(p): 设置此 FixedPoint 对象的精度
>>> FixedPoint("1.0", 3).copy() FixedPoint('1.000', 3) >>> FixedPoint("1.0").copy() # default precision is 2 FixedPoint('1.00', 2)
>>> FixedPoint('123.45').frac() FixedPoint('0.45', 2)
>>> FixedPoint('123').get_precision() 2 >>> fp = FixedPoint('123') >>> fp.set_precision(5) >>> fp FixedPoint('123.00000', 5)
测试几个操作符
>>> fp = FixedPoint >>> o = fp("0.1") >>> str(o) == "0.10" True >>> t = fp("-20e-2", 5) >>> str(t) == "-0.20000" True >>> t < o True >>> o > t True >>> min(o, t) == min(t, o) == t True >>> max(o, t) == max(t, o) == o True >>> o != t True >>> --t == t True >>> abs(t) > abs(o) True >>> abs(o) < abs(t) True >>> o == o and t == t True >>> t.copy() == t True >>> o == -t/2 == -.5 * t True >>> abs(t) == o + o True >>> abs(o) == o True >>> o/t == -0.5 True >>> -(t/o) == (-t)/o == t/-o == 2 True >>> 1 + o == o + 1 == fp(" +00.000011e+5 ") True >>> 1/o == 10 True >>> o + t == t + o == -o True >>> 2.0 * t == t * 2 == "2" * t == o/o * 2L * t True >>> 1 - t == -(t - 1) == fp(6L)/5 True >>> t*t == 4*o*o == o*4*o == o*o*4 True >>> fp(2) - "1" == 1 True >>> float(-1/t) == 5.0 True >>> 1/(42 + fp("1e-20", 20) - 42) == fp("100.0E18") True >>> o = fp(".9995", 4) >>> 1 - o == fp("5e-4", 10) True >>> o.set_precision(3) >>> o == 1 True >>> o = fp(".9985", 4) >>> o.set_precision(3) >>> o == fp(".998", 10) True >>> o == o.frac() True >>> o.set_precision(100) >>> o == fp(".998", 10) True >>> o.set_precision(2) >>> o == 1 True >>> x = fp(1.99) >>> long(x) == -long(-x) == 1L True >>> int(x) == -int(-x) == 1 True >>> x == long(x) + x.frac() True >>> -x == long(-x) + (-x).frac() True >>> fp(7) % 4 == 7 % fp(4) == 3 True >>> fp(-7) % 4 == -7 % fp(4) == 1 True >>> fp(-7) % -4 == -7 % fp(-4) == -3 True >>> fp(7.0) % "-4.0" == 7 % fp(-4) == -1 True >>> fp("5.5") % fp("1.1") == fp("5.5e100") % fp("1.1e100") == 0 True >>> divmod(fp("1e100"), 3) == (long(fp("1e100")/3), 1) True >>> fp("1") != '' True
更改
0.2 (2008-05-26)
将源移动到 __init__.py 以使其更易于使用
更新 pypi 元数据
0.1 (2007-11-27)
初始发布