From 28c531d9f2821cb78b8c4799267c80eedd6ae654 Mon Sep 17 00:00:00 2001 From: "Stephen R. van den Berg" <srb@cuci.nl> Date: Wed, 30 May 2018 16:03:07 +0200 Subject: [PATCH] Gmp.mpq: Ensure a leading zero for numbers between -1.0 and 1.0. --- src/modules/Gmp/mpq.cmod | 7 ++++++- src/modules/Gmp/testsuite.in | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/modules/Gmp/mpq.cmod b/src/modules/Gmp/mpq.cmod index fec5d2abac..5b9ba05c7e 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 7b568cca4b..dbd025eb98 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)) -- GitLab