python numpy机器epsilon

问题:python numpy机器epsilon

我试图了解什么是机器epsilon。根据维基百科,可以如下计算:

def machineEpsilon(func=float):
    machine_epsilon = func(1)
    while func(1)+func(machine_epsilon) != func(1):
        machine_epsilon_last = machine_epsilon
        machine_epsilon = func(machine_epsilon) / func(2)
    return machine_epsilon_last

但是,它仅适用于双精度数字。我有兴趣修改它以支持单精度数字。我读到可以使用numpy,尤其是numpy.float32类。有人可以帮助您修改功能吗?

I am trying to understand what is machine epsilon. According to the Wikipedia, it can be calculated as follows:

def machineEpsilon(func=float):
    machine_epsilon = func(1)
    while func(1)+func(machine_epsilon) != func(1):
        machine_epsilon_last = machine_epsilon
        machine_epsilon = func(machine_epsilon) / func(2)
    return machine_epsilon_last

However, it is suitable only for double precision numbers. I am interested in modifying it to support also single precision numbers. I read that numpy can be used, particularly numpy.float32 class. Can anybody help with modifying the function?


回答 0

对于给定的float类型,获取机器epsilon的更简单方法是使用np.finfo()

print(np.finfo(float).eps)
# 2.22044604925e-16

print(np.finfo(np.float32).eps)
# 1.19209e-07

An easier way to get the machine epsilon for a given float type is to use np.finfo():

print(np.finfo(float).eps)
# 2.22044604925e-16

print(np.finfo(np.float32).eps)
# 1.19209e-07

回答 1

获得epsilon的另一种简单方法是:

In [1]: 7./3 - 4./3 -1
Out[1]: 2.220446049250313e-16

Another easy way to get epsilon is:

In [1]: 7./3 - 4./3 -1
Out[1]: 2.220446049250313e-16

回答 2

正如David指出的那样,它已经可以工作了!

>>> def machineEpsilon(func=float):
...     machine_epsilon = func(1)
...     while func(1)+func(machine_epsilon) != func(1):
...         machine_epsilon_last = machine_epsilon
...         machine_epsilon = func(machine_epsilon) / func(2)
...     return machine_epsilon_last
... 
>>> machineEpsilon(float)
2.220446049250313e-16
>>> import numpy
>>> machineEpsilon(numpy.float64)
2.2204460492503131e-16
>>> machineEpsilon(numpy.float32)
1.1920929e-07

It will already work, as David pointed out!

>>> def machineEpsilon(func=float):
...     machine_epsilon = func(1)
...     while func(1)+func(machine_epsilon) != func(1):
...         machine_epsilon_last = machine_epsilon
...         machine_epsilon = func(machine_epsilon) / func(2)
...     return machine_epsilon_last
... 
>>> machineEpsilon(float)
2.220446049250313e-16
>>> import numpy
>>> machineEpsilon(numpy.float64)
2.2204460492503131e-16
>>> machineEpsilon(numpy.float32)
1.1920929e-07


Fatal error: Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in /www/wwwroot/pythondict/wp-content/plugins/wp-performance/vendor/matthiasmullie/minify/src/CSS.php:528 Stack trace: #0 /www/wwwroot/pythondict/wp-content/plugins/wp-performance/vendor/matthiasmullie/minify/src/CSS.php(528): implode() #1 /www/wwwroot/pythondict/wp-content/plugins/wp-performance/vendor/matthiasmullie/minify/src/CSS.php(314): MatthiasMullie\Minify\CSS->shortenColors() #2 /www/wwwroot/pythondict/wp-content/plugins/wp-performance/vendor/matthiasmullie/minify/src/Minify.php(111): MatthiasMullie\Minify\CSS->execute() #3 /www/wwwroot/pythondict/wp-content/plugins/wp-performance/includes/classes/minify.php(29): MatthiasMullie\Minify\Minify->minify() #4 /www/wwwroot/pythondict/wp-content/plugins/wp-performance/includes/classes/parser.php(176): WPP\Minify::code() #5 /www/wwwroot/pythondict/wp-content/plugins/wp-performance/includes/classes/parser.php(113): WPP\Parser->parseCSS() #6 /www/wwwroot/pythondict/wp-content/plugins/wp-performance/includes/classes/parser.php(35): WPP\Parser->parseTemplate() #7 /www/wwwroot/pythondict/wp-content/plugins/wp-performance/includes/classes/parser.php(60): WPP\Parser->__construct() #8 [internal function]: WPP\Parser::init() #9 /www/wwwroot/pythondict/wp-includes/functions.php(5481): ob_end_flush() #10 /www/wwwroot/pythondict/wp-includes/class-wp-hook.php(341): wp_ob_end_flush_all() #11 /www/wwwroot/pythondict/wp-includes/class-wp-hook.php(365): WP_Hook->apply_filters() #12 /www/wwwroot/pythondict/wp-includes/plugin.php(522): WP_Hook->do_action() #13 /www/wwwroot/pythondict/wp-includes/load.php(1308): do_action() #14 [internal function]: shutdown_action_hook() #15 {main} thrown in /www/wwwroot/pythondict/wp-content/plugins/wp-performance/vendor/matthiasmullie/minify/src/CSS.php on line 528