float 不大于、小于或等于 0,但也不是 NaN,也不是 Infinity

发布于 2025-01-16 11:43:28 字数 1401 浏览 2 评论 0原文

从标题来看,我有一个着色器,其中我的浮点数显然不是 NaN,显然不是 Infinity,并且显然不等于、大于或小于 0。什么?

生成这个浮点数的函数是这样的:

float ray1(vec3 rayO, vec3 rayD){
    
    float a = rayD.x;
    float b = rayD.y;
    float c = rayD.z;
    
    float x0 = rayO.x;
    float y0 = rayO.y;
    float z0 = rayO.z;
    
    
    float alpha = 0.1 * ((a * a * c) + (b * b * c));
    float beta = 0.1 * ((a * a * z0) + (2. * x0 * a * c) + (b * b * z0) + (2. * y0 * b * c));
    float gamma = 0.1 * ((2. * x0 * z0 * a) + (x0 * x0 * c) + (2. * y0 * z0 * b) + (y0 * y0 *c)) + (0.6  * c);
    float rho = 0.1 * ((z0 * x0 * x0) + (z0 * y0 * y0)) + (0.6 * z0) + 1.;
    
    float P = -beta / (3. * alpha);
    float Q = (P * P * P) + (((beta * gamma) - (3. * alpha * rho)) / (6. * (alpha * alpha)));
    float R = gamma / (3. * alpha);
    
    float M = (Q * Q) + pow(pow(R - (P * P), 3.), .5);
    
    float t = pow(Q + M, .333) + pow(Q - M, .333) + P;
    
    return t;
}

我将这个传递给它:

float t0 = ray1(vec3(0.), vec3(1.));

当我用这些 if 中的任何一个(一次一个)检查 t0 是什么时:

if(isnan(t0)){
    col = vec4(.5);
}
if(t0 <= 0.){
    col = vec4(.5);
}
if(t0 >= 0.){
    col = vec4(.5);
}
if(isinf(t0)){
    col = vec4(.5);
}

它们都返回 false。

t0 是什么?为什么会发生这种情况?

更新:我相当确定问题在于取 Q 的立方根,因为当我 return pow(Q, .333); 时,它会给出相同的结果。

From the title, I have a shader where my a floating point number is apparently not NaN, is apparently not Ininity, and is apparently not equal to, greater than, or lesser than 0. What?

The function that generates this floating point number is this:

float ray1(vec3 rayO, vec3 rayD){
    
    float a = rayD.x;
    float b = rayD.y;
    float c = rayD.z;
    
    float x0 = rayO.x;
    float y0 = rayO.y;
    float z0 = rayO.z;
    
    
    float alpha = 0.1 * ((a * a * c) + (b * b * c));
    float beta = 0.1 * ((a * a * z0) + (2. * x0 * a * c) + (b * b * z0) + (2. * y0 * b * c));
    float gamma = 0.1 * ((2. * x0 * z0 * a) + (x0 * x0 * c) + (2. * y0 * z0 * b) + (y0 * y0 *c)) + (0.6  * c);
    float rho = 0.1 * ((z0 * x0 * x0) + (z0 * y0 * y0)) + (0.6 * z0) + 1.;
    
    float P = -beta / (3. * alpha);
    float Q = (P * P * P) + (((beta * gamma) - (3. * alpha * rho)) / (6. * (alpha * alpha)));
    float R = gamma / (3. * alpha);
    
    float M = (Q * Q) + pow(pow(R - (P * P), 3.), .5);
    
    float t = pow(Q + M, .333) + pow(Q - M, .333) + P;
    
    return t;
}

I am passing this to it:

float t0 = ray1(vec3(0.), vec3(1.));

When I check what t0 is with any of these ifs (one at a time):

if(isnan(t0)){
    col = vec4(.5);
}
if(t0 <= 0.){
    col = vec4(.5);
}
if(t0 >= 0.){
    col = vec4(.5);
}
if(isinf(t0)){
    col = vec4(.5);
}

they all return false.

What is t0? Why is this happening?

Update: I'm fairly certain that the issue lies with taking the cube root of Q, because when I do return pow(Q, .333); instead, it gives the same result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦毁影碎の 2025-01-23 11:43:28

这似乎是编译器进行了一些积极优化的结果。通过一些代数,您可以确定从这一行开始:

float t = pow(Q + M, .333) + pow(Q - M, .333) + P;

Q 的值为 -2.5,M 的值为 7.25。因此,第二个 pow 将产生 NaN,整个表达式也将产生 NaN,并且就编译器而言,可以优化整个 ray1 函数调用。

事实上,编译器似乎也优化了 NaN 检查 if 语句,这也许可以被视为一个错误,但值得注意的是,WebGL 的实现不需要支持 NaN首先。 (请参阅用户 ibesora 对这个与 NaN 相关的问题的回答。)换句话说,让着色器的行为依赖于编译时保证为 NaN 的值会导致未定义的行为。

为了确认这与优化相关,我修改了设置 M 的行,如下所示:

float M = (Q * Q) + pow(pow(R - (P * P), 3.), .5) + (iDate.x - 2022.);

通过此更改,图像将变为灰色,正如您所期望的那样。

This appears to be the result of some aggressive optimization by the compiler. With some algebra, you can determine that as of this line:

float t = pow(Q + M, .333) + pow(Q - M, .333) + P;

the value of Q is -2.5 and M is 7.25. Thus, the second pow will produce a NaN, as will the whole expression, and as far as the compiler is concerned the entire ray1 function call can be optimized away.

The fact that the compiler then seems to optimize away the NaN-checking if statement as well could be seen as a bug, perhaps, but it's relevant to note that implementations of WebGL aren't required to support NaN in the first place. (See user ibesora's answer on this NaN-related question.) In other words, having your shader's behavior depend on a value that's guaranteed to be NaN at compile time leads to undefined behavior.

To confirm that this is optimization-related, I modified the line that sets M as follows:

float M = (Q * Q) + pow(pow(R - (P * P), 3.), .5) + (iDate.x - 2022.);

With this change, the image becomes gray as you might be expecting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文