Re: Rounding error

2021-08-27 Thread Marco Bodrato

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

2021-08-27 Thread Frank Heckenbach
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