def _decimal_to_Rational_prec(dec):
 if (not dec.is_finite()):
  raise TypeError(('dec must be finite, got %s.' % dec))
 (s, d, e) = dec.as_tuple()
 prec = len(d)
 if (e >= 0):
  rv = Integer(int(dec))
 else:
  s = ((-1) ** s)
  d = sum([(di * (10 ** i)) for (i, di) in enumerate(reversed(d))])
  rv = Rational((s * d), (10 ** (- e)))
 return (rv, prec)

def _decimal_to_Rational_prec ( dec ) : if ( not dec . is_finite ( ) ) : raise TypeError ( ( 'dec must be finite, got %s.' % dec ) ) ( s , d , e ) = dec . as_tuple ( ) prec = len ( d ) if ( e >= 0 ) : rv = Integer ( int ( dec ) ) else : s = ( ( - 1 ) ** s ) d = sum ( [ ( di * ( 10 ** i ) ) for ( i , di ) in enumerate ( reversed ( d ) ) ] ) rv = Rational ( ( s * d ) , ( 10 ** ( - e ) ) ) return ( rv , prec )
convert an ordinary decimal instance to a rational .