mpf_eq can give wrong results in certain cases
Uwe Mueller
um.computing at arcor.de
Mon Jul 13 00:53:58 CEST 2009
The following test program evaluates the function mpf_eq in 9 specific
cases.
In my systems (1. GNU/Linux i686, 2. GNU/Linux x86_64), 3 of these 9 cases
evaluate to wrong results. The results do not depend on the OS, but on
the GMP version (4.3.1, 4.2.2, and 4.2.1).
In which cases the results are right and wrong, is mentioned in the
comments
of the program. The actual versus correct values are also given in the
program output.
Please find the output for different GMP versions below, after the
program source.
-----------------------------------------------------------------------------------------------------------------
#include <gmp.h>
int main()
{
mpf_t x, y;
unsigned long int cmp_bits;
mpf_set_default_prec(128);
gmp_printf("GMP version %s\n", gmp_version);
/* wrong in 4.3.1, wrong in 4.2.1 */
mpf_init_set_str(x, "1000000000000000000", 16);
mpf_init_set_str(y, "1000000000000000001", 16);
cmp_bits = 72;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 1)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
/* right in 4.3.1, right in 4.2.1 */
cmp_bits = 73;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 0)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
/* right in 4.3.1, right in 4.2.1 */
cmp_bits = 74;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 0)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
/* right in 4.3.1, wrong in 4.2.1 */
mpf_set_str(x, "1000000000000000010", 16);
mpf_set_str(y, "1000000000000000011", 16);
cmp_bits = 72;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 1)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
/* right in 4.3.1, right in 4.2.1 */
cmp_bits = 73;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 0)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
/* wrong in 4.3.1, right in 4.2.1 */
cmp_bits = 74;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 0)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
/* right in 4.3.1, wrong in 4.2.1 */
mpf_set_str(x, "100", 16);
mpf_set_str(y, "101", 16);
cmp_bits = 8;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 1)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
/* right in 4.3.1, right in 4.2.1 */
cmp_bits = 9;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 0)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
/* wrong in 4.3.1, right in 4.2.1 */
cmp_bits = 10;
gmp_printf("mpf_eq(%.0Ff, %.0Ff, %ld) = %d (should be 0)\n",
x, y, cmp_bits, mpf_eq(x, y, cmp_bits));
return 0;
}
-----------------------------------------------------------------------------------------------------------------
The test program does not take any input. On my systems, it has the
following outputs
GMP version 4.3.1
mpf_eq(4722366482869645213696, 4722366482869645213697, 72) = 0 (should be 1)
mpf_eq(4722366482869645213696, 4722366482869645213697, 73) = 0 (should be 0)
mpf_eq(4722366482869645213696, 4722366482869645213697, 74) = 0 (should be 0)
mpf_eq(4722366482869645213712, 4722366482869645213713, 72) = 1 (should be 1)
mpf_eq(4722366482869645213712, 4722366482869645213713, 73) = 0 (should be 0)
mpf_eq(4722366482869645213712, 4722366482869645213713, 74) = 1 (should be 0)
mpf_eq(256, 257, 8) = 1 (should be 1)
mpf_eq(256, 257, 9) = 0 (should be 0)
mpf_eq(256, 257, 10) = 1 (should be 0)
GMP version 4.2.2
mpf_eq(4722366482869645213696, 4722366482869645213697, 72) = 0 (should be 1)
mpf_eq(4722366482869645213696, 4722366482869645213697, 73) = 0 (should be 0)
mpf_eq(4722366482869645213696, 4722366482869645213697, 74) = 0 (should be 0)
mpf_eq(4722366482869645213712, 4722366482869645213713, 72) = 0 (should be 1)
mpf_eq(4722366482869645213712, 4722366482869645213713, 73) = 0 (should be 0)
mpf_eq(4722366482869645213712, 4722366482869645213713, 74) = 0 (should be 0)
mpf_eq(256, 257, 8) = 0 (should be 1)
mpf_eq(256, 257, 9) = 0 (should be 0)
mpf_eq(256, 257, 10) = 0 (should be 0)
GMP version 4.2.1
mpf_eq(4722366482869645213696, 4722366482869645213697, 72) = 0 (should be 1)
mpf_eq(4722366482869645213696, 4722366482869645213697, 73) = 0 (should be 0)
mpf_eq(4722366482869645213696, 4722366482869645213697, 74) = 0 (should be 0)
mpf_eq(4722366482869645213712, 4722366482869645213713, 72) = 0 (should be 1)
mpf_eq(4722366482869645213712, 4722366482869645213713, 73) = 0 (should be 0)
mpf_eq(4722366482869645213712, 4722366482869645213713, 74) = 0 (should be 0)
mpf_eq(256, 257, 8) = 0 (should be 1)
mpf_eq(256, 257, 9) = 0 (should be 0)
mpf_eq(256, 257, 10) = 0 (should be 0)
More information about the gmp-bugs
mailing list