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

Document common trap in arb_set_d #1786

Closed
rudolph-git-acc opened this issue Feb 18, 2024 · 7 comments
Closed

Document common trap in arb_set_d #1786

rudolph-git-acc opened this issue Feb 18, 2024 · 7 comments

Comments

@rudolph-git-acc
Copy link

rudolph-git-acc commented Feb 18, 2024

Ref. this AskSagemath-question: https://ask.sagemath.org/question/76066/ghost-numbers-when-using-arb/

When I run this simple code in my IDE:

    #include "flint/arb.h"
    
    int main(int argc, char **argv)
    {
    	arb_t a;
    	arb_init(a);
    	
    	slong prec;
    	prec = 256;
    	
    	arb_set_d(a, 1.1);
    	arb_printd(a, 70);
    	printf("  WRONG \n");
    	
    	arb_set_str(a, "1.1", prec);
    	arb_printd(a, 70);
    	printf("  FINE \n");
    	
    	arb_set_d(a, 1);
    	arb_printd(a, 70);
    	printf("  FINE \n");
    	
    	arb_clear(a);
    	return 0;
    }

I obtain:

1.100000000000000088817841970012523233890533447265625000000000000000000 +/- 0  WRONG 
1.100000000000000000000000000000000000000000000000000000000000000000000 +/- 1.7272e-77  FINE 
1.000000000000000000000000000000000000000000000000000000000000000000000 +/- 0  FINE 

Only the arb_set_d command seems to induce this issue and only for non-integer values. Is this a bug or an unavoidable effect?

@albinahlback
Copy link
Collaborator

albinahlback commented Feb 18, 2024

I don't know what you specifically mean by "wrong" here, or a "bug".

  1. arb_set_d will only convert a double to an arb_t. If the double is "inexact" (meaning that the original decimal number is not representable as a dyadic rational), that's on the user -- FLINT/Arb is not a compiler.
  2. arb_set_d will not impose some error bound since it doesn't know if the double is inexact or not. Once again, that's on the user.

As 1.1 is not representable as a dyadic rational, you should not expect Arb to give a "correct" number. It will only convert what is given to it, which is an approximation, which in turn an arb_t can represent.

@vneiger
Copy link
Collaborator

vneiger commented Feb 18, 2024

Reading the provided code and the sagemath link, I think the following addition to the answer of @albinahlback might be useful for @rudolph-git-acc : observe what you get from the following piece of C,

printf("%.20f\n", 1.1);

It should yield 1.10000000000000008882. This is what is meant by "inexact" (not representable etc.) above, and this has nothing to do with arb or flint or sagemath. (Sorry for the noise if that was already clear to you.)

@rudolph-git-acc
Copy link
Author

@vneiger Thanks for the clarification. Your example is very helpful and I had never appreciated the "inexact" issue.

It is good to be aware of this though and maybe it is worthwhile mentioning it in the documentation of arb_set_d. I have for instance seen it being used in setting integral limits and there it could make a big difference e.g. in a lower limit close to zero. Or for instance in this issue on the old ARB GitHub : flintlib/arb#451 Replacing arb_set_d by are_set_str has an immediate impact on the result.

I most certainly don't want to blame FLINT/ARB for having "bugs" or being "wrong" and will include a link to this thread in my AskSageMath question.

@albinahlback
Copy link
Collaborator

Yes, this should be documented. Thanks for bringing this up!

@albinahlback albinahlback reopened this Feb 18, 2024
@albinahlback albinahlback changed the title It seems that the arb_set_d function picks up noise when fed with a non-integer value Document common trap in arb_set_d Feb 18, 2024
@albinahlback
Copy link
Collaborator

Fixed in 0c07030

@rudolph-git-acc
Copy link
Author

Sorry to be a pain, but the same issue could be induced by the following commands:

image \

image \

image \

The latter is often used in setting integral limits. Probably good to update the documentation for these as well.

P.S.
I believe we should be good for fmpz-types:
image

@edgarcosta
Copy link
Member

arf_set_d does not have this issue, one is converting between two equivalent representations.

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

4 participants