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

set_bigfloat_precision behaves odd (or BigFloat display) #11565

Closed
lbenet opened this issue Jun 3, 2015 · 5 comments
Closed

set_bigfloat_precision behaves odd (or BigFloat display) #11565

lbenet opened this issue Jun 3, 2015 · 5 comments

Comments

@lbenet
Copy link
Contributor

lbenet commented Jun 3, 2015

Something odd may be happening with set_bigfloat_precision, or possibly with the way BigFloats are displayed.

Consider the following:

julia> VERSION # two days old...
v"0.4.0-dev+5147"

julia> get_bigfloat_precision()
256

julia> parse(BigFloat, "1.1")  # default behavior, which I think is ok
1.100000000000000000000000000000000000000000000000000000000000000000000000000003

julia> set_bigfloat_precision(40)
40

julia> parse(BigFloat, "0.1") # ??!?
1.100000000000363797880709171295166015625000000000000000000000000000000000000000

In the last line I was expecting something like 1.1000000000004e+00, since it mimics the last example (of the arbitrary precision section) of the documentation. But I can't actually reproduce it:

julia> with_bigfloat_precision(40) do
       parse(BigFloat,"1.1")
       end
1.100000000000363797880709171295166015625000000000000000000000000000000000000000
@pabloferz
Copy link
Contributor

This is because of the way BigFloat's are displayed now. The idea was to print a lot of digits when printing BigFloat's. I submitted a PR to do this but it wasn't clear how much digits to display by default so I choose to display at least 78 decimal places (which is around the number of decimal places needed to read back the number exactly for the default precision of 256 bits).

The MPFR library doesn't make it easy to find out how many decimal places are needed to read back the number exactly at a given precision (at least this is not clear to me). Playing around with MPFR, I found heuristically, that this number of digits for a lot of precision values (p), is almost always round(Int, p / 3.3). We might use this to choose the number of digits to display depending upon the precision.

To figure out something like this was proposed before by @dpsanders

CC: @StefanKarpinski @tkelman

@lbenet
Copy link
Contributor Author

lbenet commented Jun 4, 2015

I'm not an expert of MPFR. Yet, apart from the info about the precision (which is pleasant to make it disappear), I think the current output of version 0.3.x is nicer:

julia> VERSION
v"0.3.9"

julia> get_bigfloat_precision()
256

julia> BigFloat("1.1")
1.100000000000000000000000000000000000000000000000000000000000000000000000000003e+00 with 256 bits of precision

julia> set_bigfloat_precision(40)
40

julia> BigFloat("1.1")
1.1000000000004e+00 with 40 bits of precision

Regarding the number of displayed digits, I think it is related to the actual number of bits used. For Float64 we have 53 bits of precision, so -floor(log(10,2.0^-53)) yields 16.0, 40 bits of precision it yields 13.0 and 256 bits of precision it yields 78.0.

@StefanKarpinski
Copy link
Sponsor Member

Agree – the number of digits printed should depend on the precision of the BigFloat.

@pabloferz
Copy link
Contributor

@lbenet I see, It makes sense, as log(10, 2^-p) = -p * log(10, 2) = -p / log(2, 10) which is around -p / 3.32.

OK, I can submit again a PR with these changes. Would that be all right @StefanKarpinski ?

@lbenet
Copy link
Contributor Author

lbenet commented Jun 4, 2015

Thanks for taking care of this.

pabloferz added a commit to pabloferz/julia that referenced this issue Jun 4, 2015
pabloferz added a commit to pabloferz/julia that referenced this issue Jun 8, 2015
pabloferz added a commit to pabloferz/julia that referenced this issue Jun 9, 2015
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