开发者

pyinterval with trvial floating point example doesn't seem to be working

开发者 https://www.devze.com 2023-03-20 15:29 出处:网络
There are probably hundreds of questions on here about floating point rou开发者_JAVA百科nding errors, and the solution is supposed to be interval arithmetic which helps keep track of them. As I\'d lik

There are probably hundreds of questions on here about floating point rou开发者_JAVA百科nding errors, and the solution is supposed to be interval arithmetic which helps keep track of them. As I'd like to experiment with this, I tried installing pyinterval.

Unfortunately, it doesn't seem to be working. For this trivial case of floating point error, I'd expect an interval with 2 numbers, one higher and one lower than the actual answer (0.001):

>>> from interval import interval
>>> interval(1.001)-1
interval([0.0009999999999998899])

After some head scratching, my best guess was that the number 1.001 is being converted to a single floating point value before being passed to the interval module, so I tried using the floating point fpu.up and fpu.down functions to force the conversion:

>>> from interval import fpu
>>> fpu.down(lambda : float("1.001")-1)
0.0009999999999998899
>>> fpu.up(lambda : float("1.001")-1)
0.0009999999999998899

Any idea what is wrong? Or have I got the wrong end of the stick on how this is supposed to work?

I'm using an x86 mac with "Python 2.7.1 (r271:86832, Jan 1 2011, 22:28:41) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin"


It looks like the conversion of strings to floats is not respecting the rounding, but if I do it manually by entering 1.001 as 1001/1000, I get the sort of thing I am expecting:

>>> (interval[1001]/1000)-1
interval([0.0009999999999996678, 0.001000000000000112])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号