Skip to content
This repository has been archived by the owner on May 16, 2019. It is now read-only.

Undefined behavior #1

Open
Ravirael opened this issue Sep 16, 2016 · 2 comments
Open

Undefined behavior #1

Ravirael opened this issue Sep 16, 2016 · 2 comments

Comments

@Ravirael
Copy link

Ravirael commented Sep 16, 2016

In C++ the order of evaluation is unspecified. That's why most of implementations here cause an undifined behavior, working correctly only sometimes or only with choosen compilers. For instance:

float Quart::easeIn (float t,float b , float c, float d) {
    return c*(t/=d)*t*t*t + b;
}

C++ standard doesn't specify if t/=d will be evaluated before t*t*tor after, or in between.... That can lead to different results on different compilers/operating systems etc.

This code, and all others, should be rewritten as:

float Quart::easeIn (float t,float b , float c, float d) {
       t /= d;
    return c*t*t*t*t + b;
}

In C++ there is no performance gain in writing such one-liners, even if they don't cause undefined behavior.

@jesusgollonet
Copy link
Owner

Hey thanks! Unfortunately I don't use openframeworks much these days so won't be able to fix myself in the foreseeable future. If you get to forking / fixing send me a pull request and I'll happily merge in. Cheers,
j

@Ravirael
Copy link
Author

This code has only <math.h> dependency so it's pure C++ not bound to openframeworks at all (and that's a good thing for sure)! I will fix it in few days I guess.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants