python中的fixedpoint数据类型。

gocept.fixedpoint的Python项目详细描述


固定点

FixedPoint是根据ZPL 2.1授权的

Gocept GmbH&Co.KG版权所有2007-2008

定点使用

>>> from gocept.fixedpoint import FixedPoint

FixedPoint对象支持固定数量的十进制算法 小数点后的数字(称为对象的精度)。这个 小数点前的位数是可变的和无限的。

当一个固定点 是构造的,并且可能因固定点对象而异。精确性 也可在施工后通过定点进行更改。设置精度(P)。 注意,如果通过设置精度降低固定点的精度, 信息可能因舍入而丢失。

>>> 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对象的精度。

>>> print FixedPoint("3.42") + FixedPoint("100.005", 3)
103.425
>>> print FixedPoint("2.1") * FixedPoint("10.995", 3)
23.090

当FixedPoint与其他数值类型(int、float、float和 表示数字的字符串)通过+-*/,然后类似地计算 使用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()转换为float或 long via 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运算符和函数接受 预期方式:

  • binary + - * / % divmod with auto-coercion of other types to FixedPoint. + - % divmod of FixedPoints are always exact.* / of FixedPoints may lose information to rounding, in which case the result is the infinitely precise answer rounded to the result’s precision.
  • divmod(x, y) returns (q, r) where q is a long equal to floor(x/y) as if x/y were computed to infinite precision, and r is a FixedPoint equal to x - q * y; no information is lost. Note that q has the sign of y, and abs(r) < abs(y).
  • unary -
  • == != < > <= >= cmp
  • min, max
  • float, int, long (int and long truncate)
  • abs
  • str, repr
  • hash
  • use as dict keys
  • use as boolean (e.g. “if some_FixedPoint:” – true iff not zero)
固定点特有的方法:
  • copy():返回具有相同值的新定点
  • frac():长(x)+x.frac()==x
  • get_precision():返回此定点对象的精度(p)
  • set_precision(p):设置此固定点对象的精度
>>> 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)

  • 将源移动到“初始化”py以使其更可用
  • 更新了PYPI元数据

0.1(2007-11-27)

  • 初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
为Java排序一个好的排序列表   java如何识别最后一行,然后单击Add按钮。。根据人们给出的建议,点击第二行想要点击最后一行任何潜在客户吗   收集器(.stream()。独特的()。collect(Collectors.toList());)在java中从eclipse成功运行并从命令提示符运行?   java致命异常:安卓的后台任务。数据库sqlite。SQLiteException:没有这样的表   java如何创建生成随机特殊字符的方法   java SQL只插入到特定列中   java Hibernate 5.2混合本机和JPA API   在Android应用程序中使用Java获取特定的XML标记文本   java之间的通道差异。addPeer()和channel。joinPeer()   java关闭对serialVersionUID的检查   java如何在不设置Id的情况下处理新的实体对象?   用于afterJob(JobExecution JobExecution)的java Spring批处理将参数从编写器传递到侦听器   java如何通过OSGi声明性服务声明hasa依赖关系?   java Android捕获在警报对话框中启动的OnFocusChange或Recents   Java刽子手游戏(主要关注输出)   java消息在OpenMQ中超时后丢失   Java方法在父级中的占位符用于子级   Java AWT列表框   来自http get请求的java“格式错误的JSON”