On Mon, 10 Aug 2020 at 10:48, wei zhao <zwleopard....@gmail.com> wrote:
>
> emmmmm, i just hope mpir tries to add 0s at the end when conversion.

It doesn't add *anything* at the end. There is nothing after the end.
Once you reach the limit of the precision, there is no further data
after that point.

Finite precision means there's a finite number of bits. There's
nothing after those bits. Only finitely many bits are stored.

>
> i tried mpf_set_str(), it seems give me correct result.

It will give you the nearest approximation to the decimal value using
the the precision you set.

mpf is NOT a decimal system, it is a binary system. I think you are
confused about this. There is no exact conversion between fractional
decimal numbers to a given (decimal) precision and fractional binary
numbers to a given (binary) precision. The precision in mpf is a
BINARY precision, not a decimal one. You can lose information
converting between decimal and binary! That has nothing to do with
mpf. It is a feature of the mathematics itself.

The random digits you see are because you lost information converting
from decimal to binary and back again. mpf does not store the decimal
number. It stores a binary approximation. So when you convert back to
decimal, some information is gone and cannot be retrieved.

Think about it, how would YOU write 0.1 decimal in binary? It's not
possible in a finite number of bits.

Bill.

>
> wei.
>
>
> 在 2020年8月10日星期一 UTC+8下午4:24:43,Bill Hart写道:
>>
>> Correction: integers and powers of 10 with non-negative exponent are
>> exactly representable in binary.
>>
>> On Mon, 10 Aug 2020 at 10:22, Bill Hart <goodwi...@googlemail.com> wrote:
>> >
>> > On Mon, 10 Aug 2020 at 10:08, wei zhao <zwleop...@gmail.com> wrote:
>> > >
>> > > thanks Bill.
>> > >
>> > > I am confused. As you said, i can NOT initialize mpf_t via a double, 
>> > > right?
>> >
>> > I did not say that. I said that doubles have 53 bits precision. You
>> > cannot get more precision from mpf if you start with 53 bits of
>> > precision at the input.
>> >
>> > > The reason I turn to multiple-precision is that I hope mpir could 
>> > > provide more precision, double does have 53bit precision, but mpf_z 
>> > > should have more.
>> >
>> > It does allow more precision, but you cannot start with less
>> > information than you want to end up with. mpf is not able to add
>> > precision that wasn't there to start with. The information was already
>> > lost in the conversion of 0.1 decimal to a double (binary). This
>> > happened before mpf even got the value.
>> >
>> > >
>> > > if so, mpir is not more accurate than double, because it adds some 
>> > > random numbers at the end, which will affect the computation result.
>> >
>> > No it does not "add random numbers". This is what 0.1 looks like to 53
>> > bits of precision, which is precisely what you passed into the mpf
>> > function. It stored exactly what you gave it.
>> >
>> > >
>> > > any idea to solve this issue?
>> >
>> > I already told you exactly how to solve the issue in my previous post.
>> > Did you try it?
>> >
>> > Decimal fractions aren't always exactly representable in (limited
>> > precision) binary. One way to solve it would be to put in an integer,
>> > then divide by a power of 10. Integers and powers of 10 are exactly
>> > representable in binary.
>> >
>> > Bill.
>> >
>> > >
>> > >
>> > > thanks.
>> > >
>> > >
>> > >
>> > > 在 2020年8月10日星期一 UTC+8下午2:45:56,Bill Hart写道:
>> > >>
>> > >> You are setting it to the double 0.1, which only has 53 bits
>> > >> precision. You can't get more precision than you put in.
>> > >>
>> > >> Try setting it to 0.5 first (which can be represented exactly in
>> > >> binary), and then divide by the integer 5, which is also able to be
>> > >> represented exactly in binary. Then you will get the 128 bits you are
>> > >> after.
>> > >>
>> > >> By the way, the mpf module isn't used much any more. You should look
>> > >> into mpfr (mpfr.org) instead, which is quite often much faster, and
>> > >> has more functionality and development effort.
>> > >>
>> > >> Bill.
>> > >>
>> > >> On Mon, 10 Aug 2020 at 08:32, wei zhao <zwleop...@gmail.com> wrote:
>> > >> >
>> > >> > hi all,
>> > >> >
>> > >> > I wrote a simple code to test mpir and found a strange thing.
>> > >> >
>> > >> > the code is
>> > >> >
>> > >> > mpf_t a;
>> > >> > mpf_init2(a, 128);
>> > >> > mpf_set_d(a, 0.1);
>> > >> >
>> > >> > mp_exp_t exp;
>> > >> > char str[256];
>> > >> > mpf_get_str(str, &exp, 10, 128, a);
>> > >> > I checked str, and it was "1000000000000000055511151231257827021182" 
>> > >> > instead of "1000000000000000000000000000000000000000".
>> > >> >
>> > >> > anybody knows why? Does not it affect the computation precision?  how 
>> > >> > can I make the variable more accurate?
>> > >> >
>> > >> > thansk a lot
>> > >> >
>> > >> > wei
>> > >> >
>> > >> > --
>> > >> > You received this message because you are subscribed to the Google 
>> > >> > Groups "mpir-devel" group.
>> > >> > To unsubscribe from this group and stop receiving emails from it, 
>> > >> > send an email to mpir-...@googlegroups.com.
>> > >> > To view this discussion on the web visit 
>> > >> > https://groups.google.com/d/msgid/mpir-devel/4dca16b4-7b7d-4a16-91c6-834def1f8f97o%40googlegroups.com.
>> > >
>> > > --
>> > > You received this message because you are subscribed to the Google 
>> > > Groups "mpir-devel" group.
>> > > To unsubscribe from this group and stop receiving emails from it, send 
>> > > an email to mpir-...@googlegroups.com.
>> > > To view this discussion on the web visit 
>> > > https://groups.google.com/d/msgid/mpir-devel/22b82e2c-cc4d-4557-9f8d-ce7c434aa5ado%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "mpir-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to mpir-devel+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/mpir-devel/095760f7-9477-40e7-9e96-015df643e7c4o%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"mpir-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mpir-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mpir-devel/CAB0xFns%3DqzqrqnJ5a%3Dyg8HvbjcpgQmTu2ecrU24Lk_mtTbFj3g%40mail.gmail.com.

Reply via email to