@@ -864,6 +864,28 @@ def test_short_repr(self):
864
864
@support .requires_IEEE_754
865
865
class RoundTestCase (unittest .TestCase ):
866
866
867
+ def assertFloatsAreIdentical (self , x , y ):
868
+ """Fail unless floats x and y are identical, in the sense that:
869
+ (1) both x and y are nans, or
870
+ (2) both x and y are infinities, with the same sign, or
871
+ (3) both x and y are zeros, with the same sign, or
872
+ (4) x and y are both finite and nonzero, and x == y
873
+ """
874
+ msg = 'floats {!r} and {!r} are not identical'
875
+
876
+ if isnan (x ) or isnan (y ):
877
+ if isnan (x ) and isnan (y ):
878
+ return
879
+ elif x == y :
880
+ if x != 0.0 :
881
+ return
882
+ # both zero; check that signs match
883
+ elif copysign (1.0 , x ) == copysign (1.0 , y ):
884
+ return
885
+ else :
886
+ msg += ': zeros have different signs'
887
+ self .fail (msg .format (x , y ))
888
+
867
889
def test_inf_nan (self ):
868
890
self .assertRaises (OverflowError , round , INF )
869
891
self .assertRaises (OverflowError , round , - INF )
@@ -892,10 +914,10 @@ def test_large_n(self):
892
914
893
915
def test_small_n (self ):
894
916
for n in [- 308 , - 309 , - 400 , 1 - 2 ** 31 , - 2 ** 31 , - 2 ** 31 - 1 , - 2 ** 100 ]:
895
- self .assertEqual (round (123.456 , n ), 0.0 )
896
- self .assertEqual (round (- 123.456 , n ), - 0.0 )
897
- self .assertEqual (round (1e300 , n ), 0.0 )
898
- self .assertEqual (round (1e-320 , n ), 0.0 )
917
+ self .assertFloatsAreIdentical (round (123.456 , n ), 0.0 )
918
+ self .assertFloatsAreIdentical (round (- 123.456 , n ), - 0.0 )
919
+ self .assertFloatsAreIdentical (round (1e300 , n ), 0.0 )
920
+ self .assertFloatsAreIdentical (round (1e-320 , n ), 0.0 )
899
921
900
922
def test_overflow (self ):
901
923
self .assertRaises (OverflowError , round , 1.6e308 , - 308 )
0 commit comments