def _integer_rational_reconstruction(c, m, domain):
 if (c < 0):
  c += m
 (r0, s0) = (m, domain.zero)
 (r1, s1) = (c, domain.one)
 bound = sqrt((m / 2))
 while (r1 >= bound):
  quo = (r0 // r1)
  (r0, r1) = (r1, (r0 - (quo * r1)))
  (s0, s1) = (s1, (s0 - (quo * s1)))
 if (abs(s1) >= bound):
  return None
 if (s1 < 0):
  (a, b) = ((- r1), (- s1))
 elif (s1 > 0):
  (a, b) = (r1, s1)
 else:
  return None
 field = domain.get_field()
 return (field(a) / field(b))

def _integer_rational_reconstruction ( c , m , domain ) : if ( c < 0 ) : c += m ( r0 , s0 ) = ( m , domain . zero ) ( r1 , s1 ) = ( c , domain . one ) bound = sqrt ( ( m / 2 ) ) while ( r1 >= bound ) : quo = ( r0 // r1 ) ( r0 , r1 ) = ( r1 , ( r0 - ( quo * r1 ) ) ) ( s0 , s1 ) = ( s1 , ( s0 - ( quo * s1 ) ) ) if ( abs ( s1 ) >= bound ) : return None if ( s1 < 0 ) : ( a , b ) = ( ( - r1 ) , ( - s1 ) ) elif ( s1 > 0 ) : ( a , b ) = ( r1 , s1 ) else : return None field = domain . get_field ( ) return ( field ( a ) / field ( b ) )
reconstruct a rational number frac a b from .