@@ -1635,139 +1635,141 @@ def msum(iterable):
1635
1635
self .assertRaises (ValueError , math .fsum , [1. , 2 , INF , NINF ])
1636
1636
self .assertEqual (math .fsum ([1. , 2 , INF , INF ]), INF )
1637
1637
1638
- class IsCloseTests (unittest .TestCase ):
1639
- isclose = staticmethod (math .isclose ) # sublcasses should override this
1640
-
1641
- def assertIsClose (self , a , b , * args , ** kwargs ):
1642
- self .assertTrue (self .isclose (a , b , * args , ** kwargs ),
1643
- msg = "%s and %s should be close!" % (a , b ))
1644
-
1645
- def assertIsNotClose (self , a , b , * args , ** kwargs ):
1646
- self .assertFalse (self .isclose (a , b , * args , ** kwargs ),
1647
- msg = "%s and %s should not be close!" % (a , b ))
1648
-
1649
- def assertAllClose (self , examples , * args , ** kwargs ):
1650
- for a , b in examples :
1651
- self .assertIsClose (a , b , * args , ** kwargs )
1652
-
1653
- def assertAllNotClose (self , examples , * args , ** kwargs ):
1654
- for a , b in examples :
1655
- self .assertIsNotClose (a , b , * args , ** kwargs )
1656
-
1657
- def test_negative_tolerances (self ):
1658
- # ValueError should be raised if either tolerance is less than zero
1659
- with self .assertRaises (ValueError ):
1660
- self .assertIsClose (1 , 1 , rel_tol = - 1e-100 )
1661
- with self .assertRaises (ValueError ):
1662
- self .assertIsClose (1 , 1 , rel_tol = 1e-100 , abs_tol = - 1e10 )
1663
-
1664
- def test_identical (self ):
1665
- # identical values must test as close
1666
- identical_examples = [(2.0 , 2.0 ),
1667
- (0.1e200 , 0.1e200 ),
1668
- (1.123e-300 , 1.123e-300 ),
1669
- (12345 , 12345.0 ),
1670
- (0.0 , - 0.0 ),
1671
- (345678 , 345678 )]
1672
- self .assertAllClose (identical_examples , rel_tol = 0.0 , abs_tol = 0.0 )
1673
-
1674
- def test_eight_decimal_places (self ):
1675
- # examples that are close to 1e-8, but not 1e-9
1676
- eight_decimal_places_examples = [(1e8 , 1e8 + 1 ),
1677
- (- 1e-8 , - 1.000000009e-8 ),
1678
- (1.12345678 , 1.12345679 )]
1679
- self .assertAllClose (eight_decimal_places_examples , rel_tol = 1e-8 )
1680
- self .assertAllNotClose (eight_decimal_places_examples , rel_tol = 1e-9 )
1681
-
1682
- def test_near_zero (self ):
1683
- # values close to zero
1684
- near_zero_examples = [(1e-9 , 0.0 ),
1685
- (- 1e-9 , 0.0 ),
1686
- (- 1e-150 , 0.0 )]
1687
- # these should not be close to any rel_tol
1688
- self .assertAllNotClose (near_zero_examples , rel_tol = 0.9 )
1689
- # these should be close to abs_tol=1e-8
1690
- self .assertAllClose (near_zero_examples , abs_tol = 1e-8 )
1691
-
1692
- def test_identical_infinite (self ):
1693
- # these are close regardless of tolerance -- i.e. they are equal
1694
- self .assertIsClose (INF , INF )
1695
- self .assertIsClose (INF , INF , abs_tol = 0.0 )
1696
- self .assertIsClose (NINF , NINF )
1697
- self .assertIsClose (NINF , NINF , abs_tol = 0.0 )
1698
-
1699
- def test_inf_ninf_nan (self ):
1700
- # these should never be close (following IEEE 754 rules for equality)
1701
- not_close_examples = [(NAN , NAN ),
1702
- (NAN , 1e-100 ),
1703
- (1e-100 , NAN ),
1704
- (INF , NAN ),
1705
- (NAN , INF ),
1706
- (INF , NINF ),
1707
- (INF , 1.0 ),
1708
- (1.0 , INF ),
1709
- (INF , 1e308 ),
1710
- (1e308 , INF )]
1711
- # use largest reasonable tolerance
1712
- self .assertAllNotClose (not_close_examples , abs_tol = 0.999999999999999 )
1713
-
1714
- def test_zero_tolerance (self ):
1715
- # test with zero tolerance
1716
- zero_tolerance_close_examples = [(1.0 , 1.0 ),
1717
- (- 3.4 , - 3.4 ),
1718
- (- 1e-300 , - 1e-300 )]
1719
- self .assertAllClose (zero_tolerance_close_examples , rel_tol = 0.0 )
1720
-
1721
- zero_tolerance_not_close_examples = [(1.0 , 1.000000000000001 ),
1722
- (0.99999999999999 , 1.0 ),
1723
- (1.0e200 , .999999999999999e200 )]
1724
- self .assertAllNotClose (zero_tolerance_not_close_examples , rel_tol = 0.0 )
1725
-
1726
- def test_asymmetry (self ):
1727
- # test the asymmetry example from PEP 485
1728
- self .assertAllClose ([(9 , 10 ), (10 , 9 )], rel_tol = 0.1 )
1729
-
1730
- def test_integers (self ):
1731
- # test with integer values
1732
- integer_examples = [(100000001 , 100000000 ),
1733
- (123456789 , 123456788 )]
1734
-
1735
- self .assertAllClose (integer_examples , rel_tol = 1e-8 )
1736
- self .assertAllNotClose (integer_examples , rel_tol = 1e-9 )
1737
-
1738
- # TODO the test is commented out due to GR-10712
1739
- '''
1740
- def test_decimals(self):
1741
- # test with Decimal values
1742
- from decimal import Decimal#
1743
-
1744
- decimal_examples = [(Decimal('1.00000001'), Decimal('1.0')),
1745
- (Decimal('1.00000001e-20'), Decimal('1.0e-20')),
1746
- (Decimal('1.00000001e-100'), Decimal('1.0e-100')),
1747
- (Decimal('1.00000001e20'), Decimal('1.0e20'))]
1748
- self.assertAllClose(decimal_examples, rel_tol=1e-8)
1749
- self.assertAllNotClose(decimal_examples, rel_tol=1e-9)
1750
- '''
1751
-
1752
- # TODO the test is commented out due to GR-10711
1753
- '''
1754
- def test_fractions(self):
1755
- # test with Fraction values
1756
- from fractions import Fraction
1757
-
1758
- fraction_examples = [
1759
- (Fraction(1, 100000000) + 1, Fraction(1)),
1760
- (Fraction(100000001), Fraction(100000000)),
1761
- (Fraction(10**8 + 1, 10**28), Fraction(1, 10**20))]
1762
- self.assertAllClose(fraction_examples, rel_tol=1e-8)
1763
- self.assertAllNotClose(fraction_examples, rel_tol=1e-9)
1764
- '''
1765
- def test_objects (self ):
1766
- # these are close regardless of tolerance -- i.e. they are equal
1767
- self .assertIsClose (MyFloat (), MyFloat ())
1768
- self .assertIsClose (MyFloat (), MyFloat (), abs_tol = 0.0 )
1769
- self .assertIsClose (MyFloat (), MyFloat (), abs_tol = MyFloat ())
1770
- self .assertIsClose (MyFloat (), MyFloat (), rel_tol = 0.0 )
1771
- self .assertIsClose (MyFloat (), MyFloat (), rel_tol = MyFloat ())
1772
-
1773
- self .assertIsNotClose (MyFloat (), 10 )
1638
+ if (sys .version_info .major >= 3 and sys .version_info .minor >= 5 ):
1639
+ # math.isclose since 3.5
1640
+ class IsCloseTests (unittest .TestCase ):
1641
+ isclose = staticmethod (math .isclose ) # sublcasses should override this
1642
+
1643
+ def assertIsClose (self , a , b , * args , ** kwargs ):
1644
+ self .assertTrue (self .isclose (a , b , * args , ** kwargs ),
1645
+ msg = "%s and %s should be close!" % (a , b ))
1646
+
1647
+ def assertIsNotClose (self , a , b , * args , ** kwargs ):
1648
+ self .assertFalse (self .isclose (a , b , * args , ** kwargs ),
1649
+ msg = "%s and %s should not be close!" % (a , b ))
1650
+
1651
+ def assertAllClose (self , examples , * args , ** kwargs ):
1652
+ for a , b in examples :
1653
+ self .assertIsClose (a , b , * args , ** kwargs )
1654
+
1655
+ def assertAllNotClose (self , examples , * args , ** kwargs ):
1656
+ for a , b in examples :
1657
+ self .assertIsNotClose (a , b , * args , ** kwargs )
1658
+
1659
+ def test_negative_tolerances (self ):
1660
+ # ValueError should be raised if either tolerance is less than zero
1661
+ with self .assertRaises (ValueError ):
1662
+ self .assertIsClose (1 , 1 , rel_tol = - 1e-100 )
1663
+ with self .assertRaises (ValueError ):
1664
+ self .assertIsClose (1 , 1 , rel_tol = 1e-100 , abs_tol = - 1e10 )
1665
+
1666
+ def test_identical (self ):
1667
+ # identical values must test as close
1668
+ identical_examples = [(2.0 , 2.0 ),
1669
+ (0.1e200 , 0.1e200 ),
1670
+ (1.123e-300 , 1.123e-300 ),
1671
+ (12345 , 12345.0 ),
1672
+ (0.0 , - 0.0 ),
1673
+ (345678 , 345678 )]
1674
+ self .assertAllClose (identical_examples , rel_tol = 0.0 , abs_tol = 0.0 )
1675
+
1676
+ def test_eight_decimal_places (self ):
1677
+ # examples that are close to 1e-8, but not 1e-9
1678
+ eight_decimal_places_examples = [(1e8 , 1e8 + 1 ),
1679
+ (- 1e-8 , - 1.000000009e-8 ),
1680
+ (1.12345678 , 1.12345679 )]
1681
+ self .assertAllClose (eight_decimal_places_examples , rel_tol = 1e-8 )
1682
+ self .assertAllNotClose (eight_decimal_places_examples , rel_tol = 1e-9 )
1683
+
1684
+ def test_near_zero (self ):
1685
+ # values close to zero
1686
+ near_zero_examples = [(1e-9 , 0.0 ),
1687
+ (- 1e-9 , 0.0 ),
1688
+ (- 1e-150 , 0.0 )]
1689
+ # these should not be close to any rel_tol
1690
+ self .assertAllNotClose (near_zero_examples , rel_tol = 0.9 )
1691
+ # these should be close to abs_tol=1e-8
1692
+ self .assertAllClose (near_zero_examples , abs_tol = 1e-8 )
1693
+
1694
+ def test_identical_infinite (self ):
1695
+ # these are close regardless of tolerance -- i.e. they are equal
1696
+ self .assertIsClose (INF , INF )
1697
+ self .assertIsClose (INF , INF , abs_tol = 0.0 )
1698
+ self .assertIsClose (NINF , NINF )
1699
+ self .assertIsClose (NINF , NINF , abs_tol = 0.0 )
1700
+
1701
+ def test_inf_ninf_nan (self ):
1702
+ # these should never be close (following IEEE 754 rules for equality)
1703
+ not_close_examples = [(NAN , NAN ),
1704
+ (NAN , 1e-100 ),
1705
+ (1e-100 , NAN ),
1706
+ (INF , NAN ),
1707
+ (NAN , INF ),
1708
+ (INF , NINF ),
1709
+ (INF , 1.0 ),
1710
+ (1.0 , INF ),
1711
+ (INF , 1e308 ),
1712
+ (1e308 , INF )]
1713
+ # use largest reasonable tolerance
1714
+ self .assertAllNotClose (not_close_examples , abs_tol = 0.999999999999999 )
1715
+
1716
+ def test_zero_tolerance (self ):
1717
+ # test with zero tolerance
1718
+ zero_tolerance_close_examples = [(1.0 , 1.0 ),
1719
+ (- 3.4 , - 3.4 ),
1720
+ (- 1e-300 , - 1e-300 )]
1721
+ self .assertAllClose (zero_tolerance_close_examples , rel_tol = 0.0 )
1722
+
1723
+ zero_tolerance_not_close_examples = [(1.0 , 1.000000000000001 ),
1724
+ (0.99999999999999 , 1.0 ),
1725
+ (1.0e200 , .999999999999999e200 )]
1726
+ self .assertAllNotClose (zero_tolerance_not_close_examples , rel_tol = 0.0 )
1727
+
1728
+ def test_asymmetry (self ):
1729
+ # test the asymmetry example from PEP 485
1730
+ self .assertAllClose ([(9 , 10 ), (10 , 9 )], rel_tol = 0.1 )
1731
+
1732
+ def test_integers (self ):
1733
+ # test with integer values
1734
+ integer_examples = [(100000001 , 100000000 ),
1735
+ (123456789 , 123456788 )]
1736
+
1737
+ self .assertAllClose (integer_examples , rel_tol = 1e-8 )
1738
+ self .assertAllNotClose (integer_examples , rel_tol = 1e-9 )
1739
+
1740
+ # TODO the test is commented out due to GR-10712
1741
+ '''
1742
+ def test_decimals(self):
1743
+ # test with Decimal values
1744
+ from decimal import Decimal#
1745
+
1746
+ decimal_examples = [(Decimal('1.00000001'), Decimal('1.0')),
1747
+ (Decimal('1.00000001e-20'), Decimal('1.0e-20')),
1748
+ (Decimal('1.00000001e-100'), Decimal('1.0e-100')),
1749
+ (Decimal('1.00000001e20'), Decimal('1.0e20'))]
1750
+ self.assertAllClose(decimal_examples, rel_tol=1e-8)
1751
+ self.assertAllNotClose(decimal_examples, rel_tol=1e-9)
1752
+ '''
1753
+
1754
+ # TODO the test is commented out due to GR-10711
1755
+ '''
1756
+ def test_fractions(self):
1757
+ # test with Fraction values
1758
+ from fractions import Fraction
1759
+
1760
+ fraction_examples = [
1761
+ (Fraction(1, 100000000) + 1, Fraction(1)),
1762
+ (Fraction(100000001), Fraction(100000000)),
1763
+ (Fraction(10**8 + 1, 10**28), Fraction(1, 10**20))]
1764
+ self.assertAllClose(fraction_examples, rel_tol=1e-8)
1765
+ self.assertAllNotClose(fraction_examples, rel_tol=1e-9)
1766
+ '''
1767
+ def test_objects (self ):
1768
+ # these are close regardless of tolerance -- i.e. they are equal
1769
+ self .assertIsClose (MyFloat (), MyFloat ())
1770
+ self .assertIsClose (MyFloat (), MyFloat (), abs_tol = 0.0 )
1771
+ self .assertIsClose (MyFloat (), MyFloat (), abs_tol = MyFloat ())
1772
+ self .assertIsClose (MyFloat (), MyFloat (), rel_tol = 0.0 )
1773
+ self .assertIsClose (MyFloat (), MyFloat (), rel_tol = MyFloat ())
1774
+
1775
+ self .assertIsNotClose (MyFloat (), 10 )
0 commit comments