Skip to content

“convert” between gr status flags and truth_t values #2297

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

Open
mezzarobba opened this issue Apr 10, 2025 · 6 comments · May be fixed by #2312
Open

“convert” between gr status flags and truth_t values #2297

mezzarobba opened this issue Apr 10, 2025 · 6 comments · May be fixed by #2312
Labels

Comments

@mezzarobba
Copy link
Contributor

mezzarobba commented Apr 10, 2025

I have been playing a bit with the generics subsystem and I often find myself wishing for a way to “convert” truth values into status codes and vice-versa. I mean for instance something like

inline truth_t
gr_in_domain(int status)
{
    switch (status)
    {
        case GR_SUCCESS:
            return T_TRUE;
        case GR_DOMAIN:
            return T_FALSE;
        default:
            return T_UNKNOWN;
    }
}

inline int
gr_check(truth_t t)
{
    switch (t)
    {
        case T_TRUE:
            return GR_SUCCESS;
        case T_FALSE:
            return GR_DOMAIN;
        case T_UNKNOWN:
            return GR_UNABLE;
    }
}

(these are probably the most useful mappings, but not the only ones that make sense).

Was it a deliberate choice not to provide this kind of conversions as standard functions?

@rburing
Copy link
Contributor

rburing commented Apr 13, 2025

(these are probably the most useful mappings, but not the only ones that make sense).

I guess this is probably the reason. For the first one in theory status can have multiple flags set, e.g. both GR_DOMAIN and GR_UNABLE (though the function could be changed to handle this), and for the second one there are cases where T_UNKNOWN is good, e.g. to check if a matrix with interval entries contains the zero matrix you can check gr_mat_is_zero(error_term, ctx) != T_FALSE.

@mezzarobba
Copy link
Contributor Author

For the first one in theory status can have multiple flags set, e.g. both GR_DOMAIN and GR_UNABLE (though the function could be changed to handle this),

Well, it already does — anything with GR_UNABLE set maps to T_UNKNOWN, which I think is usually what one wants (since one can only interpret GR_DOMAIN | GR_UNABLE as a domain error when one can be sure that the GR_DOMAIN flag wasn't set by a function called with incorrect input because of a previous unsupported operation).

and for the second one there are cases where T_UNKNOWN is good, e.g. to check if a matrix with interval entries contains the zero matrix you can check gr_mat_is_zero(error_term, ctx) != T_FALSE.

Sure, the idea is not to replace manually testing status flags or truth values, just to simplify some common cases.

@fredrik-johansson
Copy link
Collaborator

Sure, it would make sense to have such conversions.

This may or may not be implemented in the future, but the set of return flags could conceivably be extended for more granularity. For example, one could have

GR_DOMAIN_NOT_INVERTIBLE, GR_DOMAIN_COMPLEX_RESULT, ...

GR_UNABLE_OVERFLOW, GR_UNABLE_INEXACT_COMPARISON, GR_UNABLE_NOT_IMPLEMENTED, ...

These would set the GR_DOMAIN / GR_UNABLE bits in addition to some extra flag bits.

So a forward-compatible logic for parsing status would be to first check status & GR_UNABLE, and in the else branch check for status & GR_DOMAIN.

@mezzarobba
Copy link
Contributor Author

mezzarobba commented Apr 13, 2025 via email

@fredrik-johansson
Copy link
Collaborator

On Sunday, 13 April 2025 Fredrik Johansson wrote:
This may or may not be implemented in the future, but the set of
return flags could conceivably be extended for more granularity.
Speaking of that, what do you think of reserving some bits from the
return flags for use by individual functions?

Do you have a specific use case in mind?

@mezzarobba
Copy link
Contributor Author

Yes and no. The other day I wanted to write a function that tried to do a certain computation using an incomplete algorithm and returned GR_UNABLE if that didn't work. Depending exactly why the computation failed (basically, when my function explicitly chose to return GR_UNABLE, as opposed to passing it on from some other function it called) I could still be sure that the value it wrote to its output variable made sense as a starting value for a second possible algorithm. I ended up structuring the code a bit differently, but I think it would have made sense to signal that to the caller by returning, say, GR_UNABLE | 0x10.

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

Successfully merging a pull request may close this issue.

3 participants