A unary minus operator was applied to an unsigned type - conversion from 64 Bit to 32 Bit

marco.bodrato at tutanota.com marco.bodrato at tutanota.com
Wed Sep 4 12:31:08 CEST 2024


Ciao,

3 set 2024, 12:30 da vincent at vinc17.net:

> I don't think that just adding a cast to avoid an implicit conversion
> can be regarded as obfuscating the code. On the contrary, IMHO,
> an implicit conversion that may change the value is some kind of
> obfuscation.
>

I may agree on the "Warning" triggered by the code
unsigned long
mpz_get_ui (mpz_srcptr __gmp_z) __GMP_NOTHROW
{
[...]
  return (__gmp_n != 0 ? __gmp_l : 0);
}

We may add a cast:
  return (unsigned long) (__gmp_n != 0 ? __gmp_l : 0);
Is this more clean and explicit? Maybe.

But an "Error" is worst than a "Warning" isn't it?
And the ERROR "A unary minus operator was applied to an unsigned type" is triggered by the following lines:
mp_limb_t
mpn_neg (mp_ptr __gmp_rp, mp_srcptr __gmp_up, mp_size_t __gmp_n)
{
[...]
  *__gmp_rp = (- *__gmp_up) & GMP_NUMB_MASK;
[...]
}

To negate an mpn (an unsigned type), the relevant mp_limb_t (an unsigned type) is negated.
How can we write this more clearly than this way?

Ĝis,
m


More information about the gmp-bugs mailing list