Skip to content

Commit

Permalink
Fix creating of mpfr's with base=0 for negative values
Browse files Browse the repository at this point in the history
Closes #530
  • Loading branch information
skirpichev committed Nov 22, 2024
1 parent 54c1145 commit f4ef93e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 20 deletions.
20 changes: 0 additions & 20 deletions src/gmpy2_convert_mpfr.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,26 +305,6 @@ GMPy_MPFR_From_PyStr(PyObject *s, int base, mpfr_prec_t prec, CTXT_Object *conte
len = PyBytes_Size(ascii_str);
cp = PyBytes_AsString(ascii_str);

/* Check for leading base indicators. */
if (base == 0) {
if (len > 2 && cp[0] == '0') {
if (cp[1] == 'b') { base = 2; cp += 2; len -= 2; }
else if (cp[1] == 'x') { base = 16; cp += 2; len -= 2; }
else { base = 10; }
}
else {
base = 10;
}
}
else if (cp[0] == '0') {
/* If the specified base matches the leading base indicators, then
* we need to skip the base indicators.
*/
if (cp[1] =='b' && base == 2) { cp += 2; len -= 2; }
else if (cp[1] =='x' && base == 16) { cp += 2; len -= 2; }
}


if (!(result = GMPy_MPFR_New(prec, context))) {
Py_XDECREF(ascii_str);
return NULL;
Expand Down
3 changes: 3 additions & 0 deletions test/test_mpfr.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def test_mpfr_create():
assert mpfr(-1) == mpfr('-1.0')
assert mpfr("123e17") == mpfr('1.23e+19')
assert mpfr('1._3_5e4_5') == mpfr('1.3499999999999999e+45')
assert mpfr('-0b1.1000000000001p+4') == mpfr('-24.001953125')
assert mpfr('-0x1.9000000000000p+4') == mpfr('-25.0')
assert mpfr('-0x1.9p+4') == mpfr('-25.0')

pytest.raises(ValueError, lambda: mpfr("foo"))

Expand Down

0 comments on commit f4ef93e

Please sign in to comment.