note
	description: "[
				An arbitrary precision library for Rational numbers.
		
				Creation
					make,
					make_from_ints,
					make_from_string,
					make_from_real32,
					make_from_real64,
		
				Queries
					absolute: RATIONAL
					add (other: RATIONAL): RATIONAL
					as_real32: REAL_32
					as_real64: REAL_64
					divide alias "//" (other: RATIONAL): RATIONAL
					is_equal (other: RATIONAL): BOOLEAN
					is_greater alias ">" (other: RATIONAL): BOOLEAN
					is_greater_equal alias ">=" (other: RATIONAL): BOOLEAN
					is_less alias "<" (other: RATIONAL): BOOLEAN
					is_less_equal alias "<=" (other: RATIONAL): BOOLEAN
					is_valid_real_32: BOOLEAN
					is_valid_real_64: BOOLEAN
					max (other: [like Current] RATIONAL): RATIONAL
					min (other: [like Current] RATIONAL): RATIONAL
					minus alias "-" (other: RATIONAL): RATIONAL
					multiply (other: RATIONAL): RATIONAL
					negate: RATIONAL
					opposite alias "-": RATIONAL
					out: STRING_8
					plus alias "+" (other: RATIONAL): RATIONAL
					power alias "^" (other: INTEGER): RATIONAL
					product alias "*" (other: RATIONAL): RATIONAL
					quotient alias "/" (other: RATIONAL): RATIONAL
					reciprocal: RATIONAL
					round_to (digits: INTEGER): STRING
					square: RATIONAL
					string_is_float (s: STRING): BOOLEAN
					string_is_fraction (s: STRING): BOOLEAN
					string_is_rational (s: STRING): BOOLEAN
					subtract (other: RATIONAL): RATIONAL
		
				Commands
					canonicalize
	]"
	author: "JSO and AB"
	date: "$Date$"
	revision: "$Revision$"

class interface
	RATIONAL

create 
	make (a_p, a_q: INTEGER_64)
			-- Initialization for Current.
		require
			q_non_zero: a_q /= 0

	make_from_ints (a_p, a_q: BIG_INTEGER)
			-- Create by specifying the pand q values
		require
			q_non_zero: a_q /~ a_q.zero

	make_from_string (s: STRING_8)
			-- Create from string s
		require
			has_correct_format: string_is_rational (s)

	make_from_real32 (r: REAL_32)
			-- Create from a REAL_32, r

	make_from_real64 (r: REAL_64)
			-- Create from a REAL_64

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)

convert
	default_create ({STRING_8}),
	default_create ({REAL_32}),
	default_create ({REAL_64}),
	default_create: {REAL_32},
	default_create: {REAL_64},
	default_create: {STRING_8}

feature -- Access

	generating_type: TYPE [detachable RATIONAL]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generating_type_not_void: Result /= Void

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))

	frozen is_deep_equal (other: RATIONAL): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)

	is_greater alias ">" (other: RATIONAL): BOOLEAN
			-- Is current object greater than other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = (other < Current)

	is_greater_equal alias ">=" (other: RATIONAL): BOOLEAN
			-- Is current object greater than or equal to other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = (other <= Current)

	is_less_equal alias "<=" (other: RATIONAL): BOOLEAN
			-- Is current object less than or equal to other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = ((Current < other) or (Current ~ other))

	max (other: RATIONAL): RATIONAL
			-- The greater of current object and other
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			current_if_not_smaller: Current >= other implies Result = Current
			other_if_smaller: Current < other implies Result = other

	min (other: RATIONAL): RATIONAL
			-- The smaller of current object and other
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			current_if_not_greater: Current <= other implies Result = Current
			other_if_greater: Current > other implies Result = other

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))

	frozen standard_is_equal (other: RATIONAL): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)

	three_way_comparison (other: RATIONAL): INTEGER_32
			-- If current object equal to other, 0;
			-- if smaller, -1; if greater, 1
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			equal_zero: (Result = 0) = (Current ~ other)
			smaller_negative: (Result = -1) = (Current < other)
			greater_positive: (Result = 1) = (Current > other)
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
	
feature -- Duplication

	copy (other: RATIONAL)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_equal: Current ~ other

	frozen deep_copy (other: RATIONAL)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)

	frozen deep_twin: RATIONAL
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)

	frozen standard_copy (other: RATIONAL)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)

	frozen standard_twin: RATIONAL
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)

	frozen twin: RATIONAL
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
	
feature -- Basic operations

	frozen default: detachable RATIONAL
			-- Default value of object's type
			-- (from ANY)

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
	
feature -- Commands

	canonicalize
			-- Canonicalize the current rational by dividing its numerator
			-- and denominator by their GCD.
	
feature -- Internal queries

	get_p_q (s: STRING_8): TUPLE [a_p: like s; a_q: like s]
			-- Returns the numerator and denominator parsed from s.
		require
				string_is_rational (s)
	
feature -- Operations

	abs: like Current
			-- Absolute value of Current
			-- Was declared in RATIONAL as synonym of absolute.

	absolute: like Current
			-- Absolute value of Current
			-- Was declared in RATIONAL as synonym of abs.

	add (other: like Current): like Current
			-- Return the result of adding Current to other
			-- Was declared in RATIONAL as synonym of plus.

	divide alias "//" (other: like Current): like Current
			-- Return the result of dividing Curent by other
			-- Was declared in RATIONAL as synonym of quotient.
		require
			denominator_non_zero: other /~ zero

	exp (other: INTEGER_32): RATIONAL
			-- Return the value of Current raised to the power other
			-- Was declared in RATIONAL as synonym of power.

	minus alias "-" (other: like Current): like Current
			-- Return the result of subtracting other from Current
			-- Was declared in RATIONAL as synonym of subtract.

	multiply (other: like Current): like Current
			-- Return the result of multiplying Current by other
			-- Was declared in RATIONAL as synonym of product.

	negate: like Current
			-- Negate Current
			-- Was declared in RATIONAL as synonym of opposite.

	opposite alias "-": like Current
			-- Negate Current
			-- Was declared in RATIONAL as synonym of negate.

	plus alias "+" (other: like Current): like Current
			-- Return the result of adding Current to other
			-- Was declared in RATIONAL as synonym of add.

	power alias "^" (other: INTEGER_32): RATIONAL
			-- Return the value of Current raised to the power other
			-- Was declared in RATIONAL as synonym of exp.

	product alias "*" (other: like Current): like Current
			-- Return the result of multiplying Current by other
			-- Was declared in RATIONAL as synonym of multiply.

	quotient alias "/" (other: like Current): like Current
			-- Return the result of dividing Curent by other
			-- Was declared in RATIONAL as synonym of divide.
		require
			denominator_non_zero: other /~ zero

	reciprocal: like Current
			-- Return 1 / Current

	square: RATIONAL
			-- Return the value of Current * Current

	subtract (other: like Current): like Current
			-- Return the result of subtracting other from Current
			-- Was declared in RATIONAL as synonym of minus.
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
	
feature -- Queries

	as_real32: REAL_32
			-- Represent Current as a REAL_32
		require
				is_valid_real_32

	as_real64: REAL_64
			-- Represent Current as a REAL_64
		require
				is_valid_real_64

	debug_output: STRING_8
			-- Debut output for Current
		ensure -- from DEBUG_OUTPUT
			result_not_void: Result /= Void

	divisible (other: like Current): BOOLEAN
			-- May current object be divided by other?

	exponentiable (other: NUMERIC): BOOLEAN
			-- May current object be elevated to the power other?

	identity alias "+": like Current
			-- Unary plus

	int_zero: BIG_INTEGER
			-- Returns an Integer with value 0 for creation

	is_canonical: BOOLEAN
			-- Is Current in canonical form?

	is_equal (other: like Current): BOOLEAN
			-- Is other value equal to current
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		ensure then -- from COMPARABLE
			trichotomy: Result = (not (Current < other) and not (other < Current))

	is_less alias "<" (other: like Current): BOOLEAN
			-- Is current object less than other?
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			asymmetric: Result implies not (other < Current)

	is_valid_real_32: BOOLEAN
			-- Is Current a valid REAL_32?

	is_valid_real_64: BOOLEAN
			-- Is Current a valid REAL_64?

	one: like Current
			-- Neutral element for "*" and "/"

	out: STRING_8
			-- Return a string representation of Current
		ensure -- from ANY
			out_not_void: Result /= Void

	string_is_float (s: STRING_8): BOOLEAN
			-- Does s represent a valid and well-defined decimal number
			-- with potentially a floating point? This function was copied
			-- from {INT}.ensureValid, which came from mathmodels' VALUE.
		require
			non_void: s /= Void
			non_empty: not s.is_empty

	string_is_fraction (s: STRING_8): BOOLEAN
			-- Does s represent a valid and well-defined fraction?
			-- It must contain at most one '/', which separates the numerator
			-- from the denominator (no whitespace allowed). Also, either
			-- the numerator or denominator must be non-zero. Further, if '/'
			-- is present, what comes before and after it must be valid INTs.
			-- If s doesn't contain a '/', a denominator of 1 is assumed.

	string_is_rational (s: STRING_8): BOOLEAN
			-- Does s represent a fraction or a decimal with a floating
			-- point?

	zero: like Current
			-- Neutral element for "+" and "-"
	
feature -- rounding

	round_to (digits: INTEGER_32): STRING_8
			-- Round Current to digits number of digits. Uses half-up rounding.
	
invariant
	well_defined: q /~ q.zero

		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

		-- from COMPARABLE
	irreflexive_comparison: not (Current < Current)

end -- class RATIONAL

Generated by ISE EiffelStudio