Next / Previous / Contents / TCC Help System / NM Tech homepage

10.4. MixedUnits.singleToMix(): Convert to mixed units

This method takes a single quantity, in the largest unit of the system, and converts it to mixed units.

sidereal.py
# - - -   M i x e d U n i t s . s i n g l e T o M i x

    def singleToMix ( self, value ):
        """Convert to mixed units.

          [ value is a float ->
              return value as a sequence of coefficients in
              self's system ]
        """

The first element of the result list is the whole part of the value.

sidereal.py
        #-- 1 --
        # [ whole  :=  whole part of value
        #   frac  :=  fractional part of value ]
        whole, frac  =  divmod ( value, 1.0 )
        result  =  [int(whole)]

For each factor in the system, we then multiply the fraction from the previous step by that factor, and separate the result into whole and fractional parts.

sidereal.py
        #-- 2 --
        # [ result  :=  result with integral parts of value
        #               in self's system appended ]
        for  factorx in range(len(self.factors)):
            frac  *=  self.factors[factorx]
            whole, frac  =  divmod ( frac, 1.0 )
            result.append ( int(whole) )

After the loop above, frac contains the fractional part of the value in the smallest unit, so we must add it to the last element of the result.

sidereal.py
        #-- 3 --
        # [ result  :=  result with frac added to its last element ]
        result[-1]  +=  frac

        #-- 4 --
        return result