在Floating point linear interpolation中,一位用户提出了lerp
的这种实现:
float lerp(float a, float b, float f)
{
return (a * (1.0 - f)) + (b * f);
}
而另一位用户则建议实施lerp
:
float lerp(float a, float b, float f)
{
return a + f * (b - a);
}
显然,由于浮点精度损失,后一种实现较差.
然后我查看了Wikipedia,它表示对于前一个实现:
// Precise method, which guarantees v = v1 when t = 1. This method is monotonic only when v0 * v1 < 0.
// Lerping between same values might not produce the same value
对于后者:
// Imprecise method, which does not guarantee v = v1 when t = 1, due to floating-point arithmetic error.
// This method is monotonic. This form may be used when the hardware has a native fused multiply-add instruction.
这给了我几个问题:
-
Wikipedia声明"相同值之间的Lerping可能不会产生相同的值",我认为除了浮点精度之外,函数是相同的.不是的
(a * (1.0 - f)) + (b * f)=a + f * (b - a)
a mathematical identity?
如果不是,在两个函数中会产生不同结果的值是什么?
-
维基百科所说的单调是什么意思?为什么一个实现是单调的,而另一个不是单调的?
-
lerp
还有其他常见的实现吗?
Edit: