Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect derivative for while-loops with declarations and assignments in forward mode #913

Closed
gojakuch opened this issue May 29, 2024 · 0 comments · Fixed by #943
Closed
Assignees

Comments

@gojakuch
Copy link
Collaborator

gojakuch commented May 29, 2024

In the following example, fn is basically a zero function. Yet, Clad computes its derivative to be 1.

double fn(double x) {
    while (double t = (x = 0)) {}
    return x;
} // = 0

int main() {
    auto f_dx = clad::differentiate(fn, 0);

    std::cout << f_dx.execute(5) << '\n';
}

Expected output: 0

Output: 1

Same thing for

double fn(double x) {
    while ((x = 0)) {}
    return x;
} // = 0

int main() {
    auto f_dx = clad::differentiate(fn, 0);

    std::cout << f_dx.execute(5) << '\n';
}

Expected output: 0

Output: 1

This could be solved similarly to how #905 tries to solve the same issue for for-loops, that is with comma operators with a possibility to support more complicated conditions like (y = x*x) || (y = y - 1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants