Example #1
0
    def test_div_known_Values(self):
        #__div__ should give known result with known input
        #the unit of the product should be the product of the units

        #scalar division
        x = PhysicalQuantity('1cm')
        y = 12.3
        z = 1/12.3
        self.assertAlmostEqual((x/y).value, PhysicalQuantity('%f cm'%z).value, 4)
        self.assertEqual((x/y).unit, PhysicalQuantity('%f cm'%z).unit)
        self.assertEqual(y/x, PhysicalQuantity('12.3cm**-1'))

        #unitless result
        x = PhysicalQuantity('1.0m')
        y = PhysicalQuantity('5m')
        quo = 1.0/5
        # if quotient is unit-less (that is, x and y are additively compatible)
        # re-arranges x & y in terms of the known quotient and __rdiv__ and checks for consistency
        self.assertEqual((x/y), quo)
        self.assertEqual(x.__rdiv__(y), 1/quo)
        self.assertEqual(type(x/y), float)

        x = PhysicalQuantity('3cm')
        y = PhysicalQuantity('5s')
        quo = 3.0/5
        # if quotient has a unit (x and y are additively incompatible)
        # re-arranges x & y in terms of the known quotient and __rdiv__ and checks for consistency
        self.assertEqual((x/y).value, quo)
        self.assertEqual(x.__rdiv__(y).value, 1/quo)

        self.assertEqual((x/y).unit, x.unit/y.unit)
        self.assertEqual(x.__rdiv__(y).unit, y.unit/x.unit)
        self.assertEqual(str(x/y), '0.6 cm/s')