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

Issue with process = (1.0 + ma.EPSILON/2.0) > 1.0 #962

Open
sletz opened this issue Oct 29, 2023 · 7 comments
Open

Issue with process = (1.0 + ma.EPSILON/2.0) > 1.0 #962

sletz opened this issue Oct 29, 2023 · 7 comments

Comments

@sletz
Copy link
Member

sletz commented Oct 29, 2023

The code process = (1.0 + ma.EPSILON/2.0) > 1.0 correctly returns 0 when compiled in -double but incorrectly returns 1 when compiled in -single.

Even if ma.EPSILON definition is dependant of -single/-double/-quad (see https://github.com/grame-cncm/faustlibraries/blob/master/maths.lib#L157), the internal computation is actually using double and has to be fixed.

@oleg-nesterov
Copy link
Contributor

It is clear why -single/-double makes a difference in this case,
but...

To be honest, I never really understood the purpose of ma.EPSILON.
And what is it? Say,

    doubleprecision EPSILON = 2.2204460492503131e-016;

is it DBL_MIN? Or DBL_TRUE_MIN?

Nevermind. In any case I'd say it should be used with care.

process = (1.0 + ma.EPSILON/2.0) > 1.0 correctly returns 0 when
compiled in -double

OK, lets suppose that "0" is correct in this case. Now consider

    import("stdfaust.lib");

    process =
            (0.0 + ma.EPSILON/2.0) > 0.0,
            (0.5 + ma.EPSILON/2.0) > 0.5,
            (1.0 + ma.EPSILON/2.0) > 1.0;

compiled in double, it outputs

    1, 1, 0

Doesn't look consistent but what can we do? and do we really care?

Oleg.

@sletz
Copy link
Member Author

sletz commented Oct 29, 2023

#include <iostream>
#include <cfloat>

int main() {
    bool a = (0.0f + FLT_EPSILON / 2.0f) > 0.f;
    bool b = (0.5f + FLT_EPSILON / 2.0f) > 0.5f;
    bool c = (1.f + FLT_EPSILON / 2.0f) > 1.f;


    std::cout << "a: " << a << std::endl;
    std::cout << "b: " << b << std::endl;
    std::cout << "c: " << c << std::endl;
    
    bool d = (0.0f + DBL_EPSILON / 2.0f) > 0.f;
    bool e = (0.5f + DBL_EPSILON / 2.0f) > 0.5f;
    bool f = (1.f + DBL_EPSILON / 2.0f) > 1.f;

    std::cout << "d: " << d << std::endl;
    std::cout << "e: " << e << std::endl;
    std::cout << "f: " << f << std::endl;

    return 0;
}

@dariosanfilippo
Copy link

dariosanfilippo commented Oct 29, 2023 via email

@oleg-nesterov
Copy link
Contributor

Ah. Yes, yes, I know. And I too wrote the similar program to verify.

Sorry for confusion. When I said "Doesn't look consistent" I meant "Doesn't look consistent but in fact correct".

My only point was, again, in any case ma.EPSILON should be used with care.
It is not immediately clear that, again,

    process = (0.5 + ma.EPSILON/2.0) > 0.5;

and
process = (1.0 + ma.EPSILON/2.0) > 1.0;

do not output the same result.

And I think we do not really care.

But I agree that the fact it depends on -single/-double make the things worse.

@oleg-nesterov
Copy link
Contributor

I think that it is defined as the smallest quantity, let's call it EPS, for
which (1.0 + EPS) > 1.0 returns true.

OK, quite possible, then ma.EPSILON == nextafter(1.0,2.0) - 1.0

And this means that even a more simple example can surprise the user:

    import("stdfaust.lib");

    process =
            (1.0 + ma.EPSILON) > 1.0,
            (2.0 + ma.EPSILON) > 2.0;

compiled with -double outputs

    output0[i0] = FAUSTFLOAT(1);
    output1[i0] = FAUSTFLOAT(0);

Again, this is correct in that this is how the FP math works.

But again, this means that ma.EPSILON should be used with care.
And "the smallest quantity" depends on the usage, while *.lib
blindly use ma.ma.EPSILON assuming it always fits the needs.

@sletz
Copy link
Member Author

sletz commented Oct 30, 2023

I guess we want ma.EPSILON to exactly behave as the FP norm says, in -single and -double mode, no more no less (and which is not currently the case).

@sletz
Copy link
Member Author

sletz commented Oct 30, 2023

Improved documentation: https://faustlibraries.grame.fr/libs/maths/#maepsilon

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

No branches or pull requests

3 participants