问题描述
在Go中获得机器epsilon的最简单方法是什么?浮点数的其他方面(例如精度,最小指数,最大指数,摆动等)又如何呢?
What is the easiest way to get the machine epsilon in Go? What about other aspects of floating point numbers such as precision, min exponent, max exponent, wobble, etc?
我意识到,有一个math/const包,其中包含针对不同浮点类型的max和min( http://golang.org/src/pkg/math/const.go ),但没有其他信息.
I realize that there is the math/const package with the max and min for the different float types (http://golang.org/src/pkg/math/const.go), but no other information.
我想知道的一个原因是,对于给定的计算量,我可以验证该机器可以达到的最高精度,这样我就不会早退,也不会尝试更长的时间.
One reason I would like to know is to verify that I've reached the maximum precision for a given calculation that the machine can do so that I don't quit to early or try longer then needed.
另一个只是出于好奇.
谢谢
出于乐趣,我在学校里查阅了一些笔记,以了解如何手动计算epsilon的乐趣,这是c ++的粗略翻译 http://play.golang.org/p/XOXwIdNfsa 享受
For the fun I looked up in some notes from school on how to calculate the epsilon manually for fun and here is a rough translation from c++ http://play.golang.org/p/XOXwIdNfsa enjoy
来自下方的评论(感谢您使用一种更惯用的方式来找到epsilon):
comment from below (thanks for a more idiomatic way of finding epsilon):
使用epsilon := math.Nextafter(1, 2) - 1
游乐场 – 3月5日8:07
Use epsilon := math.Nextafter(1, 2) - 1
Playground – Nick Craig-Wood Mar 5 at 8:07
推荐答案
未定义,我发现问题已在问题跟踪器上解决为按预期工作":
It's not defined, I found the issue resolved as "working as intended" on the issue tracker:
https://code.google.com/p/go/issues/detail?id = 966
建议似乎是使用math.Nextafter
来导出所需的值.
The suggestion seems to be to use math.Nextafter
to derive the value if you need it.
具体来说,公式为math.Nextafter(1.0,2.0)-1.0
(第二个参数可以是大于1.0的任何数字).
Specifically, the formula is math.Nextafter(1.0,2.0)-1.0
(the second argument can be any number greater than 1.0).
这篇关于在Go中获得机器epsilon的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!