英文:
Which is better for testing for non finite floats IsNaN or IsInf?
问题
我需要测试非有限浮点数并将其排除。我计划使用 math.IsInf()
来测试浮点数,但我看到有些人使用 math.IsNaN()
来实现这个目的。其中一个函数比另一个更适合这个目的吗?如果是的话,为什么?
编辑:这个问题因为不清楚而被搁置,所以这里提供更多信息,希望能澄清问题。我正在做《Go程序设计语言》中的练习3.1,参考了这个程序。练习要求:
如果函数f返回一个非有限的float64值,SVG文件将包含无效的
元素(尽管许多SVG渲染器可以优雅地处理这个问题)。修改程序以跳过无效的多边形。
我计划通过在corner函数中添加以下代码来解决这个问题:
if math.IsInf(z, 0) {
return math.NaN(), math.NaN()
}
并且将main函数中第二个for循环的内容更改为:
ax, ay := corner(i+1, j)
if math.IsNaN(ax) {
continue
}
bx, by := corner(i, j)
if math.IsNaN(bx) {
continue
}
cx, cy := corner(i, j+1)
if math.IsNaN(cx) {
continue
}
dx, dy := corner(i+1, j+1)
if math.IsNaN(dx) {
continue
}
fmt.Printf("<polygon points='%g,%g %g,%g %g,%g %g,%g'/>\n",
ax, ay, bx, by, cx, cy, dx, dy)
我想检查一下我的工作,所以决定查找其他人在网上发布的解决此问题的答案。我找到的其他人都没有在解决方案中使用 math.IsInf()
,而是大多数人使用了 math.IsNaN()
。这让我想知道是否我漏掉了什么,以及是否 math.IsNaN()
由于某种原因更适合这个目的。所以我查阅了 math.IsInf()
和 math.IsNaN()
的Go文档。我在维基百科和IEEE 754上查找了NaN的相关信息。我还进行了一般的网络搜索,了解为什么其他人都在使用 math.IsNaN()
,尽管对我来说似乎不太直观。然后我在这里和stackoverflow上进行了搜索,但在所有这些之后,我并没有得到一个确切的答案,所以我决定发布一个问题。
英文:
I need to test for non finite floats and eliminate them. I was planing to use math.IsInf()
to test the floats but I have seen some ppl using math.IsNaN()
for this purpose. Is one of these better for this purpose than the other ? and if so why?
edit: This has been put on hold because it is unclear so here is more information that will hopefully clarify the question. I was doing exercise 3.1 from "The Go Programming Language" which references this program. The exercise it asks
> If the function f returns a non-finite float64 value, the SVG file will > contain invalid <polygon> elements (although many SVG renderers handle > this gracefully). Modify the program to skip invalid polygons.
I was planing to solve it by adding the following to the corner func
if math.IsInf(z, 0) {
return math.NaN(), math.NaN()
}
and changing the contents of the second for loop in main to
ax, ay := corner(i+1, j)
if math.IsNaN(ax) {
continue
}
bx, by := corner(i, j)
if math.IsNaN(bx) {
continue
}
cx, cy := corner(i, j+1)
if math.IsNaN(cx) {
continue
}
dx, dy := corner(i+1, j+1)
if math.IsNaN(dx) {
continue
}
fmt.Printf("<polygon points='%g,%g %g,%g %g,%g %g,%g'/>\n",
ax, ay, bx, by, cx, cy, dx, dy)
I wanted to check my work so I decided to look up any answers other ppl had posted online to this problem. No one else that I found had used math.IsInf()
in there solutions but most had used math.IsNaN()
. This made me wonder if I was missing some something and if math.IsNaN()
was better for this purpose for some reason. So I looked through the Go Docs for both functions. I looked up NaN on wikipedia and the IEEE 754. I did general web searches for why everyone else was using math.IsNaN()
even though it seemed less intuitive to me. Then I did searches on here and on stackoverflow for answers after all of that I didn't really have an answer so I decided to post a question.
答案1
得分: 6
如果你只需要考虑无穷大,那么math.IsInf()
就足够了。然而,如果你需要防范无穷大和非数字的值,你应该同时使用这两个函数。
关于浮点数的更多阅读:https://en.wikipedia.org/wiki/IEEE_754
math.IsNaN()
在处理无穷大时的一个例子:https://play.golang.org/p/blHjr8i7p9
英文:
If you only need to account for either infinities, then math.IsInf()
should suffice. However, if you need to guard against both infinities and values that are not numbers, you should use both in conjunction.
For more reading on floats: https://en.wikipedia.org/wiki/IEEE_754
An example of math.IsNaN()
not working for infinite values: https://play.golang.org/p/blHjr8i7p9
答案2
得分: -3
数学上来说,在编程中存在NaN是很奇怪的,因为无效的表达式不应该编译通过(不幸的是,它们确实可以)。
如果你的数字无法在实数线上表示(例如sqrt(-1)),或者在数学上是未定义的(例如0/0),可以使用math.IsNaN()
。
如果你的数字可能非常大(无论是正方向还是负方向),以至于你没有足够的位数来表示它,或者它实际上应该是无穷大,可以使用math.IsInf()
。
英文:
Mathematically speaking, it's weird that NaN is a thing in programming because invalid expressions should not compile (unfortunately they do however).
Use math.IsNaN()
if your number won't fit on the real line (like sqrt(-1), for example) or if it's mathematically undefined (like 0/0).
Use math.IsInf()
if your number might be really large (in either the positive or negative direction) such that you don't have enough bits to represent it, or if it actually should be infinity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论