File tree 3 files changed +28
-11
lines changed
Filter options
3 files changed +28
-11
lines changed
Original file line number Diff line number Diff line change @@ -734,18 +734,23 @@ def from_float(cls, f):
734
734
735
735
"""
736
736
if isinstance (f , int ): # handle integer inputs
737
- return cls (f )
738
- if not isinstance (f , float ):
739
- raise TypeError ("argument must be int or float." )
740
- if _math .isinf (f ) or _math .isnan (f ):
741
- return cls (repr (f ))
742
- if _math .copysign (1.0 , f ) == 1.0 :
743
- sign = 0
737
+ sign = 0 if f >= 0 else 1
738
+ k = 0
739
+ coeff = str (abs (f ))
740
+ elif isinstance (f , float ):
741
+ if _math .isinf (f ) or _math .isnan (f ):
742
+ return cls (repr (f ))
743
+ if _math .copysign (1.0 , f ) == 1.0 :
744
+ sign = 0
745
+ else :
746
+ sign = 1
747
+ n , d = abs (f ).as_integer_ratio ()
748
+ k = d .bit_length () - 1
749
+ coeff = str (n * 5 ** k )
744
750
else :
745
- sign = 1
746
- n , d = abs (f ).as_integer_ratio ()
747
- k = d .bit_length () - 1
748
- result = _dec_from_triple (sign , str (n * 5 ** k ), - k )
751
+ raise TypeError ("argument must be int or float." )
752
+
753
+ result = _dec_from_triple (sign , coeff , - k )
749
754
if cls is Decimal :
750
755
return result
751
756
else :
Original file line number Diff line number Diff line change @@ -1185,6 +1185,16 @@ def test_wide_char_separator_decimal_point(self):
1185
1185
self .assertEqual (format (Decimal ('100000000.123' ), 'n' ),
1186
1186
'100\u066c 000\u066c 000\u066b 123' )
1187
1187
1188
+ def test_decimal_from_float_argument_type (self ):
1189
+ class A (self .decimal .Decimal ):
1190
+ def __init__ (self , a ):
1191
+ self .a_type = type (a )
1192
+ a = A .from_float (42.5 )
1193
+ self .assertEqual (self .decimal .Decimal , a .a_type )
1194
+
1195
+ a = A .from_float (42 )
1196
+ self .assertEqual (self .decimal .Decimal , a .a_type )
1197
+
1188
1198
class CFormatTest (FormatTest ):
1189
1199
decimal = C
1190
1200
class PyFormatTest (FormatTest ):
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ What's New in Python 3.7.0 alpha 1?
10
10
Core and Builtins
11
11
-----------------
12
12
13
+ - bpo-29534: Fixed different behaviour of Decimal.from_float() for _decimal and _pydecimal.
14
+
13
15
- bpo-29438: Fixed use-after-free problem in key sharing dict.
14
16
15
17
- Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
You can’t perform that action at this time.
0 commit comments