Example #1
0
 def testExceptions(self):
     """
     Test exceptions.
     """
     with self.assertRaises(BasesError):
         Radices.from_rational(Fraction(1, 2), 0)
     with self.assertRaises(BasesError):
         Radices.from_rational(Fraction(1, 2), 2, precision=-1)
Example #2
0
 def test_exceptions(self):
     """
     Test exceptions.
     """
     with self.assertRaises(BasesError):
         Radices.from_rational(Fraction(1, 2), 0)
     with self.assertRaises(BasesError):
         Radices.from_rational(Fraction(1, 2), 2, -1)
Example #3
0
 def testInverses(self, value, to_base):
     """
     Test that functions are inverses of each other.
     """
     (result, relation) = Radices.from_rational(value, to_base)
     assert result.sign in (0, 1) or value < 0
     assert relation == 0
     assert result.as_rational() == value
Example #4
0
 def test_inverses(self, value, to_base):
     """
     Test that functions are inverses of each other.
     """
     (result, relation) = Radices.from_rational(value, to_base)
     self.assertTrue(result.sign in (0, 1) or value < 0)
     self.assertEqual(relation, 0)
     self.assertEqual(result.as_rational(), value)
Example #5
0
    def testRoundingConversion(
       self,
       value,
       base,
       precision,
       method,
       expand
    ):
        """
        Test that converting and then rounding is the same as converting
        with rounding.
        """
        # pylint: disable=too-many-arguments
        (rounded, rel) = \
           Radices.from_rational(
              value,
              base,
              precision=precision,
              method=method,
              expand_repeating=expand
        )
        (unrounded, urel) = Radices.from_rational(value, base)

        assert urel == 0

        (frounded, frel) = unrounded.rounded(precision, method)

        if expand:
            assert frounded == rounded
            assert rel == frel

        rounded_value = rounded.as_rational()

        if rounded_value > value:
            assert rel > 0 and rel < 1
        elif rounded_value < value:
            assert rel < 0 and rel > -1
        else:
            assert rel == 0
Example #6
0
    def test_rounding_conversion(self, value, base, precision, method):
        """
        Test that converting and then rounding is the same as converting
        with rounding.
        """
        (rounded, rel) = Radices.from_rational(value, base, precision, method)
        (unrounded, urel) = Radices.from_rational(value, base)

        self.assertEqual(urel, 0)

        (frounded, frel) = unrounded.rounded(precision, method)

        self.assertEqual(frounded, rounded)
        self.assertEqual(rel, frel)

        rounded_value = rounded.as_rational()

        if rounded_value > value:
            self.assertEqual(rel, 1)
        elif rounded_value < value:
            self.assertEqual(rel, -1)
        else:
            self.assertEqual(rel, 0)
Example #7
0
def main():
    ratio = Fraction(1024, 1000)

    exponents = range(0, 9)

    increases = [ratio**exp - 1 for exp in exponents]

    percentages = [100 * x for x in increases]

    radices = [Radices.from_rational(x, 10, 2)[0] for x in percentages]

    rationals = [x.as_rational() for x in radices]

    percent_values = \
       [Decimal(x.numerator)/Decimal(x.denominator) for x in rationals]

    plt.title('Percent Difference between Values in SI and IEC Units')
    plt.xticks(exponents, ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'])
    plt.ylabel("Percent Increase: (SI - IEC)/IEC")
    plt.xlabel("Unit Size")
    plt.plot(exponents, percent_values)

    plt.show()
def main():
    ratio = Fraction(1024, 1000)

    exponents = range(0, 9)

    increases = [ratio ** exp - 1 for exp in exponents]

    percentages = [100 * x for x in increases]

    radices = [Radices.from_rational(x, 10, 2)[0] for x in percentages] 

    rationals = [x.as_rational() for x in radices] 

    percent_values = \
       [Decimal(x.numerator)/Decimal(x.denominator) for x in rationals]

    plt.title('Percent Difference between Values in SI and IEC Units')
    plt.xticks(exponents, ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'])
    plt.ylabel("Percent Increase: (SI - IEC)/IEC")
    plt.xlabel("Unit Size")
    plt.plot(exponents, percent_values) 

    plt.show()