为什么在glsl中变化的float相等性测试失败? [英] Why does varying float equality test fail in glsl?

查看:449
本文介绍了为什么在glsl中变化的float相等性测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的着色器程序中的浮动量有所变化:

If I have a varying float in my shader program:

varying highp float someFloat;

在顶点着色器中,我将其设置为某些内容.

and in the vertex shader, I set it to something.

someFloat = 1.0;

为什么在我的片段着色器中,此比较似乎返回false?

why in my fragment shader does this comparison seem to return false?

someFloat == 1.0 // false

但这返回true吗?

someFloat > .0 // true

在iPad mini上的openGL ES上进行测试.

testing on openGL ES in an iPad mini.

推荐答案

它发生在任何IEEE 754浮点数上.这是因为浮点表示的性质.任何使用IEEE 754格式的语言都将遇到相同的问题.

It happens on any IEEE 754 floating point number. It is because of the nature of floating point representation. Any language that use the IEEE 754 format will encounter the same problem.

由于在浮点系统中1.0可能无法准确地表示为1.000000000...,因此使用==进行比较被认为是危险的. 浮点数应始终与epsilon值进行比较.

Since 1.0 may not be represented exactly in floating point system as 1.000000000... , hence it is considered dangerous to compare them using ==. Floating point numbers should always be compared with an epsilon value .

由于浮点计算涉及一些不确定性,因此我们可以通过查看两个数字是否彼此接近"来尝试实现这一点.如果您基于错误分析,测试或大胆的猜测而决定,结果应始终在预期结果的0.00001以内,那么您可以将比较结果更改为此:

Since floating point calculations involve a bit of uncertainty we can try to allow for this by seeing if two numbers are ‘close’ to each other. If you decide – based on error analysis, testing, or a wild guess – that the result should always be within 0.00001 of the expected result then you can change your comparison to this:

if (fabs(someFloat - 1.0)) < 0.00001)

最大误差值通常称为epsilon.

The maximum error value is typically called epsilon.

可能您应该阅读每位计算机科学家应该了解的浮动知识点算术

这篇关于为什么在glsl中变化的float相等性测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆