diff --git a/src/modules/Gmp/mpq.cmod b/src/modules/Gmp/mpq.cmod index fec5d2abac5b625ce69dc3e1239e49a48be5adf1..5b9ba05c7e7962ed07d48f338fbdf6944107ec88 100644 --- a/src/modules/Gmp/mpq.cmod +++ b/src/modules/Gmp/mpq.cmod @@ -372,7 +372,7 @@ PIKECLASS mpq prec++; /* present 'num' and insert dot */ len += 3; - s = begin_shared_string(len); + s = begin_shared_string(len + 1); /* +1 to cater for a leading 0 */ if(len/2 > prec) { /* Shift the integer part forward to make room for the dot. */ @@ -394,6 +394,11 @@ PIKECLASS mpq prec+1); len++; } + /* Make sure numbers start with a digit */ + if ((len == prec) || ((unsigned)(s->str[len-prec-1] - '0') > 9)) { + memmove(s->str+len-prec+1, s->str+len-prec, prec); + s->str[len++ -prec] = '0'; + } s->str[len-prec]='.'; push_string (end_and_resize_shared_string (s, len)); } diff --git a/src/modules/Gmp/testsuite.in b/src/modules/Gmp/testsuite.in index 7b568cca4b35fc1b5dfe18076fb760920ec04a29..dbd025eb98aa826d2b5ffac2892e7f66aebdd0a3 100644 --- a/src/modules/Gmp/testsuite.in +++ b/src/modules/Gmp/testsuite.in @@ -320,6 +320,12 @@ cond_begin([[ master()->resolv("Gmp")->mpq ]]) test_true(stringp((string)Gmp.mpq(17))) test_eq((string)Gmp.mpq(17),"17") test_eq((string)Gmp.mpq(1.25),"1.25") + test_eq((string)Gmp.mpq(0.25),"0.25") + test_eq((string)Gmp.mpq(-0.25),"-0.25") + test_eq((string)Gmp.mpq(0.5), "0.5") + test_eq((string)Gmp.mpq(-0.5), "-0.5") + test_eq((string)Gmp.mpq(0.75),"0.75") + test_eq((string)Gmp.mpq(-0.75),"-0.75") test_eq((int)Gmp.mpq(17),17) test_false(Gmp.mpq(0)) test_true(Gmp.mpq(1))