[Win32] set_str() ignores exponent iff base > 36

Torbjorn Granlund tg at swox.com
Mon Dec 10 01:16:57 CET 2007


  Here's the test script:
  
  ----------------------------------
  #include <stdio.h>
  #include <gmp.h>
  
  int main(void) {
      int ret1, ret2;
      mpf_t x;
      printf("%s\n", gmp_version);
      mpf_set_default_prec(64);
      ret1 = mpf_init_set_str(x, "ZXFY at -17", 62);
      ret2 = mpf_out_str(stdout, 62, 0, x);
      printf("\n%d %d\n", ret1, ret2);
  }
  --------------------------------
  
  And here's the output:
  
  --------------------------------
  4.2.2
  0.ZXFY at 4
  0 8


Please try this patch.  It should fix the bug, and also adds some
error checking.

*** mpf/set_str.c	30 Aug 2007 18:19:41 -0000
--- mpf/set_str.c	9 Dec 2007 23:46:07 -0000
*************** mpf_set_str (mpf_ptr x, const char *str,
*** 272,277 ****
  
      if (expptr != 0)
!       /* FIXME: Should do some error checking here.  */
!       exp_in_base = strtol (expptr, (char **) 0, exp_base);
      else
        exp_in_base = 0;
--- 272,298 ----
  
      if (expptr != 0)
!       {
! 	/* Scan and convert the exponent, in base exp_base.  */
! 	long dig, neg = -(long) ('-' == expptr[0]);
! 	expptr -= neg;			/* conditional increment */
! 	c = (unsigned char) *expptr++;
! 	dig = digit_value[c];
! 	if (dig >= exp_base)
! 	  {
! 	    TMP_FREE;
! 	    return -1;
! 	  }
! 	exp_in_base = dig;
! 	c = (unsigned char) *expptr++;
! 	dig = digit_value[c];
! 	while (dig < exp_base)
! 	  {
! 	    exp_in_base = exp_in_base * exp_base;
! 	    exp_base += dig;
! 	    c = (unsigned char) *expptr++;
! 	    dig = digit_value[c];
! 	  }
! 	exp_in_base = (exp_in_base ^ neg) - neg; /* conditional negation */
!       }
      else
        exp_in_base = 0;

-- 
Torbjörn


More information about the gmp-bugs mailing list