diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog
index 85be102968294dca10b388cb4ec6955e1bf68f99..dd6b48ea65a171864c56d953f63f774ed2603c24 100644
--- a/gcc/fortran/ChangeLog
+++ b/gcc/fortran/ChangeLog
@@ -1,3 +1,8 @@
+2004-08-25  Paul Brook  <paul@codesourcery.com>
+
+	PR fortran/17190
+	* arith.c (gfc_mpfr_to_mpz): Workaround mpfr bug.
+
 2004-08-25  Paul Brook  <paul@codesourcery.com>
 
 	PR fortran/17144
diff --git a/gcc/fortran/arith.c b/gcc/fortran/arith.c
index 03ee14c099998381a44f4e48496d2ca8d47d7cab..5f558139401ede03a180d27b68ab27a53d24f719 100644
--- a/gcc/fortran/arith.c
+++ b/gcc/fortran/arith.c
@@ -106,17 +106,20 @@ int gfc_index_integer_kind;
    It's easily implemented with a few calls though.  */
 
 void
-gfc_mpfr_to_mpz(mpz_t z, mpfr_t x)
+gfc_mpfr_to_mpz (mpz_t z, mpfr_t x)
 {
   mp_exp_t e;
 
   e = mpfr_get_z_exp (z, x);
+  /* MPFR 2.0.1 (included with GMP 4.1) has a bug whereby mpfr_get_z_exp
+     may set the sign of z incorrectly.  Work around that here.  */
+  if (mpfr_sgn (x) != mpz_sgn (z))
+    mpz_neg (z, z);
+
   if (e > 0)
     mpz_mul_2exp (z, z, e);
   else
     mpz_tdiv_q_2exp (z, z, -e);
-  if (mpfr_sgn (x) < 0)
-    mpz_neg (z, z);
 }