easing-functions icon indicating copy to clipboard operation
easing-functions copied to clipboard

Several implementations rely on undefined behaviour

Open Olof-IL opened this issue 3 years ago • 2 comments

Looking through the code I notice that several implementations contain undefined behaviour, using --t and t several times in the same expressions.

The evaluation order is undefined without sequence points, so the result of these expressions are undefined.

Olof-IL avatar Nov 03 '20 09:11 Olof-IL

I ported the code to plain C and found that the easeInOutCubic() function suddenly returns wrong values when compiling with gcc

quick code snippet for onlinegdb

int main()
{
    int i,N;
    float t,r;
    N = 10;
    for(i=0,t=0.0f; i<N; i++){
        t = (float)i * 1.0f/((float)N);
        r = t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t);
        printf("%.3f, %.3f\n", t, r);
    }
    return 0;
}

Will print wrong output for t>0.5

0.000, 0.000
0.100, 0.004
0.200, 0.032
0.300, 0.108
0.400, 0.256
-1.500, -12.500
-1.400, -9.976
-1.300, -7.788
-1.200, -5.912
-1.100, -4.324

I had to remove one of the pre-decrements:

float easeInOutCubic( float t ) {
    return t < 0.5 ? 4 * t * t * t : 1 + (t) * (2 * (--t)) * (2 * t);
    // return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t);  // returns false output
}

mnemocron avatar Jan 16 '22 09:01 mnemocron

As previously discussed in #1

mnemocron avatar Jan 16 '22 09:01 mnemocron