python中epsilon什么意思_Python的epsilon值是否正确?Machine epsilon is defined as the smallest number that, when added to one, yields a result different from one
在Python中,epsilon可以使⽤sys.float_info.epsilon到,并返回⼀个相当于2^-52的值。但是,我可以将⼤于2^-53的任何数字加到1,仍然得到与1不同的结果。
但是根据上⾯对epsilon的定义,加上任何⼩于epsilon的值就应该得到epsilon。
这是否意味着sys.float_info.epsilon返回的值不正确,或者Python正在使⽤epsilon的其他定义?
下⾯的代码说明了这⼀点,浮点数以⼗六进制格式打印出来。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
结果输出是epsilon=2.22045e-16
epsilon(hex) = 0x1.0000000000000p-52网页float是什么意思
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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。