何时使用机器epsilon何时不使用?(When to use machine epsilon and when not to?)

编程入门 行业动态 更新时间:2024-10-18 01:30:51
何时使用机器epsilon何时不使用?(When to use machine epsilon and when not to?)

我正在读一本关于渲染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.

更多推荐

本文发布于:2023-04-29 02:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机器   epsilon   machine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!