Does -0.5 fit an unsigned when truncated to an integer?

bodrato at bodrato at
Tue Mar 19 08:48:35 CET 2013


Il Lun, 18 Marzo 2013 10:35 pm, Torbjorn Granlund ha scritto:
> I haven't though a lot about this, but it is not clear that -1 + eps
> should be considered to fit an unsigned type.

If the documentation says "when truncated to an integer"... -1+eps is
truncated to zero, and zero fits an unsigned type.

Both _get_si, and _get_ui, with -1+eps, return zero... mpz_set_d (z,
-0.999) sets z to zero...

By the way, this testing program:

#include <gmp.h>
#include <mpfr.h>
#include <stdio.h>

main (void)
  mpz_t z;
  mpf_t f;
  mpfr_t r;
  double d = -0.9;

  mpz_init_set_d (z, d);
  printf ("z: %i %i\n", mpz_fits_sint_p (z), mpz_fits_uint_p (z));

  mpf_init_set_d (f, d);
  mpz_set_f (z, f);
  printf ("Z: %i %i\n", mpz_fits_sint_p (z), mpz_fits_uint_p (z));
  printf ("f: %i %i\n", mpf_fits_sint_p (f), mpf_fits_uint_p (f));

  mpfr_init_set_d (r, d, GMP_RNDZ);
  printf ("r: %i %i\n", mpfr_fits_sint_p (r, GMP_RNDZ), mpfr_fits_uint_p
(r, GMP_RNDZ));

  printf ("s: %i %i %i\n", mpz_get_si (z) >= 0, mpf_get_si (f) >= 0,
mpfr_get_si (r, GMP_RNDZ) >= 0);
  printf ("d: %i\n", ((int) d) >= 0);

  return 0;


z: 1 1
Z: 1 1
f: 1 0
r: 1 0
s: 1 1 1
d: 1

This means that MPFR too consider that -0.9 does not fit an unsigned int
when rounded towards zero... I'd expect all "1".



More information about the gmp-devel mailing list