Re: Rounding error
Paul Zimmermann writes: Of course you can add say 100 guard bits, then the probability to get an incorrect rounding is about 2^-100... You're joking, but this was sort-of my thinking when designing mpf! -- Torbjörn Please encrypt, key id 0xC8601622 ___ gmp-bugs mailing list gmp-bugs@gmplib.org https://gmplib.org/mailman/listinfo/gmp-bugs
Re: Rounding error
Dear Frank, > If I ever need correct rounding with GMP (I don't ATM), I think > I could add 0.5e-P, then (like above) multiply by 1eP, convert to > mpz_t and insert the decimal point manually. since there is no documented error bound in the mpf computations, there is no chance that this will (provably) work. The GMP manual says: Note that the 'mpf' functions are _not_ intended as a smooth extension to IEEE P754 arithmetic. In particular results obtained on one computer often differ from the results on a computer with a different word size. Of course you can add say 100 guard bits, then the probability to get an incorrect rounding is about 2^-100... Best regards, Paul Zimmermann ___ gmp-bugs mailing list gmp-bugs@gmplib.org https://gmplib.org/mailman/listinfo/gmp-bugs
Re: Rounding error
Marco Bodrato wrote: > Il 2021-08-25 05:54 Frank Heckenbach ha scritto: > > mpf_get_str seems to round incorrectly sometimes. > > In this test program, the digits around 809 are "...244594553...", > > so it should round to "...244595", but it outputs "...244594". > > Here, your report says that you expected a "rounded" result, > but you get a truncated one. > > Il 2021-08-27 07:36 Frank Heckenbach ha scritto: > > Actually, what I wanted is truncation, not rounding. But AFAICS > > Here, you say that you actually want truncation. Yes, I actually want truncation. Since GMP does not support string conversion with truncation (correct?), I tried to emulate truncation by subtracting 0.5e-P, then rounding, which should give the same result as truncation (e.g. 2.3 -> 1.8, rounded to 2; 2.8 -> 2.3, rounded to 2). When I did this, I found that in some cases, rounding was wrong, and after asking here, I learned that apparently GMP is not expected to always round correctly. Yes, it's true that the wrong rounding happens to be truncation, but that doesn't help me, since it only occurs sometimes, so I never know if what I get is rounded or truncated. My solution (hopefully) is therefore to multiply by 1eP, convert to mpz_t and insert the decimal point manually. AFAICS, the conversion to mpz_t always truncates (correct?), so this should work (albeit slightly slower, but I don't mind). If I ever need correct rounding with GMP (I don't ATM), I think I could add 0.5e-P, then (like above) multiply by 1eP, convert to mpz_t and insert the decimal point manually. > > there is no string conversion function with truncation (correct?), > > From the manual I read a general claim about functions using mpf: > "Final results are always truncated to the destination variable's > precision." > See https://gmplib.org/manual-6.2.1/Floating_002dpoint-Functions . > > Do you have an example of the function not working as documented? AIUI, this refers to mpf_t computations (i.e. where the destination is a mpf_t), not conversion to string, so something else. (String conversions are usually rounded, but not always.) Viele Grüße, Frank ___ gmp-bugs mailing list gmp-bugs@gmplib.org https://gmplib.org/mailman/listinfo/gmp-bugs
Re: Rounding error
Ciao, Il 2021-08-25 05:54 Frank Heckenbach ha scritto: mpf_get_str seems to round incorrectly sometimes. In this test program, the digits around 809 are "...244594553...", so it should round to "...244595", but it outputs "...244594". Here, your report says that you expected a "rounded" result, but you get a truncated one. Il 2021-08-27 07:36 Frank Heckenbach ha scritto: Actually, what I wanted is truncation, not rounding. But AFAICS Here, you say that you actually want truncation. there is no string conversion function with truncation (correct?), From the manual I read a general claim about functions using mpf: "Final results are always truncated to the destination variable’s precision." See https://gmplib.org/manual-6.2.1/Floating_002dpoint-Functions . Do you have an example of the function not working as documented? Ĝis, m ___ gmp-bugs mailing list gmp-bugs@gmplib.org https://gmplib.org/mailman/listinfo/gmp-bugs
Re: Rounding error
Marc Glisse wrote: > if you care about correct rounding, I would recommend you use MPFR > instead. This seems to be a bigger change which I'd like to avoid, at least for now. (I use GMP in several programs, some of which have grown over many years ...) Actually, what I wanted is truncation, not rounding. But AFAICS there is no string conversion function with truncation (correct?), so I tried subtracting 0.5e-P, then rounding, which ran into this problem. So now I try multiplying by 1eP and converting to mpz which does truncation. (It seems slightly slower, but insignificant to me.) This doesn't seem to have precision issues (and if needed, it seems correct rounding can be done by adding 0.5e-P and truncating like this). At least my tests give correct results. Is this expected or was I just lucky? This is the function I use (C++ with an obvious implementation of ToString; decimal only): std::string mpf_to_string (mpf_class x, size_t precision) { auto r = ToString ((x < 0 ? "-" : ""), std::setw (precision + 1), std::setfill ('0'), mpz_class (abs (x * mpf_class (ToString ("1e", precision); r.insert (r.length () - precision, "."); return r; } ___ gmp-bugs mailing list gmp-bugs@gmplib.org https://gmplib.org/mailman/listinfo/gmp-bugs
Re: Rounding error
On Wed, 25 Aug 2021, Frank Heckenbach wrote: mpf_get_str seems to round incorrectly sometimes. In this test program, the digits around 809 are "...244594553...", so it should round to "...244595", but it outputs "...244594". Hello, if you care about correct rounding, I would recommend you use MPFR instead. -- Marc Glisse ___ gmp-bugs mailing list gmp-bugs@gmplib.org https://gmplib.org/mailman/listinfo/gmp-bugs
Rounding error
mpf_get_str seems to round incorrectly sometimes. In this test program, the digits around 809 are "...244594553...", so it should round to "...244595", but it outputs "...244594". #include #include int main (void) { mpf_t pi; mp_exp_t e; mpf_set_default_prec (1); // much more than sufficient mpf_init_set_str (pi, "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577", 10); puts (mpf_get_str (NULL, &e, 10, 809, pi)); } - gmp 6.2.1, original release, unpatched, no configure options - configure: summary of build options: Version: GNU MP 6.2.1 Host type: zen2-pc-linux-gnu ABI: 64 Install prefix:/usr/local Compiler: gcc Static libraries: yes Shared libraries: yes - gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 8.3.0-6' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release \ --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 8.3.0 (Debian 8.3.0-6) - uname -a Linux mars 5.6.0-0.bpo.2-amd64 #1 SMP Debian 5.6.14-2~bpo10+1 (2020-06-09) x86_64 GNU/Linux - ./config.guess zen2-pc-linux-gnu - ./configfsf.guess x86_64-pc-linux-gnu - CPU: AMD Ryzen 9 3900X 12-Core Processor ___ gmp-bugs mailing list gmp-bugs@gmplib.org https://gmplib.org/mailman/listinfo/gmp-bugs