python中epsilon什么意思_Python的epsilon值是否正确? (Is Python's epsilon value correct?)

2014-04-21 02:41:18

0

According to Wikipedia:

Machine epsilon is defined as the smallest number that, when added to one, yields a result different from one

In Python, epsilon can be found using sys.float_info.epsilon and returns a value equivalent to 2^-52. However, I can add any number greater than 2^-53 to 1 and still get a result different to one.

But by the above definition of epsilon, adding any value less than epsilon to one should give one.

Does this mean that sys.float_info.epsilon is returning an incorrect value, or that Python is using some other definition of epsilon?

The following code illustrates this, with the floating point numbers printed out in hex format.

import sys

import numpy

print 'epsilon=%g' % sys.float_info.epsilon

# output: 2.22045e-16

epsilon = sys.float_info.epsilon

print 'epsilon(hex) = %s' % float.hex(epsilon)

# output: 0x1.0000000000000p-52

one = numpy.float64(1.0)

delta = float.fromhex('0x1.fffffffffffffp-53')

print 'delta = %s' % float.hex(delta)

print 'epsilon - delta = %s' % (float.hex(epsilon-delta))

#output: 0x1.0000000000000p-105

print '\n1.0 + epsilon = %s' % (float.hex(one+numpy.float64(epsilon)))

#output: 0x1.0000000000001p+0

print '\n1.0 + delta = %s' % (float.hex(one+numpy.float64(delta)))

#output: 0x1.0000000000001p+0

# since delta is smaller than epsilon, I expected 0x1.0000000000001p+0

delta1 = float.fromhex('0x1.0000000000001p-53')

print '\n1.0 + %s = %s' % (float.hex(delta1), float.hex(one+delta1))

#output: 0x1.0000000000001p+0

# since delta is smaller than epsilon, I expected 0x1.0000000000001p+0

delta2 = float.fromhex('0x1.0000000000000p-53')

# note: delta2 = epsilon / 2.0

print '\n1.0 + %s = %s' % (float.hex(delta2), float.hex(one+delta2))

# 0x1.0000000000000p+0

The resulting output is

epsilon=2.22045e-16

epsilon(hex) = 0x1.0000000000000p-52

delta = 0x1.fffffffffffffp-53

epsilon - delta = 0x1.0000000000000p-105

1.0 + epsilon = 0x1.0000000000001p+0

1.0 + delta = 0x1.0000000000001p+0

1.0 + 0x1.0000000000001p-53 = 0x1.0000000000001p+0

1.0 + 0x1.0000000000000p-53 = 0x1.0000000000000p+0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值