我正在读一本关于渲染3D图形的书,作者有时会使用epsilon,有时则不会。
注意if开头使用epsilon,而另一个使用epsilon。
这背后的逻辑是什么? 我可以看到他避免任何被零除的机会,但是当在函数中不使用epsilon时,它仍然有可能返回一个值,该值将使外码除以零。
顺便提一下,Book是Real-Time Rendering 3rd Edition。
I'm reading a book about rendering 3d graphics and the author sometimes uses epsilon and sometimes doesn't.
Notice the if at the beginning using epsilon and the other ifs that don't.
What's the logic behind this? I can see he avoids any chance for division by zero but when not using epsilon in the function there's still a chance it will return a value that will make the outer code to divide by zero.
Book is Real-Time Rendering 3rd Edition, by the way.
最满意答案
第一个语句, if(|f| > ϵ)只是检查以确保f与0显着不同。在代码中的特定位置执行此操作非常重要,因为接下来的两个语句除以f 。
其他陈述不需要这样做,所以他们不需要使用ϵ 。
例如,
if(t1 > t2) swap(t1, t2);是一个自包含的语句,它将两个数字相互比较,如果错误的数字更大,则交换它们。 由于不进行比较以查看值是否接近0,因此无需使用ϵ 。
如果从这个代码块返回的值可以使调用代码除以零,那么应该在调用代码中处理。
The first statement, if(|f| > ϵ) is just checking to make sure f is significantly different from 0. It's important to do that in that specific spot in the code because the next two statements divide by f.
The other statements don't need to do that, so they don't need to use ϵ.
For example,
if(t1 > t2) swap(t1, t2);is a self-contained statement that compares two numbers to each other and swaps them if the wrong one is greater. Since it's not comparing to see if a value is close to 0, there's no need to use ϵ.
If the value that is returned from this block of code can make the calling code divide by zero, that should be handled in the calling code.
更多推荐
发布评论