@kaffiene - can you honesty say that you truly understood the equation in less than a second? If so, both you and Sickan and way more clever than me and the vast majority of programmers on the planet. Its not about trouble…it’s about read/write/maintainability.
@Oskuro - Forget c++.
@princec - no matter how you slice it, it’s awful.
If I were to write this kind of abomination, it might be like this:
public T easeC2(T t)
{
r = t.dup(); // t
r.mul(6); // 6t
r.sub(15); // 6t-15
r.mul(t); // t(6t-15)
r.add(10); // t(6t-15)+10
r.mul(t); // t(t(6t-15)+10)
r.mul(t); // t^2(t(6t-15)+10)
r.mul(t); // t^3(t(6t-15)+10) = 10t^3 - 15t^4 + 6t^5
ret;
}
Now let’s compare it to some pseudo-assembly for a primative type:
.proc easeC2
T r;
fmov r0, r1; ; t
fadd r0, #6.0; ; 6t
fsub r0, #15.0 ; 6t-15
fmul r0, r1 ; t(6t-15)
fadd r0, #10.0 ; t(6t-15)+10
fmul r0, r1 ; t(t(6t-15)+10)
fmul r0, r1 ; t^2(t(6t-15)+10)
fmul r0, r1 ; t^3(t(6t-15)+10) = 10t^3 - 15t^4 + 6t^5
.endproc
Go! Go! High level languages. OK, rewrite using composite ops:
public T easeC2(T t)
{
T r;
r = t.dup(); // t
r.msub(6,15) // 6t-15
r.madd(t,10); // t(6t-15)+10
r.mul(t); // t(t(6t-15)+10)
r.mul(t); // t^2(t(6t-15)+10)
r.mul(t); // t^3(t(6t-15)+10) = 10t^3 - 15t^4 + 6t^5
}
Oh yeah…massive improvement. And let’s remember that this is a 3 term polynomial equation at the pre-algebra level. Mathematics gets a tad bit more involved.
I’ll pass and go for something like this any day:
public T easeC2(T t)
{
@HornerExpand
T r = 10*t^3 - 15*t^4 + 6*t^5;
return r;
}
(EDIT: Horror - I forget the return statement in the assembly!
)