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 <goodwillh...@googlemail.com> wrote: > > On Mon, 10 Aug 2020 at 10:08, wei zhao <zwleopard....@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-devel+unsubscr...@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/CAB0xFnvcThye8v%2BP%2Bb%3D8Wnhhmnc90h6OSmMCDGEt9VsFHXjfVA%40mail.gmail.com.