Fix from_man_exp to correctly reject non-integral mantissa

This partially reverts 25506567
This commit is contained in:
Sergey B Kirpichev
2025-06-09 05:07:00 +03:00
parent bad54feab6
commit 9ad6a13925
3 changed files with 11 additions and 3 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ class _mpf(mpnumeric):
if len(val) == 4:
val = val[0], MPZ(val[1]), *val[2:]
elif len(val) == 2:
v._mpf_ = from_man_exp(MPZ(val[0]), val[1], prec, rounding)
v._mpf_ = from_man_exp(val[0], val[1], prec, rounding)
return v
else:
raise ValueError
+5 -2
View File
@@ -8,7 +8,7 @@ import re
import sys
import warnings
from .backend import BACKEND, MPZ, MPZ_FIVE, MPZ_ONE, MPZ_ZERO, gmpy
from .backend import BACKEND, MPZ, MPZ_FIVE, MPZ_ONE, MPZ_ZERO, gmpy, int_types
from .libintmath import (bctable, bin_to_radix, isqrt, numeral, sqrtrem,
stddigits, trailtable)
@@ -204,7 +204,10 @@ def normalize(sign, man, exp, bc, prec, rnd):
def from_man_exp(man, exp, prec=0, rnd=round_fast):
"""Create raw mpf from (man, exp) pair. The mantissa may be signed.
If no precision is specified, the mantissa is stored exactly."""
man = MPZ(man)
if isinstance(man, int_types):
man = MPZ(man)
else:
raise TypeError("man expected to be an integer")
sign = 0
if man < 0:
sign = 1
+5
View File
@@ -685,3 +685,8 @@ def test_rounding_prop():
assert mp.sin(1) == mpf('0x1.aed548f090cefp-1')
with pytest.raises(ValueError):
mp.rounding = 'x'
def test_from_man_exp():
with pytest.raises(TypeError):
mp.mpf(("!", 1))