浮点线性插值 [英] Floating point linear interpolation

查看:240
本文介绍了浮点线性插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要做到两个变量 A B 给出一个分数 F之间的线性插值,我目前使用这种code:

To do a linear interpolation between two variables a and b given a fraction f, I'm currently using this code:

float lerp(float a, float b, float f) 
{
    return (a * (1.0f - f)) + (b * f);
}

我觉得有可能做的更有效的方法。我使用的微控制器没有FPU,所以浮点运算的软件来完成。他们是相当快的,但它仍然像100周期相加或相乘。

I think there's probably a more efficient way of doing it. I'm using a microcontroller without an FPU, so floating point operations are done in software. They are reasonably fast, but it's still something like 100 cycles to add or multiply.

有什么建议?

推荐答案

从precision差异于不顾,即前pression相当于

Disregarding from differences in precision, that expression is equivalent to

float lerp(float a, float b, float f)
{
    return a + f * (b - a);
}

这是2加法/减法和1倍,而不是2加法/减法和乘法2

That's 2 additions/subtractions and 1 multiplication instead of 2 addition/subtractions and 2 multiplications.

这篇关于浮点线性插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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