division for compound units not working properly

Bug #311499 reported by John Salvatier
4
Affects Status Importance Assigned to Milestone
python-quantities
Fix Released
Critical
Darren Dale

Bug Description

These two should be the same, but they are not

>>>(5.2 * eV) / (400.0 * eV)
Quantity(0.013000000000000001), (J)^2
>>>(1.0 * J ) / ( 5.3 * J)
Quantity(0.18867924528301888), (dimensionless)

Darren Dale (dsdale24)
Changed in python-quantities:
assignee: nobody → dsdale24
importance: Undecided → Critical
Darren Dale (dsdale24)
Changed in python-quantities:
milestone: none → 0.1
Revision history for this message
John Salvatier (jsalvatier) wrote :

I think I fixed this:

in dimensionality.py I changed the __div__ function and got the correct behavior:

   def __div__(self, other):
        if not isinstance(other, Dimensionality):
            scaling = self.scaling / float(other)
            units = self.units
            dimensions = copy.deepcopy(self._dimensions)
        else:
            units = []
            old = []
            new = []
            for su, ou, od, in zip(self._units, other._units, other._dimensions):
                if (su and ou) and (su != ou):
                    old.append(ou+'^%d'%od)
                    new.append(su+'^%d'%od)
                if ou and not su: units.append(ou)
                else: units.append(su)
            old = '*'.join(old)
            new = '*'.join(new)
            scaling, offset = _udunits.convert(old, new)
            dimensions = self._dimensions - other._dimensions
            compound = copy.deepcopy(self._compound)
            for k, v in other._compound.items():
                if k in self._compound:
                    compound[k] -= v # changed this from += to -=
                else:
                    compound[k] = -v # changed from = v to = -v
        new = self.__class__(units, dimensions, **compound)
        new._scaling = scaling
        new._offset = offset
        return new

Revision history for this message
John Salvatier (jsalvatier) wrote :

The only changes above where changing the signs on the compound part

Revision history for this message
Darren Dale (dsdale24) wrote :

Thanks John. I applied your change and pushed it to launchpad. You can update your branch by doing:

bzr pull

Changed in python-quantities:
status: New → Fix Committed
Darren Dale (dsdale24)
Changed in python-quantities:
milestone: none → 0.5.0
Darren Dale (dsdale24)
Changed in python-quantities:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.